在同一个进程里面可以考虑用单例模式写窗体B的构造方法, 在窗体B中公开刷新信息的方法, 然后在窗体A的Closing事件中调用单例函数得到窗体B的实例,调用刷新方法.//窗体B的构造方式 private Form1() { // // Required for Windows Form Designer support // InitializeComponent();// // TODO: Add any constructor code after InitializeComponent call // }static public Form1 m_form = null; static public Form1 CreateInstance { get { if (m_form == null) { m_form = new Form1(); } return m_form; } set {} }
public Form FormA { private _RedirectForm = null; public FormB OutputRedirect { set{_RedirectForm = value;} } private OutPutData(string data) { _RedirectForm.Data = data; } }public Form FormB { public string Data { // Refresh list } }
public string Data { set{ // Refresh list } }
联动是什么?是不是比如点击form1,你要form2也执行点击的事件?那一句话不就可以了? 假设你2个都要执行点击,希望1上点击后同时执行2的点击。那你可以这样打开新的2窗体。 Form2 frm = new Form2(); this.Click += new EventHandler(frm.Form2_Click); frm.Show()然后,打开form2,添加Form2_Click这个单击事件。修改访问级别为public。 然后你试试 :)
在窗体2 中 frmB中定义一个属性 public string EventB() { get{} set{ // 在这里调用你自已要触发的方法} } 在窗体1 frmA中给属性赋值,应该就会触发B中的事件 public void cmd_onclick(event e ) {frmB.EventB = "***" ;//这里就会自动调用了! }
说错了还望见解!
在窗体B中公开刷新信息的方法,
然后在窗体A的Closing事件中调用单例函数得到窗体B的实例,调用刷新方法.//窗体B的构造方式
private Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();//
// TODO: Add any constructor code after InitializeComponent call
//
}static public Form1 m_form = null;
static public Form1 CreateInstance
{
get
{
if (m_form == null)
{
m_form = new Form1();
}
return m_form;
}
set
{}
}
internal void SetText(string txt)
{
this.button1.Text = txt;
}// 窗体A的退出事件 // 在本问题中,这个事件就可以换成你接收到串口数据的事件
private void Form2_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Form1 frm = Form1.CreateInstance;
frm.SetText("ddddd");
e.Cancel = false;
}
没有这个方法吧!
把这个事件委托写到这个类中,定义一个发送数据的方法(SendMessage),其它的就是事件委托的代码了.
在FormA中调用方法SendMessage.
在FormB中加入定义的事件.就OK了.
或者 sendMessage
{
private _RedirectForm = null;
public FormB OutputRedirect
{
set{_RedirectForm = value;}
} private OutPutData(string data)
{
_RedirectForm.Data = data;
}
}public Form FormB
{
public string Data
{
// Refresh list
}
}
{
set{
// Refresh list
}
}
假设你2个都要执行点击,希望1上点击后同时执行2的点击。那你可以这样打开新的2窗体。
Form2 frm = new Form2();
this.Click += new EventHandler(frm.Form2_Click);
frm.Show()然后,打开form2,添加Form2_Click这个单击事件。修改访问级别为public。
然后你试试 :)
public string EventB()
{
get{}
set{
// 在这里调用你自已要触发的方法}
}
在窗体1 frmA中给属性赋值,应该就会触发B中的事件
public void cmd_onclick(event e )
{frmB.EventB = "***" ;//这里就会自动调用了!
}
在窗体A中如果有B窗体所需的变量发生变化
在是用B窗体
LZ,你真的去尝试过别人说的方法吗? 事件委托~不行,明天给你演示代码吧.
在formA中这样:
public class formA : System.Windows.Forms.Form
{
public delegate void test(参数);
public event test ReFreshB;
}
假设接收到数据的事件为ReceivedData()
在事件里面这样:
ReceivedData()
{
ReFreshB(参数);
}
在formB里面写对应的刷新显示的方法:
private void aaa(参数)
{……}
注意“参数”要相同;
在打开formA 的时候这样:
formA frm=new formA();
frm.ReFreshB+=new 你的命名空间.formA.test(参数);
frm.show();
基本上就可以了。
另外,用消息也是不错的方法。
在formA中这样:
public class formA : System.Windows.Forms.Form
{
public delegate void test(参数);
public event test ReFreshB;
}
假设接收到数据的事件为ReceivedData()
在事件里面这样:
ReceivedData()
{
ReFreshB(参数);
}
在formB里面写对应的刷新显示的方法:
private void aaa(参数)
{……}
注意“参数”要相同;
在打开formA 的时候这样:
formA frm=new formA();
frm.ReFreshB+=new 你的命名空间.formA.test(this.aaa);
frm.show();
基本上就可以了。
另外,用消息也是不错的方法。
郁闷阿..............正题开始了..//Notify.cs 关键的类,using System;namespace System.Windows.Forms
{
/// <summary>
/// Notify 的摘要说明。
/// </summary>
public class Notify
{
protected static Notify notify = null;
public delegate void NotifyEvent(object sender, NotifyEventArgs e);
public event NotifyEvent OnNotifyEvent;
protected Notify(){}
public static Notify Create()
{
if (notify == null)
notify = new Notify();
return notify;
}
public void SendMessage(int code, string message)
{
NotifyEventArgs e = new NotifyEventArgs(code, message);
if ( OnNotifyEvent != null)
OnNotifyEvent(this, e);
}//可以对消息进行扩充.
}
public class NotifyEventArgs : System.EventArgs
{
protected string _strMessage = string.Empty;
protected int _nCode;
public NotifyEventArgs(int code, string message)
{
_nCode = code;
_strMessage = message;
}
public int Code
{
get { return _nCode;}
set { _nCode = value;}
}
public string Message
{
get { return _strMessage;}
set
{
_strMessage = value;
if (_strMessage == null)
_strMessage = string.Empty;
}
}
}
}
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace 子窗口间通信
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
FormA a =new FormA();
a.Owner = this;
a.Show();
FormB b = new FormB();
b.Owner = this;
b.Show();
//我这里0代表全部显示
Notify.Create().SendMessage(0,"来自主Form的广播"); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(272, 269);
this.Name = "Form1";
this.Text = "Form1"; }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}
Class FormA
{
private FormB _formB; public FormA(FormB formB)
{
_formB = formB
} formA得到数据库的时候,_formB.RefreshData();
}class FormB
{
public void RefreshData()
{
...
}
}