2013-06-25 35 views
0

我在窗體中有幾個控件,所以我閱讀並創建了一個窗體的事件,允許我按ENTER將焦點更改爲另一個控件。這裏是代碼:textbox將不會接受在winforms中輸入

private void frSeleccionEL_KeyDown(object sender, KeyEventArgs e) 
    { 
     if (e.KeyCode == Keys.Enter) 
     { 
      Control nextControl = this.GetNextControl(ActiveControl, !e.Shift); 
      if (nextControl != null) 
      { 
       e.Handled = true; 
       if (!(nextControl is Label)) 
       { 
        nextControl.Focus(); 
       } 
       else //the next control is currently a label 
       { 
        nextControl = this.GetNextControl(ActiveControl, true); 
        while (nextControl != null) 
        { 
         nextControl.Focus(); 
         if (!(nextControl is Label)) 
         { 
          break; 
         } 
         nextControl = this.GetNextControl(ActiveControl, true); 
        } 
       } 
      } 
     } 
    } 

在文本框中,它不工作,我有代碼只有數字。下面的代碼:

private void txtLocal_KeyPress(object sender, KeyPressEventArgs e) 
    { 
     //Para obligar a que sólo se introduzcan números 
     if (Char.IsDigit(e.KeyChar)) 
     { 
      e.Handled = false; 
     } 
     else 
     { 
      if (Char.IsControl(e.KeyChar)) //permitir teclas de control como retroceso 
      { 
       e.Handled = false; 
      } 
      else 
      { 
       //el resto de teclas pulsadas se desactivan 
       e.Handled = true; 
      } 
     }  
    } 

我的問題是,當我按下在該控制什麼也沒有發生一樣ENTER從未按下ENTER,因爲事件按鍵此控件不火。

我刪除了控件並重新制作了它,問題依然存在。

我不知道什麼是錯的。

+0

您需要將Multiline屬性設置爲True。 – Eterm

+0

如何將KeyPress EventHandler添加到文本框?你有2種方法中的1種,可以使用設計器或在代碼中將處理程序添加到文本框中。即txtLocal.KeyPress + = new KeyPressEventHandler(txtLocal_KeyPress); –

+0

我添加了keyPress EventHandler與設計師,如果我按下任何鍵事件的工作,但如果我按下ENTER不要做任何事情 –

回答

0

試試這個:

private void txtLocal_KeyPress(object sender, KeyPressEventArgs e) 
    { 
     //Para obligar a que sólo se introduzcan números 
     if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar)) 
     { 
      e.Handled = true; 
     } 
    } 
0

請在表格鍵使用此代碼下來這會給你更好的性能

private void frSeleccionEL_KeyDown(object sender, KeyEventArgs e) 
{ 
    if (e.KeyCode == Keys.Enter) 
    { 
     ProcessTabKey(true); 
    } 
} 

請確保您已設置的標籤索引上所有的控制。

然後不變的你的文字改變了事件代碼。如果您使用自定義文本框而不是在您的所有表單上編寫此代碼,那將會更好。

+0

與ProcessTabKey做同樣的事情,我設置所有的tad索引,與您的自定義文本框請參閱MaskedTextBox或者是另一個控制 –

+0

客戶文本框的意思,您只需擴展一個文本框並根據需要進行自定義。 <公共部分類NumericTextbox:TextBox> –

0

試試這個......

private void txtLocal_KeyPress(object sender, KeyPressEventArgs e) 
    { 
     //Para obligar a que sólo se introduzcan números 
     if (Char.IsDigit(e.KeyChar) || asc(e.KeyChar)==13) 
     { 
      e.Handled = false; 
     } 
     else 
     { 
      if (Char.IsControl(e.KeyChar)) //permitir teclas de control como retroceso 
      { 
       e.Handled = false; 
      } 
      else 
      { 
       //el resto de teclas pulsadas se desactivan     
       e.Handled = true; 

      } 
     }  
    } 
+0

不工作,就像輸入從未按下但TAB是的,我不知道什麼是錯的。當按鍵時調試代碼,如果我按任何鍵事件的工作,但如果我按下ENTER不要做任何事情 –

+0

@JoséGordilloLópez..不太確定你的真實情況..但你可以再試一次.. – matzone

1

你的第一個代碼太長,我們並不需要它。如果要將Enter鍵按下到Tab切換,請使用表單的SelectNextControl方法。我可以看到你的txtLocal_KeyPress與切換標籤無關,所以你如何知道發生了什麼或不是。 e.Handled只有幫助抑制按鍵事件或不。我猜你的TextBoxMultiline = true;,你想要抑制回車鍵,而不是切換到下一個控制,否則你不必在TextBoxMultiline=false中按下回車鍵。此短代碼將幫助您:

private void txtLocal_KeyPress(object sender, KeyPressEventArgs e){ 
    if(e.KeyChar == 13)//Enter{ 
     e.Handled = true; 
     SelectNextControl(txtLocal, true, true, true, true); 
    } 
} 

這就是爲了解決您的問題。 我不知道,如果你知道如何註冊您的TextBox.KeyPress事件KeyPress事件處理程序,所以我添加此來幫助你在這種情況下:

txtLocal.KeyPress += txtLocal_KeyPress; 
0

如果你想提交修改回車鍵,假設你想更新您的數據綁定,而是留在你可以使用下面的代碼

private void TextBox_KeyPress(object sender, EventArgs e) 
{ 
    if(e.KeyCode == (char)Keys.Enter) 
    { 
     TextBox.DataBindings[0].WriteValue(); 
     dataBindingSource.ResetCurrentItem(); 
     TextBox.Select(TextBox.Text.Length, 0); 
     e.Handled = true; 
    } 
} 

的重要組成部分控制有WriteValue()單位犯數據綁定事件觸發前的變化。如果您希望其他控件在提交更改後在窗體上更新,則ResetCurrentItem()將執行此操作。此外,這段代碼只是提交第一個綁定值,但是如果你有多個綁定,你可以用foreach循環和WriteValue()全部或者其他的方法來遍歷它們。 TextBox.Select()行只是在您按下回車鍵後將光標保留在您的位置。