ben*_*ndr 111 c# audio system-sounds winforms
我有一个非常简单的Windows窗体应用程序.而且,在Windows(或至少Windows窗体应用程序)中,当您在单行TextBox控件中按Enter时,您会听到一个丁.这是一种不愉快的声音,表示你无法输入换行符,因为它是一个单行的TextBox.
这一切都很好.但是,在我的表单中,我有1个TextBox和一个搜索按钮.而且我允许用户按Enter键,他们已经完成录入后进行搜索,所以他们不具备使用鼠标点击搜索按钮.
但是这个丁声就出现了.这很烦人.
我们如何才能使声音在我的表格中根本不发挥?
@David H - 这是我如何检测输入按下:
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
// Perform search now.
}
}
Run Code Online (Sandbox Code Playgroud)
小智 180
这个对我有用:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
SuppressKeyPress是真正的伎俩.我希望能帮助你.
FIr*_*nda 52
尝试
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
}
}
Run Code Online (Sandbox Code Playgroud)
mdm*_*mdm 52
查看Form.AcceptButton属性.您可以使用它来指定表单的默认按钮,在这种情况下是按Enter键.
来自文档:
使用此属性可以指定当用户在应用程序中按Enter键时发生的默认操作.分配给此属性的按钮必须是位于当前表单上或位于当前表单上的容器内的IButtonControl.
当用户按下escape时,还有一个CancelButton属性.
bra*_*ong 12
只需添加e.SuppressKeyPress = true;
"if"语句即可.
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.
}
}
Run Code Online (Sandbox Code Playgroud)
Maw*_*rdy 11
您可以使用KeyPress而不是KeyUp或KeyDown,它更有效,这里是如何处理
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
e.Handled = true;
button1.PerformClick();
}
}
Run Code Online (Sandbox Code Playgroud)
并向'丁'说和平
小智 7
用于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
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
64742 次 |
最近记录: |