Alon123
Alon123

Reputation: 174

Detect when ComboBox is clicked on Winforms

I'm am trying to perform an action when my combobox has been clicked (not changed index). Basically I want to grab some items from a directory every time user clicks on the combobox, but for some reason it doesn't seem to identify a click.. I got two functions but none of them fire away when I click:

private void cmblist_MouseClick(Object sender, MouseEventArgs e)
{
  //do something
}

void cmblist_Click(object sender, MouseEventArgs e)
{
   //do something
}

I assume that the body of the functions are not so important as they won't even fire.. How can I perform an action when the combobox is clicked?

Upvotes: 0

Views: 1112

Answers (2)

user15546827
user15546827

Reputation:

I suppose, you didn't add this method to your ComboBox.MouseClick event.

If you do this dynamically (like all OOP programmers) you can assign method this way:

        private void InitializeComboBox()
        {
            this.Controls.Add(cmbList);
            cmbList.MouseClick += CmbList_MouseClick;
        }

        private void CmbList_MouseClick(object sender, MouseEventArgs e)
        {
            //do something...
        }

Other way, if you do this via WindowsForm Designer (where you add everything manually), you should click once on your ComboBox and go to Properties -> Events -> MouseClick and from drop-down menu choose your method that you already have.

Upvotes: 1

Kundan Kumar
Kundan Kumar

Reputation: 1

Try OnMouseEnter or OnKeyPress method. I hope this this is what you're searching for. You can find more details here: https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.combobox?view=net-5.0

Upvotes: 0

Related Questions