c# - Filling textbox when autofill text box selected index changed in C# windows form
问题描述
I just want to fill the other textbox when user selected an string from the autofill textbox1 i am using this code for autofill a textbox.
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);
}
}
解决方案
没有用于选择自动填充的事件。大多数人所做的就是检测是否点击了ENTER键。我建议还检测 TAB 键并处理该PreviewKeyDown
事件:
private void textBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyData == Keys.Enter || e.KeyData == Keys.Tab)
{
textBox2.Text = textBox1.Text;
}
}
推荐阅读
- rust - Warp 中的 API 密钥验证 (Rust)
- powershell - 每个无法访问或超时的 ping 之间的 Powershell 时间延迟
- sql-server - 仅在调试模式下在发布中包含 PostDeployment sql 脚本
- c++ - 未执行的函数抛出异常
- c# - 如何使用“KeyCode”检查复选框?(Windows 窗体应用程序)
- javascript - 如何在本机反应中创建动态下拉选择器
- react-native - 使用 npx react-native start 命令出错
- azure - 使用按钮发送天蓝色推送通知
- flutter - 如何在我的 listview.builder 完成构建列表视图之前显示进度指示器
- pine-script - 在策略循环中保存变量(Pine 脚本)