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

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

本文介绍了验证文本框文本并增加选项卡索引一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我的代码出了问题,但不知道如何解决...

hi i know where my code is going wrong, but don't know how to fix it...

在TextChanged事件上,我调用了验证函数,该函数可以(应该这样做)进行以下操作:

on the TextChanged event, i call my validation function which does (is supposed to do) the following:

  • 删除任何非字母字符
  • 将输入的字母转换为大写字母
  • 在文本框中只允许一个字符
  • 使用SendKeys增加标签索引(转到下一个文本框)

问题是因为它处于textchanged事件中,所以我正在努力与它进行战斗,以防止它两次切换(它正在执行).因为如果我逐步执行,则输入的首字母是第一个textchanged事件,然后,如果它是一个不允许的字符,则再次调用该函数,但是如果是字母,则ToUpper可能会再次对其进行更改,因此制表符被发送两次.有任何想法吗?我知道有一种方法可以在不设置一些复杂的布尔值的情况下....

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}");
            }
        }

推荐答案

您可以使用选项卡顺序查找下一个可见控件,然后在其上调用Focus,而不是使用SendKeys模拟TAB按键.像这样:

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();
    }
}

要调用此方法,请将SendKeys.Send("{TAB}");替换为FocusOnNextVisibleControl(T);

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

这篇关于验证文本框文本并增加选项卡索引一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 22:44