跨线程应用程序无效:控制'combobox1'是从不是在其上创建的线程的其他线程访问的 [英] cross thread application not valid : Control 'combobox1' accessed from a thread other than the thread it was created on

查看:64
本文介绍了跨线程应用程序无效:控制'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


这篇关于跨线程应用程序无效:控制'combobox1'是从不是在其上创建的线程的其他线程访问的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆