C#中窗体之间的传值操作(传递值和获取返回值)

  在C#的应用程序开发中,窗体之间的传值操作是不可避免会经常遇到的操作。
  比如,在一个窗体中调用另外一个窗体、传递值并且得到返回值,通常情况下有哪些方法呢?
  一般情况下,通过工程项目的公有变量、窗体的公有变量(字段加访问属性)、窗体构造函数、委托和事件等方式来实现,下面举例说明。
  一、通过公用变量
  在最初的开发中,通过公有变量可以干很多事情,似乎屡试不爽,也是习惯性的思维,只要能有效避免访问冲突即可。

  定义公有访问类:

    internal class GlobalVar
    {
        private static string _StrPsp = "";// 私有静态字段
        public static string StrPsp // 公有静态属性
        {
            get { return _StrPsp; }
            set { _StrPsp = value; }
        }
    }

  在窗体form2中改变它的值:

            GlobalVar.StrPsp = "这是改变后的值";
            DialogResult = DialogResult.OK;
            Close();

  调用form2窗体并得到返回值:

            //可以处理GlobalVar.StrPsp
            //......
            Form2 form = new Form2();
            DialogResult FrmSelect = form.ShowDialog();
            if (FrmSelect == DialogResult.OK)
            {
                // 获取返回值
                MessageBox.Show( GlobalVar.StrPsp);
                // 处理返回值
            }
            else
            {
                //不取值
                MessageBox.Show("没有改变值");
            }

  也不一定必须使用ShowDialog方法,这只是为了聚焦访问和避免访问冲突。

  二、通过使用ShowDialog方法
  使用ShowDialog方法打开一个窗体并将其设置为模态对话框,等待这个窗体关闭后,可以通过访问其公共属性或方法来获取返回值。
  它与上面的区别在于,前面是访问整个工程的公有变量,而这个方法是访问的窗体的公有变量。这里打开窗体并且赋值,访问结束后获取返回值,这些都是通过访问窗体的公有变量实现的。
  例如:

  在form3窗体中定义访问变量:

public string StrReturn { get; private set; }

  在需要返回结果时改变返回值:

            StrReturn = "这是改变后的返回值";
            DialogResult = DialogResult.OK;
            Close();

  调用:

            Form3 form = new Form3();
            MessageBox.Show(form.ReturnValue,"改变前的值");
            DialogResult result = form.ShowDialog();
            if (result == DialogResult.OK)
            {
                // 获取返回值
                MessageBox.Show(form.StrReturn);
            }

  如果需要访问的变量比较多或者复杂,可以通过定义类来实现,例如:

        public static class NetInfo
        {
            private static string _IpAddress;

            public static string IpAddress
            {
                get { return _IpAddress; }
                set { _IpAddress = value; }
            }
        }

  form3的返回:

            NetInfo.IpAddress = "1.2.3.4";
            DialogResult = DialogResult.OK;
            Close();

  提取返回值:

            Form3 form = new Form3();
            MessageBox.Show(Form3.NetInfo.IpAddress,"改变前的值");
            DialogResult result = form.ShowDialog();
            if (result == DialogResult.OK)
            {
                // 获取返回值
                string value = Form3.NetInfo.IpAddress;
                MessageBox.Show(value);
            }

  上面的两种方式分别是通过工程项目的公有变量和窗体的公有变量来实现值传递和获取返回值的,简单实用而且快捷方便。

  在很多的应用开发工具中,默认都有一个传递的公有对象,它相当于一个中间传递者,就是数据中转站的角色。

  三、通过事件

  通过窗体的构造函数进行参数传递,通过事件回调获取返回值。

  定义公有访问类:

    public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    }

  在需要访问的窗体form4进行定义:

        public event EventHandler<Student> ReturnValueEvent;//回调事件
        private Student MyStudent;

  form4的构造函数:

        public Form4(Student student)
        {
            InitializeComponent();
            MyStudent = student;
        }

  返回值:

            MyStudent.Id = 2;
            MyStudent.Name = "Java";
            MyStudent.Age = 3;
            //MessageBox.Show(MyStudent.Name);
            // 触发事件,传递返回值
            ReturnValueEvent?.Invoke(this, MyStudent);
            Close();

  调用form4窗体,传递值并获取返回值:

            Student student = new Student();
            student.Name = "C#";
            student.Id = 1;
            student.Age = 2;
            Form4 targetForm = new Form4(student);
            targetForm.ReturnValueEvent += targetForm_ReturnValueEvent;//订阅事件
            targetForm.ShowDialog();

  事件处理:

        private void targetForm_ReturnValueEvent(object sender, Student student)
        {
            // 处理返回值
            MessageBox.Show(student.Name);
        }

  四、通过委托

  调用者准备,定义一个访问类:

    public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    }

  声明处理方法:

        private void Form_ReturnValueCallback(Student MyStudent)
        {
            // 处理返回值
            MessageBox.Show(MyStudent.Name);
        }

  调用form4:

            Form4 frm4= new Form4();
            frm4.ReturnValueCallback = Form_ReturnValueCallback;
            frm4.Show();

  在被调用的窗体中定义一个委托:

public Action<Student> ReturnValueCallback { get; set; }

  返回值:

            Student student1 = new Student();
            student1.Id = 2;
            student1.Name = "C#";
            student1.Age = 3;
            // 调用委托,传递返回值
            ReturnValueCallback?.Invoke(student1);
            Close();

  上面总结了窗体之间进行传值的常规四种操作,可在项目开发中根据实际情况进行选择。
 

猜你喜欢

转载自blog.csdn.net/dawn0718/article/details/131954729