I am trying to populate a combobox in C#, but for some reason, the items do not appear.
public List<string> items
{
set
{
combobox.Items.Clear();
foreach(string s in value)
{
combobox.Items.Add(s);
}
combobox.Update();
}
}
This seems like incredibly straigh开发者_StackOverflowtforward code. I simply cannot see what is wrong. It is being called like this:
private void StoreNames(List<string> names)
{
if (selectionForm.InvokeRequired)
selectionForm.Invoke((MethodInvoker)delegate { selectionForm.items = names; });
else
selectionForm.items = names;
}
Interestingly, it seems to work when InvokeRequired
returns true, but does not work when it returns false.
EDIT:
I discovered that selectionForm.IsHandleCreated
is currently false
. This is causing InvokeRequired
to return false, but is also why calling the setter regularly isn't working. I don't have any idea why IsHandleCreated
is set to false. The Form has been Show()n.
Not sure why your code isn't working - I tried it and it works just fine.
However, below is some more straightforward code which also works - you may find that doing it this way instead makes your problem go away. This does presume that there is not other reason why you need to go through that property - that is quite an unusual way of doing things.
public void StoreNames(List<string> input)
{
if (comboBox1.InvokeRequired)
comboBox1.Invoke((MethodInvoker)delegate {
StoreNames(input);
});
else
{
comboBox1.Items.Clear();
comboBox1.Items.AddRange(input.ToArray());
}
}
Here we just pass the list straight to the items.AddRange() method on the comboBox.
I suspect this won't work for you - something else is going on, but I have tried it both from a backgroundworker (where InvokeRequired is true) and from the main UI thread.
Is it a typo that you refer to both combobox and combobox1? Perhaps that is your error.
Do you mean "not appear" as in you can see them when the list displays, or as in you can't even scroll to them?
Combobox has a lot of properties that can affect what you see. Try a bigger value for combobox.MaxDropDownItems.
My other answer in this thread is really just showing the way to doing this if you can get at the code providing you with the list. Since it sounds like you cannot, I'm providing this answer.
It sounds like the key problem is that when the property is called the combobox has not yet been initialized. The best answer for that is that make sure that this does not happen within the calling code.
If you can't do that then you should wait for the property to be set before you use it to populate the combobox.
I would do this be having a private list which gets set in the property setter. Within the form load event i would then put some code (possibly within a background worker) that will wait until the private list is not null and then assign it to the combobox.
Have a look at this post by Jon Skeet here where he discusses how to correctly wait for a variable to be set by another thread. You will want to be careful when doing this - threading issues can be nasty.
精彩评论