Mouse Cursor moves to beginning of combo box string

VAer-4038 771 Reputation points
2021-01-03T04:04:42.347+00:00

I am trying to explain the issue as clearly as I can.

It is comboBoxABC, I wrote below code for the combo box. I can select an item from the dropdown list, or I can enter my own value, such as Hello. If I am in the process of entering string, Cursor shows at the end of string (the one on the left of screenshot).

No matter after I select an item or after finish entering my own value, if I use mouse to click somewhere inside combo box at the moment, the cursor cannot stay where I click, and Cursor jumps to the beginning of string (the one on the right of screenshot), after jumping to the beginning of string (before H, H is the first character of Hello), it flashes for a few times, maybe 5-6 times, then stop flashing. From there, if I want to delete the string Hello, I have to use Delete key, not backspace key, since the Cursor is at the beginning of string.

I use same code (below code) to check textbox value, if I click somewhere inside textbox, the Cursor will not jump to beginning of string, it can stay where I click.

It seems it only happens to combo box, why? How to fix it?

Thanks.

52873-mouse-arrow.jpg

        private void comboBoxABC_KeyPress(object sender, KeyPressEventArgs e)  
        {  
            char ch = e.KeyChar;  
   
            if (!char.IsLetter(ch) && ch != 8)    
            {  
                e.Handled = true;  
                MessageBox.Show("Only letter is allowed.");  
            }  
        }  
  

Edit: Due to 1000 character limit on comment, I added code to this original post. After removing comboBoxABC_KeyPress , the problem remains, so probably it is comboBoxCountry_Click (it is same combo box as comboBoxABC in this question, I use different name on different posts) causing the problem, only two events are associated with this comboBox. The code is also in another post ( c-error-too-few-parameters-combo-box-based-on-data.html ), as mentioned in below comment.

  private void comboBoxCountry_Click(object sender, EventArgs e)  
         {  
             comboBoxCountry.Items.Clear();  
      
             OdbcConnection Cn = new OdbcConnection(GlobalVariables.DatabaseConnectionString);  
             Cn.Open();  
      
             string qCountry = "SELECT DISTINCT Country from Place";  
      
             using (var cmd = new OdbcCommand(qCountry, Cn))  
             {  
                 using (var reader = cmd.ExecuteReader())  
                 {  
                     while (reader.Read())  
                     {  
                         string sColString = reader.GetString(0);  
                         comboBoxCountry.Items.Add(sColString);  
                     }  
                 }  
             }  
      
             Cn.Close();  
      
      
         }  
Developer technologies | Visual Studio | Other
{count} votes

1 answer

Sort by: Most helpful
  1. Dylan Zhu-MSFT 6,426 Reputation points
    2021-01-04T06:56:16.087+00:00

    Hi VAer,

    >After removing comboBoxABC_KeyPress , the problem remains, so probably it is comboBoxCountry_Click (it is same combo box as comboBoxABC in this question, I use different name on different posts) causing the problem, only two events are associated with this comboBox.

    Yes, the comboBoxCountry_Click event will be triggered when you click the combo box every time. I suggest you can create a new button to update your combo box or create a background thread to update combo box at specified interval.

    Best Regards, Dylan

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our * *documentation* to enable e-mail notifications if you want to receive the related email notification for this thread.**

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.