本文介绍了从文本框更改为下一个文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题!如何从一个文本框切换到另一个文本框?!

没有指定MaxLength。按Enter键时应切换到下一个文本框。



谢谢

I have a question! How do I switch from one text box to another?!
There is no MaxLength specified. It should be switched to the next textbox when pressing the Enter key.

Thank you

推荐答案


private void TextBoxes_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        TextBox theTextBox = sender as TextBox;

        switch (theTextBox.Name)
        {
            case "textBox1":
                textBox2.Focus();
                break;
            case "textBox2":
                textBox3.Focus();
                break;
            case "textBox3":
                textBox1.Focus();
                break;
        }
    }
}

当然,最简单的方法是使用设置TabOrder,但是,如果TextBox在同一个Form或ContainerControl中,其他您想要响应按Tab键的控件,然后您可能想要显式操作Focus订单,如上面的代码所示。



它很便宜并且有效,只需将您想要的TextControl选项卡放在ContainerControl中,就​​像Panel中的隔离一样,这样您就可以轻松地在它们之间进行制表。您可以使Panel无边框,并将其颜色设置为它自己的任何背景,如果您希望隐藏它。

Of course, the easiest way is to just use setting TabOrder, but, if the TextBoxes are in the same Form, or ContainerControl, with other Controls that you want to respond to the Tab Key being pressed, then you may want to explicitly manipulate the Focus order, as shown in the code above.

It's cheap, and effective, to simply put the TextControls you want to tab among in a ContainerControl like a Panel in "isolation" so you get tabbing among them easily. You can make the Panel borderless, and set its color to the whatever background its own, if you wish to kind-of hide it.


这篇关于从文本框更改为下一个文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 23:26