![](/img/trans.png)
[英]DataGridView TextBox cell Editing, Change Enter Key to Act Like Tab
[英]How to make Enter on a TextBox act as TAB button
我有幾個文本框。 我想讓 Enter 按鈕充當 Tab。 因此,當我在一個文本框中時,按 Enter 會將我移到下一個。 你能告訴我如何在不添加任何代碼的情況下在文本框類中實現這種方法(如果可能,不要覆蓋等等)?
這是我經常使用的代碼。 它必須是KeyDown事件。
if (e.KeyData == Keys.Enter)
{
e.SuppressKeyPress = true;
SelectNextControl(ActiveControl, true, true, true, true);
}
UPDATE
其他方式是發送“TAB”鍵! 並覆蓋該方法使它更容易:)
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Enter))
{
SendKeys.Send("{TAB}");
}
return base.ProcessCmdKey(ref msg, keyData);
}
你可以寫任何控件的keyDown :
if (e.KeyCode == Keys.Enter)
{
if (this.GetNextControl(ActiveControl, true) != null)
{
e.Handled = true;
this.GetNextControl(ActiveControl, true).Focus();
}
}
GetNextControl在Vista上不起作用。
要使它適用於Vista,您需要使用下面的代碼來替換this.GetNextControl ...:
System.Windows.Forms.SendKeys.Send("{TAB}");
您不需要創建“輸入事件處理程序”
您需要做的就是制作一個“中心”KeyDown事件:
例
private void General_KeyDown(object sender, KeyPressEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (this.GetNextControl(ActiveControl, true) != null)
{
e.Handled = true;
this.GetNextControl(ActiveControl, true).Focus();
}
}
}
然后你要做的就是去設計師選擇你想用EnterKey循環的所有文本框(通過按住Ctrl然后用鼠標點擊文本框來選擇它們)然后轉到事件(像按鈕那樣的雷聲),搜索Keydown事件並輸入在General_KeyDown里面。 現在你所選擇的所有文本框都將具有相同的keydown事件:)這使得一切都變得更容易,因為想象一個包含100個文本框的表單,並且你想要使用enter循環所有....為每個texbox制作一個分開的事件是...好吧,不是一個制作節目的正確方法,它並不整潔。 希望它有所幫助!!
大段引用
重要的是要注意,如果每次控件期望相關的按鈕控制並且e.Handled = true
時,你會得到惱人的“叮”聲或警告聲並不總是能夠擺脫噪音/聲音的答案/丁。
如果控件(即單行文本框)期望相關按鈕“輸入”您的條目,那么您還必須處理“缺失”控件。
e.Handled = e.SuppressKeyPress = true;
當擺脫“叮”聲時,這可能會有所幫助。
在我的情況下,我的用戶需要使用“ENTER KEY”,因為我們正在從終端/綠屏應用程序轉換到Windows應用程序,他們更習慣於通過字段而不是標簽來“輸入” 。
所有這些方法都有效,但在我添加e.SuppressKeyPress
之前仍然保持令人討厭的聲音。
如果您定義所有控件的Tab Order並使Form.KeyPreview = True,則只需要:
Private Sub frmStart_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Enter Then
System.Windows.Forms.SendKeys.Send("{TAB}")
End If
End Sub
這對我有用
if (e.Key == Key.Enter)
((TextBox)sender).MoveFocus(new TraversalRequest(new FocusNavigationDirection()));
對於那些在vb中編碼的人...
Public Class NoReturnTextBox
Inherits System.Windows.Forms.TextBox
Const CARRIAGE_RETURN As Char = Chr(13)
' Trap for return key....
Private Sub NoReturnTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
If e.KeyChar = CARRIAGE_RETURN Then
e.Handled = True
System.Windows.Forms.SendKeys.Send(vbTab)
End If
End Sub
End Class
我在其中一個文本框keydown事件中使用此代碼
if (e.KeyData == Keys.Enter)
{
e.SuppressKeyPress = true;
SelectNextControl(ActiveControl, true, true, true, true);
}
無法處理表單中所有文本框的keydown
事件。 建議一些事情。 謝謝
這是我用於VB.NET的解決方案
在表單屬性中設置Keypreview = True 。
將此代碼放在表單keydown事件中:
如果(e.KeyData = Keys.Enter)那么
'對於任何多行控件,你必須退出讓多行'文本框介紹'按鍵才能使行跳過。
If ActiveControl.Name = txtMyMutilineTextBox.Name Then Exit Sub e.SuppressKeyPress = True SelectNextControl(ActiveControl, True, True, True, True)
萬一
請享用 !!!!
Xabier Aberasturi Larruzea
我沒有在表單級別執行此操作。 我創建了一個單一的方法,我在所有輸入KeyDown事件中共享,我想要這樣做(有一個例外):
private void alltextBoxes_KeyDown(object sender, KeyEventArgs e)
{
try
{
if (e.KeyCode == Keys.Enter)
{
e.SuppressKeyPress = true;
SelectNextControl(ActiveControl, true, true, true, true);
}
}
catch
{
}
}
如果我正在編寫一個我想在其他應用程序中使用的控件,我會給出最后一個輸入它自己的方法,如下所示:
private void lastinput_KeyDown(object sender, KeyEventArgs e)
{
try
{
if (e.KeyCode == Keys.Enter)
{
e.SuppressKeyPress = true;
System.Windows.Forms.SendKeys.Send("{TAB}");
}
}
catch
{
}
}
否則,當您嘗試在另一個項目中運行它時,控件只會在其自身內部循環。 你可以在任何地方使用第二種方式,但我認為第一種方式是首選方式。
這個更好
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Enter))
{
SelectNextControl(ActiveControl, true, true, true, true);
}
return base.ProcessCmdKey(ref msg, keyData);
}
猜測一下:
// on enter event handler
parentForm.GetNextControl().Focus();
我會把Pharabus和arul的回答結合起來:
private void textBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == ‘\r’)
{
e.Handled = true;
parentForm.GetNextControl().Focus()
}
}
如果這有幫助,請告訴我! JFV
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.