What causes a system signal?

This is what I use so that the enter button starts the search. It works, but it triggers a system beep. I have no idea why.

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

This happens as soon as I press the enter button, and not some other line in the code. Thoughts on what I am missing?

Here's the 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; } } 
+4
source share
2 answers

You can try adding e.Handled = true; to the KeyPressed event for your TextBox .

Usually, if your Form does not have its AcceptButton property, the system sound is played when you press Enter inside a TextBox . A beep indicates that there is no default button for your Form .

+1
source

Add the following event handler (in addition to KeyUp ):

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

That is, sign up for a KeyPress event and when you press Enter set e.Handled to true . I just tested it on my machine and it worked; he removed the beep.

+1
source

All Articles