• C# 窗口间传递数据


    C#两个窗口之间传递数据

    1 公用变量值传递

      public partial class Form1 : Form  //parent form
        {
            public string name="";
            public Form1()
            {
                InitializeComponent();
            }
            private void newBtn_Click(object sender, EventArgs e)
            {
                Form2 form2 =new Form2();
                form2.ShowDialog(); //NOTE this! must be ShowDialog()  NOT Show()!
                if (form2.DialogResult == DialogResult.OK)
                {
                    textBox1.Text = form2.name;
                    form2.Close();
                }
            }
        }
     public partial class Form2 : Form // son form
        {
            public string name
           {
                set { textBox1.Text = value; }
                get { return textBox1.Text; }
            }
            public Form2()
            {
                InitializeComponent();
            }
    
            private void OK_Click(object sender, EventArgs e)
            {
                if (textBox1.Text == "")
                {
                    MessageBox.Show("input!");
                    return;
                }
                DialogResult = DialogResult.OK;
                Close();
            }
        }

    2 使用地址方式传递

     public partial class Form1 : Form  //parent form
    
        {
            public string name="";
            public Form1()
            {
                InitializeComponent();
            }
            private void newBtn_Click(object sender, EventArgs e)
            {
                Form2 form2 =new Form2();
                form2.Owner = this;//form2的指针指向form1
                form2.ShowDialog();
                textBox1.Text = form2.name;
                form2.Close(); 
            }
        }

     public partial class Form2 : Form  //son form
        {
            public string name
           {
                set { textBox1.Text = value; }
                get { return textBox1.Text; }
            }
            public Form2()
            {
                InitializeComponent();
            }
    
            private void OK_Click(object sender, EventArgs e)
            {
                if (textBox1.Text == "")
                {
                    MessageBox.Show("input!");
                    return;
                }
                Form1 form1 = (Form1)this.Owner;//form2的父窗口指针赋给form1
                Close();
            }
        }


  • 相关阅读:
    iOS 字体
    接口继承和实现继承的区别
    实验楼实验——LINUX基础入门
    试看看能不能发布
    mysql binlog恢复
    percona
    ssh2 php扩展
    sphinx
    ngios
    socketlog
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/4298797.html
Copyright © 2020-2023  润新知