跨线程应用程序无效:控制'combobox1'是从不是在其上创建的线程的其他线程访问的

问题描述:

大家好,
将组合框的所选项目的值传递给字符串时,出现上述错误.如何调用它以及如何使用invoke传递选定的项目.请给我一个解决方案.我正在使用Backgroundworker.
私有void selecteditem()
{
dgalaxy =(string)combobox1.SelectedItem;//在这一行中,我遇到了错误.
.
.
.
.
.
}

Hi all,
I am getting the above error when i am passing the value of combobox''s selected item to a string. How to invoke it and how to pass the selected item using invoke. Plz provide me a solution to it.I am using Backgroundworker.
private void selecteditem()
{
dgalaxy = (string)combobox1.SelectedItem;// in this line i am getting error.
.
.
.
.
.
}

根据文档,它应该可以通过键入以下内容来起作用:
combobox1.SelectedItem.ToString

http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selecteditem.aspx [ ^ ]
According to the documentation it should work by typing:
combobox1.SelectedItem.ToString

http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selecteditem.aspx[^]


private string GetSelecetedItem()
       {
           if (cmbGalaxy.InvokeRequired)
               return (string)cmbGalaxy.Invoke(new Func<string>(GetSelecetedItem));
           else
               return cmbGalaxy.SelectedItem.ToString();
       }




我使用上述方法来调用组合框,并在selecteditem方法内部对其进行了调用.




i used the above method to invoke the combobox and called it inside selecteditem method.


它可以被另一个线程访问...

如果没有访问一个控件,则会出现此错误..

了解更多详情

访问此链接

http://www.vbdotnetforums.com/windows-forms/50272-cross-thread- operation.html [ ^ ]

问候
sarva
Its accessed by Another Thread...

Access that control one by one if not you will get this error..

for further details

visit this link

http://www.vbdotnetforums.com/windows-forms/50272-cross-thread-operation.html[^]

regards
sarva