Quantcast
Viewing all articles
Browse latest Browse all 18858

How to set keyboardnavigation.Tabindex from code

I am binding textbox1 data on LostFocus event. I set keyboard navigation. Tabindex=7 fortextbox1 and for textbox2 keyboardNavigation TabIndex=8. Now my problem is am doing regular expression validation for textbox1, if I enter invalid characters in textbox1 it displyas MessageBoxsaying not valid and as soon as click ok it will navigate to textbox2 where I want to set this keyboard navigation to textbox1 till i enter valid characters. How can i achieve this?

I tried this many ways but some way am lagging to achieve this logic...any suggestion!!

if (!string.IsNullOrEmpty(txtbox1.Text))
{
    if(Regex.IsMatch(txtbox1.Text, @"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"))
    {
        txtbox2.Text = "(" + txtbox1.Text + ")";
    }
    else
    {
        MessageBoxResult mbr;
        mbr=MessageBox.Show("please enter valid Email Id", "VMS", MessageBoxButton.OK, MessageBoxImage.Error);

        if (mbr == MessageBoxResult.OK)
        {
            Keyboard.Focus(txtbox1);
            txtbox1.Clear();
            // txtbox1.TabIndex = 7;
            //txtbox1.MoveFocus(new TraversalRequest(FocusNavigationDirection.Up));
            // txtbox2.MoveFocus(new TraversalRequest(FocusNavigationDirection.Previous));
        }

        //txtbox1.Focus();                

        // KeyboardNavigation.SetTabIndex(txtbox1, 6);
     }
}
else
{
    txtbox2.Text = string.Empty;
    // txtbox1.TabIndex = 7;
    //txtbox1.MoveFocus(new TraversalRequest(FocusNavigationDirection.Previous));
    //KeyboardNavigation.SetTabIndex(txtbox1, 7);
    // txtbox2.TabIndex=7;
    //Keyboard.Focus(txtbox2);
}
How can I set the keyboard navigation to txtbox1 if the text enter is invalid? Any suggestion.


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>