通过键盘点击回车键选择ComboBox中下拉的元素



当我们选择ComboBox中下拉的元素时,可以直接使用鼠标点击选取,当有特殊需要想要通过敲回车选中元素时,可以使用键盘事件KeyPress
代码如下:

   private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            comboBox1.Text = comboBox1.SelectedItem.ToString();//show the selectieditem to the combobox.text
            Show(dataGridView1);//replace it with your load data method
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Show(dataGridView1);
        }

        public static void Show(DataGridView dgw)
        {
            //add your method load data
            DataSet customerDataSet = new DataSet();
            customerDataSet.Tables.Add(new DataTable("Customers"));
            customerDataSet.Tables["Customers"].Columns.Add("Name", typeof(string));
            customerDataSet.Tables["Customers"].Columns.Add("CountryRegion", typeof(string));
            customerDataSet.Tables["Customers"].Rows.Add("Juan", "Spain");
            customerDataSet.Tables["Customers"].Rows.Add("Johann", "Germany");
            customerDataSet.Tables["Customers"].Rows.Add("John", "UK");
            dgw.DataSource = customerDataSet.Tables["Customers"];
        }

——————————————————————————————————————————————————————————————————————————————————————————————————————————————

欢迎大神光临菜鸟博客,希望能得到各位大神在编码方面的指引,同时欢迎与我一样刚进入编程世界的朋友一起讨论学习!相信前进的道路上,有你们,编程世界会更加精彩!



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值