验证文本框文本并增加选项卡索引一次

发布于 2025-01-03 17:06:46 字数 1565 浏览 0 评论 0原文

嗨,我知道我的代码出了问题,但不知道如何修复它...

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅沫记忆 2025-01-10 17:06:46

您可以在 Tab 键顺序中找到下一个可见控件并对其调用 Focus,而不是使用 SendKeys 模拟 TAB 按键。像这样的事情:

private void FocusOnNextVisibleControl(Control currentControl)
{
    Form form = currentControl.FindForm();
    Control nextControl = form.GetNextControl(currentControl, true);
    while (nextControl != null && !nextControl.Visible && nextControl != currentControl)
    {
        nextControl = form.GetNextControl(nextControl, true);
    }
    if (nextControl != null && nextControl.Visible)
    {
        nextControl.Focus();
    }
}

要调用此方法,请将 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:

private void FocusOnNextVisibleControl(Control currentControl)
{
    Form form = currentControl.FindForm();
    Control nextControl = form.GetNextControl(currentControl, true);
    while (nextControl != null && !nextControl.Visible && nextControl != currentControl)
    {
        nextControl = form.GetNextControl(nextControl, true);
    }
    if (nextControl != null && nextControl.Visible)
    {
        nextControl.Focus();
    }
}

To call this method, replace SendKeys.Send("{TAB}"); with FocusOnNextVisibleControl(T);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文