假设,FormA接收到了串口发过来的数据,想让FormB刷新信息列表,如何实现?FormA和FormB都是FormMain的子Form要求,不可以让FormB进行循环刷新,FormB肯定会存在。

解决方案 »

  1.   

    大胆的回答一下:可否在FormA收到消息时同时发送信息给FormB,令其刷新!
    说错了还望见解!
      

  2.   

    在同一个进程里面可以考虑用单例模式写窗体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
    {}
    }
      

  3.   

    // 窗体B的“刷新方法”测试方法而已
    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;
    }
      

  4.   

    只要注意,创建窗体B的时候,把原来的 new FormB(); -->换成 FormB frm = FormB.CreateInstance;就可以了,这样可以在当前进程中,保证窗体B的实例,只有一个.
      

  5.   

    FormB.CreateInstance   ?
    没有这个方法吧!
      

  6.   

    先要确定 formA和formB是什么关系,1对多还是1对1
      

  7.   

    可以尝试以下方法(完全成功的, 我测试过了)用事件委托:
        把这个事件委托写到这个类中,定义一个发送数据的方法(SendMessage),其它的就是事件委托的代码了.
    在FormA中调用方法SendMessage.
    在FormB中加入定义的事件.就OK了.
      

  8.   

    用事件+委托
    或者 sendMessage
      

  9.   

    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
     }
    }
      

  10.   

    public string Data
     {
      set{
       // Refresh list
      }
     }
      

  11.   

    联动是什么?是不是比如点击form1,你要form2也执行点击的事件?那一句话不就可以了?
    假设你2个都要执行点击,希望1上点击后同时执行2的点击。那你可以这样打开新的2窗体。
    Form2 frm = new Form2();
    this.Click += new EventHandler(frm.Form2_Click);
    frm.Show()然后,打开form2,添加Form2_Click这个单击事件。修改访问级别为public。
    然后你试试 :)
      

  12.   

    在窗体2 中 frmB中定义一个属性
    public  string  EventB()
    {
    get{}
    set{
      // 在这里调用你自已要触发的方法}
    }
    在窗体1 frmA中给属性赋值,应该就会触发B中的事件
    public void cmd_onclick(event e )
    {frmB.EventB = "***" ;//这里就会自动调用了!
    }
      

  13.   

    在FormA中定义一个事件,FormB打开后就到到FormA的实例中注册该事件,关闭时注销该事件。FormA只需要在接收到数据时,当有事件观察者时调用该事件就行。
      

  14.   

    你可以在窗体A中声名局部窗体B的变量
    在窗体A中如果有B窗体所需的变量发生变化
    在是用B窗体
      

  15.   

    FormA和FormB是平级的,都是一个FormMain的子Form如果FormA发送了串口的一个字串,希望FormA能够通过事件通知FormB,让FormB进行刷新,或者处理其它信息。当主Form获取了串口返回的数据,希望能够通知FormA和FormB
      

  16.   

    faint~
    LZ,你真的去尝试过别人说的方法吗? 事件委托~不行,明天给你演示代码吧.
      

  17.   

    我也认为可以使用事件委托,但是我不会做事件委托看了其他人的方法,我感觉都不可以,你给我演示代码吧前阵子IE被杀毒软件修改了,都无法上CSDN了。
      

  18.   

    linuxyf(率人哥哥)和liuyu_net的方法比较好,事件加委托的例子-实现formA接收到数据时formB刷新:
    在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();
    基本上就可以了。
    另外,用消息也是不错的方法。
      

  19.   

    不好意思,上面错了一点,应该这样:
    在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();
    基本上就可以了。
    另外,用消息也是不错的方法。
      

  20.   

    算了 开邮箱也很累~先发几句牢骚: 下午去面试,就我去搞技术支持,技术搞习惯了,技术支持不习惯阿.. 
    郁闷阿..............正题开始了..//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;
    }
    }
    }
    }
      

  21.   

    //主窗口
    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());
    }
    }
    }
      

  22.   

    用事件+委托干净,不过要简单的话,可以这么做改写FormA的构造函数,把FormB的实例传给FormA就可以了
    Class FormA
    {
     private FormB _formB; public FormA(FormB formB)
    {
      _formB = formB
    }  formA得到数据库的时候,_formB.RefreshData();
    }class FormB
    {
      public void RefreshData()
    {
    ...
    }
    }
      

  23.   

    原理CSDN 不能连续发3次阿 晕了,, 邮件发了
      

  24.   

    谢谢Optione(找个工作太难了) ,给我发的完全满足要求