Reputation: 201
Hi I want to use my code below for a thread. I have some example code for invoke but I dont know how to do it when it comes to my combo box selected item going to string.
This is what I have:
//My code
string cb1 = comboBox1.Items[comboBox1.SelectedIndex].ToString();
//Example 1
textBox2.Invoke((Action)(() => textBox2.Text = ""));
//Example 2
textbox2.Invoke((MethodInvoker)(delegate()
{
//do something
}));
Upvotes: 1
Views: 1512
Reputation: 148150
string newValue = "hi there";
if (textBox.InvokeRequired)
textBox.Invoke((MethodInvoker)delegate { textBox.Text = newValue; });
else
textBox.Text = newValue;
For particular code asked in question we can do it like
MethodInvoker mi = delegate
{
string cb1 = comboBox1.Items[comboBox1.SelectedIndex].ToString();
};
if (InvokeRequired)
this.BeginInvoke(mi);
else
mi.Invoke();
Upvotes: 2
Reputation: 22171
Try this one if you want to go with Example 1
(using a Func<string>
delegate instead of an Action
delegate):
string cb1 = comboBox1.Invoke((Func<string>) (() => comboBox1.Items[comboBox1.SelectedIndex].ToString())) as string;
Upvotes: 5