C# winform在子窗体操作父窗体报错

yangchun1213 2014-05-23 05:18:58
1. 我有一个父窗体,名字是:ICCardManageMent,父窗体有个方法: public void RefeshStuData()在这个方法里面会用到父窗体的一些控件,如: pageIndex = Convert.ToInt32(studentBindingPage.PositionItem.Text);
// studentBindingPage.PositionItem.Text = pageIndex.ToString();
stuTchName = txtStuTchName.Text;
ICCardStatus = this.cmbICCardStatus.SelectedIndex + 1;
并且在父窗体有这么一段代码设置子窗体:GrantStuICCard stuForm = new GrantStuICCard(stuInfo);
stuForm.Owner = this;
2.有一个子窗体,名字为GrantStuICCard

我在子窗体调用父窗体的RefeshStuData方法时,会报异常,在子窗体是这样调用的:
ICCardManageMent parentForm = (ICCardManageMent)this.Owner;
parentForm.RefeshStuData();
this.Close();

异常是:线程间操作无效: 从不是创建控件“cmbICCardStatus”的线程访问它。
请问我要怎么解决呀,求大神指点呀
...全文
188 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunny906 2014-05-23
  • 打赏
  • 举报
回复
或者

public void RefeshStuData()
{
studentBindingPage.Invoke(new MethodInvoker( delegate() {  pageIndex = Convert.ToInt32(studentBindingPage.PositionItem.Text); }));
                // studentBindingPage.PositionItem.Text = pageIndex.ToString();
 stuTchName.Invoke(new MethodInvoker( delegate() { stuTchName = txtStuTchName.Text; }));
this.cmbICCardStatus.Invoke(new MethodInvoker( delegate() {  ICCardStatus = this.cmbICCardStatus.SelectedIndex + 1;}));
}
sunny906 2014-05-23
  • 打赏
  • 举报
回复
用事件

//父窗体代码
GrantStuICCard stuForm = new GrantStuICCard(stuInfo);
stuForm.RefeshStuData+=RefeshStuData;
                    stuForm.Owner = this;

//子窗体代码
public delegate void RefeshStuDataHandler();
public event RefeshStuDataHandler RefeshStuData;
//子窗体调用
if(RefeshStuData!=null)
{
RefeshStuData();
}

111,093

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧