C#利用委託實現窗體間的值傳遞 .

A、網上有不少方法,你們可搜一下,均可用。ide

B、這裏只是說明下是隻利用委託,學習基本的委託運用。函數

方法以下:學習

一、C#創建一個默認工程,默認窗體Form1this

二、加入一個新窗體,默認Form2spa

三、Form1窗體上放一個Label,一個Button,屬性分別爲:.net

this.label1.Text = ""; // 清空label的值設計

this.button1.Text="otherForm"; //設置button顯示爲otherFormcode

 

[c-sharp] view plain copy print ?
  1. protected override void OnLoad(EventArgs e)  
  2. {  
  3.     base.OnLoad(e);  
  4.     this.label1.Text = "";  
  5.     this.button1.Text = "otherForm";  
  6. }  
protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.label1.Text = "";
            this.button1.Text = "otherForm";
        }

四、Form2窗體上放一個textbox1,一個Button,屬性分別爲:orm

this.textbox1.Text = ""; //清空textbox的值blog

this.button1.Text = "input"; //設置button顯示爲input(輸入)

[c-sharp] view plain copy print ?
  1. protected override void OnLoad(EventArgs e)  
  2. {  
  3.     base.OnLoad(e);  
  4.     this.textBox1.Text = "";  
  5.     this.button1.Text = "input";  
  6. }  
protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.textBox1.Text = "";
            this.button1.Text = "input";
        }

五、上面的步驟就創建了兩個基本的窗體,接下來回到Form1的代碼設計視圖

拷貝下面代碼

 

[c-sharp] view plain copy print ?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9.   
  10. namespace WindowsFormsApplication1  
  11. {  
  12.     public partial class Form1 : Form  
  13.     {  
  14.         public Form1()  
  15.         {  
  16.             InitializeComponent();  
  17.         }  
  18.   
  19.         protected override void OnLoad(EventArgs e)  
  20.         {  
  21.             base.OnLoad(e);  
  22.             this.label1.Text = "";  
  23.             this.button1.Text = "otherForm";  
  24.         }  
  25.   
  26.         private void button1_Click(object sender, EventArgs e)  
  27.         {  
  28.             Form2 frm2 = new Form2();  
  29.             returnValueEvent += new returnValue(Form1_returnValueEvent);  
  30.             frm2.Show();  
  31.         }  
  32.   
  33.         void Form1_returnValueEvent(string str)  
  34.         {  
  35.             // throw new NotImplementedException();   
  36.             this.label1.Text = str;  
  37.         }  
  38.   
  39.         // 定義委託   
  40.         public delegate void returnValue(string str);  
  41.         // 委託的事件   
  42.         public static event returnValue returnValueEvent;  
  43.         // 委託的事件的函數   
  44.         public void doReturnValue(string str)  
  45.         {  
  46.             returnValueEvent(str);  
  47.         }  
  48.     }  
  49. }  
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.label1.Text = "";
            this.button1.Text = "otherForm";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2();
            returnValueEvent += new returnValue(Form1_returnValueEvent);
            frm2.Show();
        }

        void Form1_returnValueEvent(string str)
        {
            // throw new NotImplementedException();
            this.label1.Text = str;
        }

        // 定義委託
        public delegate void returnValue(string str);
        // 委託的事件
        public static event returnValue returnValueEvent;
        // 委託的事件的函數
        public void doReturnValue(string str)
        {
            returnValueEvent(str);
        }
    }
}

六、在Form2的代碼設計視圖中,拷貝下面代碼

[c-sharp] view plain copy print ?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9.   
  10. namespace WindowsFormsApplication1  
  11. {  
  12.     public partial class Form2 : Form  
  13.     {  
  14.         public Form2()  
  15.         {  
  16.             InitializeComponent();  
  17.         }  
  18.   
  19.         protected override void OnLoad(EventArgs e)  
  20.         {  
  21.             base.OnLoad(e);  
  22.             this.textBox1.Text = "";  
  23.             this.button1.Text = "input";  
  24.         }  
  25.   
  26.         private void button1_Click(object sender, EventArgs e)  
  27.         {  
  28.             Form1 frm1 = new Form1();  
  29.             frm1.doReturnValue(this.textBox1.Text);  
  30.         }         
  31.     }  
  32. }  
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.textBox1.Text = "";
            this.button1.Text = "input";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form1 frm1 = new Form1();
            frm1.doReturnValue(this.textBox1.Text);
        }       
    }
}

七、運行後,就能夠在兩個窗體間傳遞參數了

 

注:重點是第六步中的,對委託的定義和調用

 

        returnValueEvent += new returnValue(Form1_returnValueEvent);

        void Form1_returnValueEvent(string str)
        {
            // throw new NotImplementedException();
            this.label1.Text = str;
        }

        // 定義委託
        public delegate void returnValue(string str);
        // 委託的事件
        public static event returnValue returnValueEvent;
        // 委託的事件的函數
        public void doReturnValue(string str)
        {
            returnValueEvent(str);
        }

 在Form2中的調用,在按鈕的click事件中

            Form1 frm1 = new Form1();
            frm1.doReturnValue(this.textBox1.Text);

運行效果圖以下:

效果圖

相關文章
相關標籤/搜索