表單中實現按 斷行符號鍵 跳到下一個可選的TabIndex控制項

來源:互聯網
上載者:User
Form中一“textbox”,兩“button”,如何?在textbox中按下斷行符號響應button.click事件 :
1)把按鈕的tabindex依次設定.如btnSure 1 ,btnModify 2, textbox 5等
// 把下面的代碼放到表單中可以實現按斷行符號鍵跳到下一個可選的TabIndex控制項:
  protected override bool ProcessDialogKey(Keys keyData)
  {
   if (keyData == Keys.Enter && !(this.ActiveControl is Button))
   {
    keyData = Keys.Tab;
   }
   return base.ProcessDialogKey(keyData);
  }
 // 如果想對Button也執行這個規則,可以把!(this.ActiveControl is Button)去掉.
2)
private void txtName_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
  {
//   if (e.KeyCode == Keys.Enter)
//   {
//    if(this.Name=="修改")
//    this.btnModify.PerformClick();
//    else        //添加
//     this.btnSure.PerformClick();
//   }
  }

3)把Form的屬性:把AcceptButton設定為你要響應的Button.

if(operFlag==0)
    {
     this.btnModify.Visible=false;
     this.btnSure.Visible=true;
    // this.AcceptButton=btnSure;
    }
    else if(operFlag==1)
    {
     this.btnModify.Visible=true;
     this.btnSure.Visible=false;
    // this.AcceptButton=this.btnModify;
    }

另:
或者有多個TextBox,按下斷行符號時游標自動跳往下個Tab順序的TexeBox
// 把每個textbox的AcceptTab屬性設定為false。注意設定這組textbox的tabIndex設定為相臨的數字。
33
在一個textBox裡先 textBox1.text="abc";緊接著讓textBox1裡執行一個“斷行符號”鍵操作,不是手工按斷行符號鍵,是讓程式實現
private void txtBox_textChange(System.Object sender, System.EventArgs e)
{
    if(txtBox.text=="abc")
    {
         SendKeys.Send("{ENTER}");
     }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.