C # transfer data between forms

Source: Internet
Author: User

C # transfer data between forms

C # transfer data between two forms

1. Pass public variable values

  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. Address-Based Transmission

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; // The form2 Pointer Points to 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; // The form2 parent form pointer is assigned to form1 Close ();}}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.