码迷,mamicode.com
首页 > Windows程序 > 详细

C# 窗体间传递数据

时间:2014-07-08 13:11:03      阅读:297      评论:0      收藏:0      [点我收藏+]

标签:blog   使用   数据   os   2014   art   

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();
            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();
        }
    }


C# 窗体间传递数据,布布扣,bubuko.com

C# 窗体间传递数据

标签:blog   使用   数据   os   2014   art   

原文地址:http://blog.csdn.net/biruixing/article/details/37533671

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!