1

嗨,我想将下面的代码用于线程。我有一些用于调用的示例代码,但是当涉及到我的组合框选定项将变为字符串时,我不知道该怎么做。

这就是我所拥有的:

//My code
string cb1 = comboBox1.Items[comboBox1.SelectedIndex].ToString();

//Example 1
textBox2.Invoke((Action)(() => textBox2.Text = ""));

//Example 2
textbox2.Invoke((MethodInvoker)(delegate()
{
    //do something
}));
4

2 回答 2

5

如果你想Example 1使用这个(使用Func<string>委托而不是Action委托),请尝试这个:

string cb1 = comboBox1.Invoke((Func<string>) (() => comboBox1.Items[comboBox1.SelectedIndex].ToString())) as string;
于 2012-04-17T17:38:49.040 回答
2
string newValue = "hi there";

if (textBox.InvokeRequired)
    textBox.Invoke((MethodInvoker)delegate { textBox.Text = newValue; });
else
    textBox.Text = newValue;

对于有问题的特定代码,我们可以这样做

MethodInvoker mi = delegate
{
     string cb1 = comboBox1.Items[comboBox1.SelectedIndex].ToString();
};
if (InvokeRequired)
   this.BeginInvoke(mi);
else
   mi.Invoke();
于 2012-04-17T16:49:17.227 回答