使用委託實現窗體之間的傳值--事件訂閱實現傳值

下面我實現一個最簡單的頁面傳值功能,相信初學者能一看就明白。html

點擊打開按扭,打開傳輸值窗體ide

複製代碼
 1 public partial class Form1 : Form
 2     {
 3         public Form1()
 4         {
 5             InitializeComponent();
 6         }
 7 
 8         public void getValue(string strV)
 9         {
10             this.textBox1.Text = strV;
11         }
12 
13         private void button1_Click(object sender, EventArgs e)
14         {
15             Form2 frm = new Form2();
16             //frm.fatherform = this;//將當前窗體賦給fatherform
17             //frm.getTextHandler += new Form2.GetTextHandler(getValue);//給事件賦值(注意:GetText方法的參數必須與GetTextHandler委託的參數同樣,方可委託)
18             frm.getTextHandler = getValue;//將方法賦給委託對象
19             frm.ShowDialog();
20         }
21     }
複製代碼

 

輸入值後點擊傳輸按扭,'value'將顯示在接收值窗體的TextBox上post

複製代碼
 1 public partial class Form2 : Form
 2     {
 3         public Form2()
 4         {
 5             InitializeComponent();
 6         }
 7         //public Form1 fatherform;
 8 
 9         public delegate void GetTextHandler(string text);//聲明委託
10        // public event GetTextHandler getTextHandler = null;//定義委託事件
11        public  GetTextHandler getTextHandler;//委託對象
12         private void button1_Click(object sender, EventArgs e)
13         {
14             //if (fatherform != null)
15             //{
16             //    fatherform.getValue(this.textBox1.Text.Trim());
17             //    this.Close();
18             //}
19             if (getTextHandler != null)
20             {
21                 getTextHandler(this.textBox1.Text.Trim());
22                 this.Close();
23             }
24         }
25     }
複製代碼

這裏主要爲你們呈現了兩種傳值方式:this

1、將Form1窗體傳給fatherform對象,隨後咱們就能夠在Form2中操做Form1了。
2、使用委託,將getValue方法賦給事件或委託對象getTextHandler,那麼實現getValue操做就不用本身去作了由於已經委託給getTextHandler,直接調用getTextHandler便可spa

 

參考出處:http://www.cnblogs.com/pfcan66/archive/2012/09/12/2680596.html代理

===================================================================code

再來看幾個傳值的方法,有以下幾種方式,其中主要講講事件訂閱傳值的形式。orm

1. 聲明個全局變量,就是App.xaml裏面聲明;在全部窗體裏面均可以引用 Application.Current.Properties["ArgumentName"];htm

2. 第二個就是 在目標窗體上面公開個 屬性,直接賦值;對象

3. 最後就是在Uri裏面傳參數 NavigationService.Navigate(window object,argument value)

4. 採用事件響應,傳遞值。 

 

舉例:點擊主窗口MainWindow 上的一個OpenSubWindow按鈕 -> 打開子窗口SubWindow -> 在子窗口中的TextBox中輸入值, 點擊OK後關閉 -> 主窗口上的TextBox獲取子窗口中的值。

1. 在子窗口中定義一個事件PassValuesEvent。 當點擊 OK 按鈕時,觸發事件,並傳遞數值。( PassValuesEventArgs 是EventArgs類,須要同時定義好) 

複製代碼
    public partial class SubWindow : Window
    {
        public delegate void PassValuesHandler(object sender, PassValuesEventArgs e);

        public event PassValuesHandler PassValuesEvent; 

        public SubWindow()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            string value1 = tbValue1.Text;   // Text Property return value is string type .
            int value2;
            Int32.TryParse(tbValue2.Text, out value2);

            PassValuesEventArgs args = new PassValuesEventArgs(value1, value2);
            PassValuesEvent(this, args);

            this.Close();
        }
    }
複製代碼

2.  在主窗口中的OpenSubWindow按鈕點擊的方法中,訂閱了PassValuesEvent事件。當事件觸發時,獲取傳遞的參數的值。

複製代碼
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnOpenSubWindow_Click(object sender, RoutedEventArgs e)
        {
            SubWindow subWindow = new SubWindow(); 

            // 訂閱事件
            subWindow.PassValuesEvent += new SubWindow.PassValuesHandler(ReceiveValues);

            subWindow.Show(); 
        }

        private void ReceiveValues(object sender, PassValuesEventArgs e)
        {
            this.tbValue1.Text = e.Value1;
            this.tbValue2.Text = e.Value2.ToString(); 
        }
    }
複製代碼

 主要說明:子窗口的PassValuesEvent事件,是從PassValuesHandler代理的一個實例對象而且被定義成event類型,因此在其餘類裏就能夠訂閱這個事件了。

 

參考出處:http://www.cnblogs.com/fdyang/archive/2013/03/25/2980451.html

相關文章
相關標籤/搜索