鼠标单击时触发事件,但方向键不触发 [英] Event being triggered on mouse click but not on directional keys

查看:94
本文介绍了鼠标单击时触发事件,但方向键不触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的申请中有这个问题。要解释发生的事情是我在从组合框列表中未选择有效项时使用leave事件来显示MessageBox。目前,如果我开始输入然后尝试用鼠标选择项目,但是如果我使用箭头键则不会出现。例如。我输入cen的地方,我想从列表中选择Camp Chicken Broth 10.5oz(17960),如果我使用方向键然后点击输入就可以了,但是如果我使用鼠标那么MessageBox(离开)事件)被提出。我还注意到它允许在MessageBox Leave Event关闭后通过鼠标进行选择。用户关闭MessageBox后选择一个项目是非常麻烦的。有没有解决这个问题?以下是我目前的休假活动功能



我的尝试:



So I have this issue from my application. To explain what is happening is that i am using the leave event to show a MessageBox when a valid item is not selected from the comboBox list. Currently it comes up if I start typing and then try to select the item with the mouse but doesnt come up if I use the arrow keys. For eg. where I type in "cen" and I want to select "Camp Chicken Broth 10.5oz (17960)" from the list, if I use the directional keys and then hit enter it is fine but if I use the mouse then the MessageBox(Leave Event) is raised. I notice also that it allows selection by the the mouse after the MessageBox Leave Event has been closed. Selecting an item after the user closes the MessageBox is quite bothersome. Is there a work around for this? Below is my current Leave Event function

What I have tried:

private void suggestComboBox1_Leave(object sender, EventArgs e)
      {
          ComboBox c_med = (ComboBox)sender;
          if (!suggestComboBox1.Items.Contains(c_med.Text))
          {
              MessageBox.Show("Not a valid Cari-med Item!");
          }
      }

推荐答案

好的我回顾了我的步骤和所有内容,并记得我改变了我的查询,从那以后然后我开始遇到这个问题。最初这是上面用comboBox填充值的函数:





Okay I retraced my steps and everything and remembered I had changed my query and since then I started having the issue. Originally this was the function which populated the comboBox above with values:


void fillCari()//fill Cari-med dropdown with values
   {
       try
       {

           string connectionString = "Data Source=CMDLAP126;Initial Catalog=Carimed_Inventory;User ID = sa; Password = 123456;";
           SqlConnection con2 = new SqlConnection(connectionString);
           con2.Open();
           string query = "SELECT DISTINCT Item_Description FROM dbo.Carimed";
           SqlCommand cmd2 = new SqlCommand(query, con2);

           SqlDataReader dr2 = cmd2.ExecuteReader();
           while (dr2.Read())
           {
               string cari_des = dr2.GetString(dr2.GetOrdinal("Item_Description"));
               suggestComboBox1.Items.Add(cari_des);
               suggestComboBox1.Text.Trim();
           }
           //con2.Close();
       }
       catch (Exception ex)
       {

           MessageBox.Show(ex.ToString());
       }







此特定行是问题:






This particular line was the issue:

string query = "SELECT DISTINCT Item_Description FROM dbo.Carimed";





原来它有一些东西与DISTINCT有关。我不完全确定为什么删除它并将其更改为解决了我的问题:





Turns out it has something to do with "DISTINCT." Am not entirely sure why removing it and changing it to this solved my issue:

string query = "SELECT Item_Description FROM dbo.Carimed";







我不知道我的查询会出现问题,这就是为什么我把它从我的问题中删除了。感谢所有帮助过的人。




I had no idea that my query would have been the issue, that's why I left it out from my question. Thanks for all who helped.


如果您想在用户离开之后验证组合,请挂钩LostFocus事件(或Winforms适当的焦点更改事件)。



当然,组合应该只填充有效项目,并且应该已经选择了合理的默认值。
If you want to validate the combo after the user leaves it, hook the LostFocus event (or whatever the appropriate focus changing event is for Winforms).

Of course, the combo should only be populated with "valid" items, and a reasonable default should already be selected.


这篇关于鼠标单击时触发事件,但方向键不触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆