验证文本框文本并增加选项卡索引一次
嗨,我知道我的代码出了问题,但不知道如何修复它...
在 TextChanged 事件中,我调用我的验证函数,该函数执行(应该执行)以下操作:
- 删除任何非字母字符
- 将输入的字母为大写
- 仅允许文本框中的一个字符
- 使用 SendKeys 来增加选项卡索引(转到下一个文本框)
问题是因为它处于 textchanged 事件中,我试图阻止它两次选项卡(其中它正在做)。因为如果我单步执行,输入的首字母是第一个 textchanged 事件,那么如果它是一个 notallowedcharacter,则会再次调用该函数,但如果它是一个字母,则 ToUpper 可能会再次更改它,因此选项卡会发送两次。有什么想法吗?我知道有一种方法可以做到这一点,而无需设置一些复杂的布尔值......
private void validateTextInteger(object sender, EventArgs e)
{
TextBox T = (TextBox)sender;
try
{
//Not Allowing Numbers, Underscore or Hash
char[] UnallowedCharacters = { '0', '1','2', '3', '4', '5','6', '7','8', '9','_','#','%','$','@','!','&',
'(',')','{','}','[',']',':','<','>','?','/','=','-','+','\\','|','`','~',';'};
if (textContainsUnallowedCharacter(T.Text, UnallowedCharacters))
{
int CursorIndex = T.SelectionStart - 1;
T.Text = T.Text.Remove(CursorIndex, 1);
//Align Cursor to same index
T.SelectionStart = CursorIndex;
T.SelectionLength = 0;
}
}
catch (Exception) { }
T.Text = T.Text.ToUpper();
if (T.Text.Length > 0)
{
//how do i prevent this (or this function) from getting called twice???
SendKeys.Send("{TAB}");
}
}
hi i know where my code is going wrong, but don't know how to fix it...
on the TextChanged event, i call my validation function which does (is supposed to do) the following:
- remove any non letter character
- convert the entered letter to upper case
- only allow one character in the textbox
- use SendKeys to increase the tab index (go to next textbox)
problem is since it is in the textchanged event, i'm trying to fight it to prevent it from tabbing twice (which it is doing). because the if i step through, the initial letter entered is the first textchanged event, then if it is a notallowedcharacter, the function is called again, but if it is a letter, the ToUpper may be changing it again so tab is getting sent twice. any ideas? i know there's a way to do this without setting up some complex bools....
private void validateTextInteger(object sender, EventArgs e)
{
TextBox T = (TextBox)sender;
try
{
//Not Allowing Numbers, Underscore or Hash
char[] UnallowedCharacters = { '0', '1','2', '3', '4', '5','6', '7','8', '9','_','#','%','
,'@','!','&',
'(',')','{','}','[',']',':','<','>','?','/','=','-','+','\\','|','`','~',';'};
if (textContainsUnallowedCharacter(T.Text, UnallowedCharacters))
{
int CursorIndex = T.SelectionStart - 1;
T.Text = T.Text.Remove(CursorIndex, 1);
//Align Cursor to same index
T.SelectionStart = CursorIndex;
T.SelectionLength = 0;
}
}
catch (Exception) { }
T.Text = T.Text.ToUpper();
if (T.Text.Length > 0)
{
//how do i prevent this (or this function) from getting called twice???
SendKeys.Send("{TAB}");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在 Tab 键顺序中找到下一个可见控件并对其调用 Focus,而不是使用 SendKeys 模拟 TAB 按键。像这样的事情:
要调用此方法,请将
SendKeys.Send("{TAB}");
替换为FocusOnNextVisibleControl(T);
Instead of using SendKeys to simulate a TAB keypress, you can find the next visible control in the tab order and call Focus on it. Something like this:
To call this method, replace
SendKeys.Send("{TAB}");
withFocusOnNextVisibleControl(T);