Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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!");
          }
      }
Posted
Updated 23-Dec-16 4:16am
Comments
Michael_Davies 20-Dec-16 10:33am    
Is it win forms?

Bit puzzled as :

The direction keys do not normally leave a control so would not raise the leave event, the Tab key moves between controls and fires the leave event.

Clicking an item in a combobox does not leave the control unless you do it in the click or selectionchanged event(s).
Member 12896210 20-Dec-16 10:36am    
Yes it is winforms. If I try to select the value from the list the Leave Event is raised. When I close it I can select though. It seems as if each time I try to select the value in the list, the Leave Event pops up. Selection is only accepted(that is Leave Event doesn't come up) when I close the Leave Event and try selecting again
Michael_Davies 20-Dec-16 11:23am    
And you are not trapping the selectionchanged or any other event?

Just added a leave event and tested one of my forms combobox and it behaves as expected.

Why not use selectionchanged as it fires when the user changes the selection (caveat: also fires for each item when you add them to the control)
Member 12896210 20-Dec-16 11:24am    
No am not
Michael_Davies 20-Dec-16 11:43am    
Your also testing whether the combobox contains the text of the currently selected item of the combobox which is obviously true but returns false in my test.

Just tried it myself and it pops up the messagebox even though it does contain the text that might be where your problem lies.

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:


C#
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:

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


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

C#
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.
 
Share this answer
 
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.
 
Share this answer
 
v2
Comments
Member 12896210 20-Dec-16 12:27pm    
I have a custom suggestComboBox .cs file and am wondering if that is causing the issue. currently it handles Keys.Down, Keys.Up, Keys.Enter, Keys.Escape am wondering if not handling the mouse click could be the problem.
Michael_Davies 20-Dec-16 16:24pm    
When I asked if you were trapping any other event you said "No am not".
Member 12896210 20-Dec-16 16:25pm    
Guess I misunderstood

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900