c# 在WinForm 窗體間傳遞信息

方法1
在Form1中經過PostMessage發送消息,在Form2經過DefWndProc截獲消息來相互做用,具體參見我先前的文章《C# 收發和處理自定義的WINDOWS消息》html


 

方法2
經過第三方 公共類的公共靜態成員來實現數組

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

 在Form1中賦值class1.str="傳入的內容"; ,在Form2中使用,string s=class1.str; 這樣的方式能夠傳遞任何的對象,好比數組等。this


如下的內容來自網上,我更改其中的錯誤,並加以簡化。spa

 

方法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 經過「指針」傳遞信息";
        }
    }orm


方法4htm

爲了限制主窗體暴露給子窗體的功能,使用接口。代碼以下:對象

定義接口.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);
        }

     }

相關文章
相關標籤/搜索