形式之间的沟通 [英] Communicating between forms

查看:107
本文介绍了形式之间的沟通的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用C#形式之间的沟通?

I would like to know how to communicate between forms using C#?

在Visual Basic中这是比较容易,因为每个形式有它自己的实例,用C#这是不同的。

In Visual Basic it was easier since each form had it's own instance, with C# it's different.

程序 I级设置的握住我的形式公开和静态变量:

In the Program Class I setup a public and static variable that hold my form:

public static FormProcess2 frmProcess2 = null;

然后在方法创建表格,显示它:

Then in the Main method I create the form and show it up:

frmProcess2 = new FormProcess2();
frmProcess2.ShowDialog();

现在,当这种形式加载时,它加载了处理XML文件的后台进程。
并且处理所有文件的方法,在另一个类的名称 XMLParser的
所以在的BackgroundWorker 的doWork 我通过加载方法:

Now when that form loads, it load a background process that process XML files. And the method that process all the files are in another class name XMLParser. So in the BackgroundWorker doWork I load that method by:

XMLParser.Start();

现在的问题是,我怎么能修改 XMLParser的类的<​​code> frmProcess2 控件?
我想它来显示什么正在改变标签文本以及放一些漂亮的进度条有更新。

Now the question is, how I can modify the frmProcess2 controls in the XMLParser class? I wanted it to show what's being updated by changing the label text as well as put some nice progress bar there.

但我不能访问 Program.frmProcess2 控制,我只能访问是它的默认值只有...

But I can't access the Program.frmProcess2 controls, all I can access is it's defaults only...

推荐答案

以及可以使用委托Windows窗体之间的通信。
检查<一href=\"http://www.c-sharpcorner.com/uploadfile/mosessaur/winformsdelegates09042006094826am/winformsdelegates.aspx\" rel=\"nofollow\">http://www.c-sharpcorner.com/uploadfile/mosessaur/winformsdelegates09042006094826am/winformsdelegates.aspx

well you can use delegates to communication between windows forms. Check http://www.c-sharpcorner.com/uploadfile/mosessaur/winformsdelegates09042006094826am/winformsdelegates.aspx

有关代表

修改

EDIT

检查
代表(C#编程指南)

http://msdn.microsoft.com/ EN-US /库/ ms173171%28VS.80%29.aspx

创建自定义委托和事件在C#

HTTP://www.csharphelp .COM / 2007/02 /创建定制,委托和事件 - 在C /

介绍委托和事件

http://www.csharp-station.com/Tutorials/lesson14.aspx

这篇关于形式之间的沟通的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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