1

I need to implement a ComboBox, which acts as follows:
When Click on the ComboBox, the client calling API method and updates the combobox items with the response.
My problem is, when I have 0 results - I want the ComboBox not to open (It has 0 items).
Is there a way to do that? This is my current code:L

private void Combo_DropDown(object sender, EventArgs e)
{
    // Private method which addes items to the combo, and returns false if no itmes were added
    if (!AddItemsToComboBox())
    {
        // This is not working
        Combo.DroppedDown = false;
    }
}
6
  • What's happening when your code runs? Can we see AddItemsToComboBox? And since it returns a bool you can just do Combo.DroppedDown = AddItemsToComboBox();
    – sab669
    Aug 26, 2015 at 11:49
  • Even if AddItemsToComboBox will do return false; DroppedDown = false don't stop to comboBox to open. And also sender.DroppedDown is false...
    – Shkolar
    Aug 26, 2015 at 11:57
  • The native Windows control does not support cancellation, the expectation is that you'll set the Enabled property to false when the combobox is useless. But you'll have to find a smarter way to populate it of course. Aug 26, 2015 at 11:59
  • How about just disabling it when there is no data?
    – danish
    Aug 26, 2015 at 12:06
  • Also why are you populating the combobox when they click on it? There's got to be a better time in the code to fill it. Then as @danish suggested you can just disable it then, before they try to open it.
    – sab669
    Aug 26, 2015 at 12:08

1 Answer 1

2

You can make the DropDownHeight as small as possible (1). For example:

  int iniHeight;
  private void Form1_Load(object sender, EventArgs e)
  {
        iniHeight = Combo.DropDownHeight;
  } 

  private void Combo_DropDown(object sender, EventArgs e)
  {
        Combo.DropDownHeight = (AddItemsToComboBox() ? iniHeight : 1);
  }
2
  • If I don't keep the iniHeight and just write "1000", it will do the max, no? (My limit is around 20 items...)
    – Shkolar
    Aug 26, 2015 at 12:34
  • @Shkolar This is just a sample code to transmit the idea; you can implement it in many different ways. The whole point of my answer is that by setting DrowDownHeight to 1 you can get what you are looking for (although the drowndown menu does not disappear completely, the user would think so).
    – user2480047
    Aug 26, 2015 at 12:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.