Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read combobox from a thread other than the thread it was created on?

I am trying to read a combobox.Text from a thread other than the thread it was created on but I am getting the error:

An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll

Additional information: Cross-thread operation not valid: Control 'levelsComboBox' accessed from a thread other than the thread it was created on.

I have used .Invoke before but only to set properties, how can I use it to read combobox.Text? Because .Invoke returns void and I need a string. Or is there another way to do it without the Invoke?

like image 934
nitrkli Avatar asked Apr 01 '11 16:04

nitrkli


People also ask

Is the default event of ComboBox control?

By default, DropDownStyle property of a Combobox is DropDown. In this case user can enter values to combobox. When you change the DropDownStyle property to DropDownList, the Combobox will become read only and user can not enter values to combobox.

How do I remove items from ComboBox?

To remove an itemCall the Remove or RemoveAt method to delete items. Remove has one argument that specifies the item to remove. RemoveAt removes the item with the specified index number.


1 Answers

You can do it like this:

this.Invoke((MethodInvoker)delegate()     {         text = combobox.Text;     }); 
like image 174
BrandonZeider Avatar answered Sep 18 '22 07:09

BrandonZeider