从文本框更改为下一个文本框 [英] change from a textbox into the next one

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

问题描述

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

没有指定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

推荐答案

有看看这里 [ ^ ]你应该得到你想要的东西。它不使用keypress事件,而是使用processcmdkey和/或ProcessKeyPreview。



这是KeyPress keypress和keydown事件 [ ^ ]作为另一种解决方案。





作为替代方案,您可以将控件的TabIndex设置为您想要的顺序。当然,它与按输入不同,但存在相同的功能,只需使用Tab键。
Have a look here[^] and you should get what you are looking for. It does not use the keypress event, but the processcmdkey and/or ProcessKeyPreview.

Here is the KeyPress keypress and keydown events[^] as another solution.


As an alternative, you can set the "TabIndex" of the controls to be in the order you desire. It, of course, is not the same as pressing "enter" but the same functionality exists, just with the tab key.


假设您使用的TextBox不是多行的......



你可以指定你带的文本框以便在按下Enter键时使用相同的KeyDown EventHandler:
Assuming the TextBoxes you use are not Multi-Line ...

You can assign the TextBoxes you with to have shift Focus when the Enter key is pressed to all use the same KeyDown EventHandler:
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.


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

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆