c# 在WinForm 窗体间传递信息

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

方法1
在Form1中通过PostMessage发送消息,在Form2通过DefWndProc截获消息来相互作用,具体参见我先前的文章《C# 收发和处理自定义的WINDOWS消息》


 

方法2
通过第三方 公共类的公共静态成员来实现

        class class1
        {
            public static string str;//公用字符串
        }

 在Form1中赋值class1.str="传入的内容"; ,在Form2中使用,string s=class1.str; 这样的方式可以传递任何的对象,比如数组等。


以下的内容来自网上,我更改其中的错误,并加以简化。

 

方法3

主窗体将自己的this引用,传递给子窗体的构造函数。代码如下:
 
主窗体.cs
        private void button1_Click(object sender, EventArgs e)
        {
            Form1 f1 = new Form1(this);
            f1.Show();
        }

子窗体.cs
   public partial class Form1 : Form
    {
        private frmMain mForm;
        public Form1(frmMain f)
        {
            this.mForm = f;
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            mForm.Text = "1 通过“指针”传递信息";
        }
    }


方法4

为了限制主窗体暴露给子窗体的功能,使用接口。代码如下:

定义接口.cs
    public interface IChangeTitle
    {
        void ChangeTitle(string title);
    }

主窗体.cs
       public void ChangeTitle(string title)
        {
            this.Text = title;
        } 
        private void button2_Click(object sender, EventArgs e)
        {
           Form2 f2 = new Form2(this);
            f2.Show();
        }

子窗体.cs
    public partial class Form2 : Form
    {
         private IChangeTitle ichangeTitle;
         public Form2(IChangeTitle ichangeTitle)
         {
             InitializeComponent();
              this.ichangeTitle=ichangeTitle;
         }
         private void button1_Click(object sender,EventArgs e)
         {
              // 通过接口来调用方法
              ichangeTitle.ChangeTitle("2 通过“接口”传递信息");
         }
    }

 

方法5 

为了进一步降低窗体之间的耦合度,利用委托。代码如下:

主窗体.cs
        protected void FormTitleChanged(string title)
        {
            this.Text = title;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Form3 f3 = new Form3();
            f3.TitleChanged = new Form3.TitleChangedHandler(FormTitleChanged);
            f3.Show();
        }

子窗体.cs
    public partial class Form3 : Form
    {
      // 声明和定义委托
        public delegate void TitleChangedHandler(string title);
        public TitleChangedHandler TitleChanged;
        public Form3()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
             if (TitleChanged != null)
                 TitleChanged("3 通过“委托代理”传递信息"); //委托调用
        }
   }

 

方法6

更好是,在子窗体中定义一个自定义事件及其事件参数。代码如下:

主窗体.cs
        protected void FormTitleChanged(object sender, Form4.TitleChangedEventArgs e)
        {
            this.Text = e.Title;
        }
        private void button4_Click(object sender, EventArgs e)
        {
            Form4 f4 = new Form4();
            f4.TitleChanged +=     new Form4.TitleChangedEventHandler(FormTitleChanged);
            f4.Show();
        }

子窗体.cs
    // 主窗体订阅这个事件就可以了
    public partial class Form4 : Form
    {
        public class TitleChangedEventArgs : EventArgs // 事件参数类
        {
            private string title = "";
            public string Title
            {
                get { return title; }
                set { title = value; }
            }
        }
        // 声明委托
        public delegate void TitleChangedEventHandler(object sender, TitleChangedEventArgs e);
        // 定义事件
        public event TitleChangedEventHandler TitleChanged;

        public Form4()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            TitleChangedEventArgs e1 = new TitleChangedEventArgs();
            e1.Title = "4 通过“触发事件”传递信息";
            OnTitleChanged(e1);// 触发事件

        }
        // 触发事件的方法
        protected virtual void OnTitleChanged(TitleChangedEventArgs e)
        {
            if (TitleChanged != null)
                TitleChanged(this, e);
        }

     }


转载于:https://my.oschina.net/chengkuan/blog/197784

你可能感兴趣的:(c# 在WinForm 窗体间传递信息)