是什么导致系统发出蜂鸣声?

时间:2012-06-19 14:49:32

标签: c# system beep keyeventargs

这是我用来允许输入按钮开始搜索的内容。它工作但导致系统发出蜂鸣声。我不知道为什么。

    private void searchbox_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            searchbutton.PerformClick();
        }
        else
        {
            //Run.
        }

    }

只要按下输入而不是代码中的其他行,就会发生这种情况。关于我缺少什么的想法?

这是searchbutton_Click:

    private void searchbutton_Click(object sender, EventArgs e)
    {
        var searchvar = searchbox.Text;
        SqlParameter var1 = new SqlParameter(@"var1", SqlDbType.Text);
        var1.Value = "%" + searchvar + "%";
        var conn = new SqlConnection("Data Source=TX-MANAGER;Initial Catalog=Contacts;Integrated Security=True");
        var comm = new SqlCommand(@"SELECT [Name ID], First, Last, Address, City, State, ZIP FROM contacts WHERE (First LIKE @var1) OR (Last LIKE @var1)", conn);
        if (checkBox1.Checked == true)
        {
            comm.CommandText += "ORDER BY ZIP";
        }
        else
        {
            //Run.
        }
        try
        {
            comm.Parameters.Add(var1);
            conn.Open();
            comm.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter(comm);
            DataTable dt = new DataTable();
            da.Fill(dt);
            dataGridView1.DataSource = dt;
            conn.Close();
        }
        catch (Exception e1)
        {
            display_box.Text = e1.ToString();
            tabControl1.Focus();
        }
        finally
        {
            int rowcount = dataGridView1.RowCount - 1;
            count.Text = rowcount.ToString();
            tabControl1.SelectedTab = tabPage2;
        }

    }

2 个答案:

答案 0 :(得分:1)

您可以尝试为e.Handled = true;的{​​{1}}事件添加KeyPressed

通常情况下,如果您的TextBox未设置Form属性,则在AcceptButton内按Enter键会播放系统提示音。哔声表示没有为TextBox定义默认按钮。

答案 1 :(得分:1)

添加以下事件处理程序(除了KeyUp):

private void searchbox_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == '\r')
        e.Handled = true;
}

即订阅KeyPress事件,按下Enter时,将e.Handled设置为true。我只是在我的机器上测试了它并且它工作了;它取消了哔哔声。