当用户从自动填充文本框中选择一个字符串时,我只想填充另一个文本框,我正在使用此代码自动填充文本框。
private void frmHistory_Load(object sender, EventArgs e)
{
try
{
string query = "select ID from Customer ";
SqlCommand cmd = new SqlCommand(query,con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
AutoCompleteStringCollection mycollection = new AutoCompleteStringCollection();
while(dr.Read())
{
mycollection.Add(dr.GetInt32(0).ToString());
}
textBox1.AutoCompleteCustomSource = mycollection;
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
答案 0 :(得分:0)
没有任何事件可以选择自动填充。大多数人所做的就是检测是否单击了ENTER键。我建议还检测TAB键,并处理PreviewKeyDown
事件:
private void textBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyData == Keys.Enter || e.KeyData == Keys.Tab)
{
textBox2.Text = textBox1.Text;
}
}