这就是我用来允许输入按钮开始搜索的内容.它工作但导致系统发出蜂鸣声.我不知道为什么.
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;
}
}
解决方法:
您可以尝试添加e.Handled = true;到TextBox的KeyPressed事件.
通常,如果您的表单没有设置其AcceptButton属性,则在TextBox中按Enter键会播放系统提示音.蜂鸣声表示没有为您的表单定义默认按钮.