我试图从另一个线程中的列表框项读取值。
我尝试编写一个新的方法来运行调用命令,我可以通过调用方法向列表框发送add等命令,但是我似乎无法获得响应,也无法获取该项的值,我尝试了几种方法,一旦将其从void更改为string,事情就开始变得棘手起来...
thread t1 = new thread(thethread)
t1.start()
public void thethread()
{
string text = readListBoxSelected(listBox1) + " lala" ;
}
public static string readListBoxSelected(ListBox listbox)
{
if (listbox.InvokeRequired)
{
return (string)listbox.Invoke(
new Func<String>(() => readListBoxSelected(listbox))
);
}
else
{
string varText = listbox.SelectedValue.ToString();
return varText;
}
}
以下是我尝试做的示例。 以下是错误信息:
System.NullReferenceException未被用户代码处理 消息=未将对象引用设置到对象的实例。 源=** StackTrace: 在***。Form1.readListBoxSelected(ListBox listbox)中 e:\ documents和settings \ scott \ my documents \ visual studio 2010 \ Projects ** * * ** \ Form1.cs:第133行 在***。Form1.<> c_<DisplayClass5.b> _3() e:\ documents和settings \ scott \ my documents \ visual studio 2010 \ Projects ** * * ** \ Form1.cs:第127行InnerException:
我想,问题就是如其所述“未将对象引用设置为对象的实例”……据我所知,我的所有变量都已声明完毕,我应该怎样纠正这个问题??
我有种感觉整个事情都错了....0_o 提前致谢, Scott