[英]Stop the 'Ding' when pressing Enter
我有一個非常簡單的 Windows Forms 應用程序。 並且,在 Windows(或至少 Windows Forms 應用程序)中,當您在單行文本框控件中按 Enter 鍵時,您會聽到叮當聲。 這是一個令人不快的聲音,表明您不能輸入換行符,因為它是一個單行文本框。
這一切都很好。 但是,在我的表單中,我有 1 個文本框和一個搜索按鈕。 而且我允許用戶在完成輸入后通過按 Enter 來執行搜索,因此他們不必使用鼠標單擊搜索按鈕。
但這丁聲卻出現了。 這很煩人。
我們怎樣才能讓它在我的表單中根本不播放聲音?
@David H - 這是我檢測輸入的方式:
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
// Perform search now.
}
}
這個對我有用:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
//Se apertou o enter
if (e.KeyCode == Keys.Enter)
{
//enter key is down
this.doSomething();
e.Handled = true;
e.SuppressKeyPress = true;
}
}
SuppressKeyPress 是真正的訣竅。 我希望對你有所幫助。
查看Form.AcceptButton屬性。 您可以使用它為表單指定默認按鈕,在本例中為按 Enter。
從文檔:
此屬性使您能夠指定當用戶在應用程序中按下 ENTER 鍵時發生的默認操作。 分配給此屬性的按鈕必須是當前窗體上或位於當前窗體上的容器內的 IButtonControl。
當用戶按下轉義鍵時,還有一個CancelButton屬性。
嘗試
textBox.KeyPress += new KeyPressEventHandler(keypressed);
private void keypressed(Object o, KeyPressEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.Handled = true; //this line will do the trick
}
}
只需添加e.SuppressKeyPress = true;
在你的“如果”聲明中。
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
//If true, do not pass the key event to the underlying control.
e.SuppressKeyPress = true; //This will suppress the "ding" sound.*/
// Perform search now.
}
}
您可以使用 KeyPress 而不是 KeyUp 或 KeyDown 它更有效,這里是如何處理
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
e.Handled = true;
button1.PerformClick();
}
}
和'丁'說和平
使用SuppressKeyPress
在處理擊鍵后停止繼續處理擊鍵。
public class EntryForm: Form
{
public EntryForm()
{
}
private void EntryTextBox_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
{
e.Handled = true;
e.SuppressKeyPress = true;
// do some stuff
}
else if(e.KeyCode == Keys.Escape)
{
e.Handled = true;
e.SuppressKeyPress = true;
// do some stuff
}
}
private void EntryTextBox_KeyUp(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
{
// do some stuff
}
else if(e.KeyCode == Keys.Escape)
{
// do some stuff
}
}
}
在 WinForms 上,Enter 鍵會導致 Ding 聲音,因為未指定表單屬性 AcceptButton。 如果您不需要 AcceptButton,則可以通過將表單 KeyPreview 設置為 true 並輸入以下 KeyPress 事件來抑制叮當聲:
private void Form_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
e.Handled = true;
}
無論激活哪個控件,按下 Enter 鍵時都不會再發出叮當聲。 由於鍵事件處理順序是 KeyDown、KeyPress 和 KeyUp,因此 Enter 鍵仍然適用於控件的 KeyDown 事件。
我在嘗試處理對我有用的 KeyDown 時偶然發現了這篇文章。
If e.KeyCode = Keys.Enter Then
e.SuppressKeyPress = True
btnLogIn.PerformClick()
End If
按下按鍵會停止將事件發送到底層控件。 如果您手動處理輸入鍵將在該文本框中執行的所有操作,這應該可以工作。 對不起Visual Basic。
任何人都很少有機會得到這個答案,但其他一些答案真的很可怕。 抑制KeyDown
上的事件會在一次打擊中殺死 2 個額外的事件。 在這種情況下,將e.Handled
屬性設置為true
是沒有用的。
最好的方法是將Form.AcceptButton
屬性設置為實際的搜索按鈕。
還有另一種使用Enter
鍵的方法 - 有些人可能希望它充當TAB
按鈕。 為此,添加一個新Button
,將其Location
屬性設置在Form
區域之外(即(-100, -100)
) - 在某些情況下,將Visible
屬性設置為false
可能會禁用Button
處理程序。 將Form.AcceptButton
屬性設置為新按鈕。 在Click
事件處理程序中添加以下代碼this.SelectNextControl(ActiveControl, true, true, true, true)
現在,您可能只想在將focus
放在TextBox
上時才轉移focus
,您可能想要測試ActiveControl
類型或在不打算將Enter
用作TAB
的控件的事件處理程序中使用e.Supress
屬性就是這樣。 您甚至不需要捕獲e.KeyCode
$("#txtSomething").keypress(function (e) {
if (e.which == 13) {
e.Handled = true; //This will prevent the "ding" sound
//Write the rest of your code
}
});
好吧,我在這個問題上生活了足夠長的時間,並在這里查找了它。
在考慮了很長時間並想要最簡單的方法來修復它之后,我想出了最簡單但不太優雅的修復方法。
這就是我所做的。
這解決了該線程中列出的所有次要問題,包括 ToolStripMenu。 我最大的抱怨是 BindingNavigator,當我在 Current position 中輸入記錄號以導航到並按下回車時。
根據程序員在按下回車按鈕時想要搜索 function 的原始問題,我只是將搜索代碼放在不可見的確定按鈕中!
到目前為止,這似乎解決了所有問題,但我們都知道使用 Visual Studio,可能會出現一些問題。
我能想到的唯一其他可能的優雅方式是編寫一個新的擊鍵處理 class 這對於我的大多數項目來說都是很多工作的方式。
您可以將文本框多行設置為 true,然后處理 Enter 按鍵。
private void yourForm_Load(object sender, EventArgs e)
{
textBox1.Multiline = true;
}
//then write your TextBox codes
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
// doSomething();
}
}
我更改了多行文本框的文本框屬性,它對我有用。
將 Search 按鈕的IsDefault屬性設置為true
。 這將使其成為默認按鈕,並在按下 Enter 時自動單擊。
關於e.SuppressKeyPress = true;
解決方案,它本身就可以正常工作。 將SuppressKeyPress
設置為 true 也會將Handled
設置為 true,因此無需使用e.Handled= true;
void RTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Enter)
{
//do ...
bool temp = Multiline;
Multiline = true;
e.Handled = true;
Multiline = temp;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.