WinForm中的窗体传值的多种方法

WinForm中的窗体传值有多种方法,自己结合相关资料总结了一下,大概有5种方式(或者更多):
1、通过 ShowDialog()进行传值;
2、通过改造构造函数进行传值(加参数);
3、通过公共静态类进行传值;
4、通过绑定事件进行传值;
5、使用Attribute(本人属初学,尚需深入研究,希望高手给与指正)
代码如下:

主窗体代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WinTestValue
   {
    public partial class Main : Form
       {
        public Main()
           {
            InitializeComponent();
            F004_ComonClass fc = new F004_ComonClass();
            fc.Show();
        }
        方法1:通过ShowDialog进行页面传值#region 方法1:通过ShowDialog进行页面传值
        private void btn_ok_Click(object sender, EventArgs e)
           {
            F001_ShowDialog fs = new F001_ShowDialog();
            if (fs.ShowDialog() == DialogResult.OK)
               {
                this.tbx_value.Text = F001_ShowDialog.returnValue;
                MessageBox.Show("从F002_ShowDialog传值到Main窗体成功!", "Success",

MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btn_ok1_Click(object sender, EventArgs e)
           {
            F001_ShowDialog fs = new F001_ShowDialog();
            if (fs.ShowDialog() == DialogResult.OK)
               {
                this.tbx_value.Text = fs.ReturnValue();
                MessageBox.Show("从F002_ShowDialog传值到Main窗体成功!", "Success1",

MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btn_ok2_Click(object sender, EventArgs e)
           {
            F001_ShowDialog fs = new F001_ShowDialog();
            if (fs.ShowDialog() == DialogResult.OK)
               {
                this.tbx_value.Text = fs.RValue;
                MessageBox.Show("从F002_ShowDialog传值到Main窗体成功!");
            }
        }
        #endregion

        方法2: 通过构造函数进行页面传值#region 方法2: 通过构造函数进行页面传值
        private void btn_gouzao_Click(object sender, EventArgs e)
           {
            F002_GouZao gz = new F002_GouZao(this);
            gz.Show();
        }
        #endregion
       
        //定义textbox属性
        public TextBox Tvalue
           {
            get
               {
                return this.tbx_value;
            }
        }

        private void btn_cancel_Click(object sender, EventArgs e)
           {
            this.Close();
        }

        方法4:通过绑定事件进行传值#region 方法4:通过绑定事件进行传值
        private void btn_event_Click(object sender, EventArgs e)
           {
            F003_Event fe = new F003_Event();
            fe.myenent +=new MyEventHandler(fe_myenent);
            fe.ShowDialog();
        }
        private void fe_myenent(object sender, MyEvent e)
           {
            this.tbx_value.Text += e.MValue;
        }
        #endregion

        方法3:通过公共静态类进行传值#region 方法3:通过公共静态类进行传值
        private void btnComDate_Click(object sender, EventArgs e)
           {
            //F004_ComonClass fc = new F004_ComonClass();
            //fc.ShowDialog();
            this.tbx_value.Text = ComonData.sTextBox;
        }
        #endregion

        方法5:使用Attribute#region 方法5:使用Attribute
        protected override void WndProc(ref Message m)
           {
            if (m.Msg = "")
               {

            }
            base.WndProc(ref m);
        }

        #endregion
    }
}
F001_ShowDialog的代码:

public partial class F001_ShowDialog : Form
       {
        //第一种类型
        //定义一个变量
        public static string returnValue;

        //第二种类型
        //定义一个方法
        public string ReturnValue()
           {
            return this.tbx_value.Text.Trim();
        }

        //第三种类型
        //定义一个属性
        private string _value;
        public string RValue
           {
            get
               {
                return _value;
            }
            set
               {
                _value = value;
            }
        }

        public F001_ShowDialog()
           {
            InitializeComponent();
        }

        private void btn_ok_Click(object sender, EventArgs e)
           {
            returnValue = this.tbx_value.Text.Trim();
        }

        private void btn_ok1_Click(object sender, EventArgs e)
           {
            _value = this.tbx_value.Text.Trim();
        }
    }

F002_GouZao的代码:

    public partial class F002_GouZao : Form
       {
        //定义Main窗体的实例
        private Main main;

        //public F003_GouZao()
        //{
        //    InitializeComponent();
        //}

        public F002_GouZao(Main m)
           {
            InitializeComponent();
            main = m;
        }

        private void btn_ok_Click(object sender, EventArgs e)
           {
            main.Tvalue.Text = this.tbx_value.Text.Trim();
            MessageBox.Show("成功!");
        }
    }

F004_ComonClass的代码:

    public partial class F004_ComonClass : Form
       {
        public F004_ComonClass()
           {
            InitializeComponent();
            //ComonData.sTextBox = this.tbxInfo.Text.Trim().ToString();
        }

        private void btn_data_Click(object sender, EventArgs e)
           {
            ComonData.sTextBox = this.tbxInfo.Text.Trim().ToString();
        }
    }

F003_Event的代码:

    public partial class F003_Event : Form
       {
        //定义一个事件
        public event MyEventHandler myenent;
        public F003_Event()
           {
            InitializeComponent();
        }

        private void btn_ok_Click(object sender, EventArgs e)
           {
            if (myenent != null)
               {
                myenent(this,new MyEvent(this.tbx_value.Text.Trim()));
            }
        }
    }
自定义的委托和事件的代码:
    public delegate void MyEventHandler(object sender, MyEvent e);
    public class MyEvent : EventArgs
       {
        private string mvalue;
        public string MValue
           {
            get
               {
                return mvalue;
            }
        }
        public MyEvent(string s)
           {
            mvalue = s;
        }
    }
    // 定义一个公共数据类
    public static class ComonData
       {
        public static string sTextBox;
    }

Attribute部分还没完成,有些方面没有搞明白,过段时间再加上。

posted @ 2009-03-22 17:29  迪卡.凯恩  阅读(1029)  评论(0编辑  收藏  举报