动态地中窗体中添加一个TEXTBOX控件数组。如何按回车让焦点在TEXTBOX控件数组中移动

解决方案 »

  1.   


    private void Form1_Load(object sender, System.EventArgs e)
    {
       TextBox []txt = new TextBox[10];
       for(int i=0;i<10;i++)
       {
          txt[i]= new TextBox();
          txt[i].Location = new System.Drawing.Point(8, 10+i*30);
          txt[i].Size = new System.Drawing.Size(50, 21);
          txt[i].TabIndex = i;
          txt[i].KeyPress+=new KeyPressEventHandler(txt_KeyPress);
          this.Controls.Add(txt[i]);
       }
    }
    private void txt_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
    if(e.KeyChar==13)
    {
    SendKeys.Send("{TAB}");
    }
    }
      

  2.   

    和1楼的方法差不多,参考如下代码:
    protected override bool ProcessDialogKey(Keys keyData)
    {
        if ((ActiveControl is TextBox || ActiveControl is ComboBox) && 
            keyData == Keys.Enter)
        {
            keyData = Keys.Tab;
        }
        return base.ProcessDialogKey(keyData);
    }