文本框焦点 [英] textbox focus

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

问题描述



我在运行时在C#中创建了一些文本框.
我想通过将一个字母从一个文本框键入另一个文本框(用于填字游戏)来更改文本框的焦点:

只需输入字母即可检查代码
必须改变这种方法的重点?如何?
我的表格有一个组合框,该框是(水平,垂直,非活动).当组合框索引更改时,更改方向的文本框也会更改.(向下或向左或向右或向上)



I created some text boxes in C# at run time.
I want to change focus of text box by typing a letter from a text box to another text box (for a crossword puzzle):

it''s code for check that just enter letter
must change focus in this method?how?
my form has a combo box that this items are (horizontal,vertica,deactive). when combo box index change, direction of change text boxes change too.(down or left or right or up)

private void textBox_KeyPress(object sender, KeyPressEventArgs e)
       {
           // Allows only letters
           if (!(Char.IsLetter(e.KeyChar) || Char.IsControl(e.KeyChar)))
           {
                   e.Handled = true;

           }
       }



在此部分中创建文本框



in this part create text boxes

public void CreateTb()
        {
            for (i = 0; i < 15; i++)
            {
                for (j = 0; j < 15; j++)
                {
                    CrossTb = new TextBox();
                    TbTemp[i, j] = CrossTb;
                    CrossTb.Multiline = true;
                    CrossTb.Size = new Size(w, h);
                    CrossTb.MaxLength = 1;
                    CrossTb.TextAlign = HorizontalAlignment.Center;
                    CrossTb.BackColor = Color.White;
                    CrossTb.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
                    CrossTb.Location = new Point((i + fx) * w, (j + fy) * h);
                    this.Controls.Add(CrossTb);
TbTemp[i, j].KeyPress += new KeyPressEventHandler(textBox_KeyPress);
                }
                //TbTemp[10, 10].ForeColor = Color.Black;
                CreatePanel();
            }



这是我创建文本框的代码.
我必须在哪里检查密钥才能转到另一个文本框?如何?
每当我测试不正确时:(

非常感谢您的帮助:)



It''s my code to create textboxes.
where i must check key for go to another text box and how?
whenever that i test doesn''t true :(

Thanks a lot for your help :)

推荐答案

为TextBox KeyPress事件添加处理程序.
确定接下来要接收按键的文本框,然后使用Focus()方法.
例如,如果只有两个框:
Add a handler for the TextBox KeyPress event.
Decide which text box is next to receive the keystroke and use the Focus() method.
For example, if there were two boxes only:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
    textBox2.Focus();
    }

private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
    {
    textBox1.Focus();
    }

您将需要一个更通用的方法,但是方法就交给您了-下一步的选择取决于您组织软件的方式.

You will want a more generic method, but that is over to you - next right or down will depend on how you are organising your software.


您可以通过代码调用tab事件将焦点从一个文本框移到另一个文本框.
You can call tab event through code to move focus from one textbox to the other.


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

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