How do you get the combobox selected item in a separate thread in C#?

How do you get the combobox's selected item from a separate thread in C#?

Thanks in anticipation.
LVL 2
ArticAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nffvrxqgrcfqvvcCommented:
You can create a delegate function that returns the property value and in your thread function use the Invoke keyword on the control. However it's unclear when your thread begins either during the selection or the selection occurs before your thread starts. If it occurs before the thread starts you can pass the value or object into the thread parameter instead.
public void MyThread(object param)
{
	string value = string.Empty;
	if (this.ComboBox1.InvokeRequired) {
		GetComboSelectedTextDelegate d = new GetComboSelectedTextDelegate(GetComboSelectedText);
		value = this.ComboBox1.Invoke(d);
	} else {
		value = this.ComboBox1.SelectedItem;
	}

	Console.WriteLine(value);
}

private delegate string GetComboSelectedTextDelegate();
private string GetComboSelectedText()
{
	return this.ComboBox1.SelectedItem;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AndyAinscowFreelance programmer / ConsultantCommented:
Because you specifically have multiple threads then you need to be certain the value isn't being changed whilst the thread is running (and wanting to get the value).

Have a think about your logic - maybe you need to store the value of the combobox and pass it to the thread rather than the thread requesting the value.
0
ArticAuthor Commented:
Is it possible to pass in the name of the comobox so that the method can be applied to different comboboxes?

Thanks in anticipation.
0
apeterCommented:
A thread can't access UI Controls since they below to the main UI thread. Only way i think is you have to pass the value of the combox box as parameter to the thread which you are starting.

But threads can access public property. Try setting the value of the combo box value to public property and the thready reading it.

Look also BackgroundWorker class if it is for a windows application.
0
ArticAuthor Commented:
Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.