我正在绑定事件textbox1
数据LostFocus
。我设置了键盘导航。Tabindex=7
对于textbox1
和textbox2
键盘导航TabIndex=8
。现在我的问题是正在对 进行正则表达式验证textbox1
,如果我在其中输入无效字符,textbox1
则显示MessageBox
无效,并且一旦单击确定,它将导航到textbox2
我想要设置此键盘导航的位置,textbox1
直到我输入有效字符。我怎样才能做到这一点?
我试过这样:
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);
}
txtbox1
如果输入的文本无效,如何将键盘导航设置为?任何建议。
编辑:添加了 xaml
<Window x:Class="DataBinding.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<TextBox Name="txtbox1" Margin="71,22,82,195" LostFocus="txtbox1_LostFocus" />
<TextBox Name="txtbox2" Margin="71,96,82,127" />
</Grid>