2

So I have a set up like the answer for this post: display list of custom objects as a drop-down in the PropertiesGrid , where I have a property tree and I need one of them to have a dropdown which I fill from a string array.

Im using IWindowsFormsEditorService to have a drop down. I create the list box:

 IWindowsFormsEditorService service = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
ListBox lb = new ListBox();
lb.SelectionMode = SelectionMode.One;
lb.SelectedIndexChanged += OnListBoxSelectedValueChanged;
lb.Items.AddRange(myStringArray)
service.DropDownControl(lb);

The OnListBoxSelectedValueChanged event I have:

service.CloseDropDown();

However the dropdown doesn' close and I dont know why. It works fine until that point.

From other code examples I have found, it should be working. If I click elsewhere on the property tree it does close, but why doesn't it close on SelectedIndexChanged? Is there something with the listBox control that is overriding that? Other then what is above, I have no other listBox properties set.

meh93
  • 311
  • 4
  • 13
  • The `service` variable which you are using is local to `EditValue` method. It's different instance from what you are using in `OnListBoxSelectedValueChanged`. – Reza Aghaei Feb 15 '18 at 17:49

2 Answers2

1

The service variable which you are using is local to EditValue method. It's different instance from what you are using in OnListBoxSelectedValueChanged.

You can simply replace the event handler this way:

var service = (IWindowsFormsEditorService)provider
    .GetService(typeof(IWindowsFormsEditorService));
var lb = new ListBox();
lb.SelectionMode = SelectionMode.One;
lb.SelectedIndexChanged += (obj, args) => { service.CloseDropDown(); };
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
0

What is wrong is that,

private IWindowsFormsEditorService service;

is declared at the beginning of the class, as it is in the example display list of custom objects as a drop-down in the PropertiesGrid . However I had

IWindowsFormsEditorService service = (IWindowsFormsEditorService)provider.GetService...

So it was creating a different instance. The event handler was referencing the service on the top, but the listBox was set with the 2 service.

meh93
  • 311
  • 4
  • 13