在C#Windows窗体中自动填充文本框选定的索引已更改时填充文本框

时间:2019-01-18 12:20:49

标签: c#

当用户从自动填充文本框中选择一个字符串时,我只想填充另一个文本框,我正在使用此代码自动填充文本框。

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); 
    }
}

1 个答案:

答案 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;
    }
}
相关问题