简体   繁体   English

将dateTimePicker和文本框的信息从Form1传输到Form2

[英]Transfer info of dateTimePicker and textbox from form1 to form2

okay so I'm trying to transfer my info of name(textbox) and birthdate(dateTimePicker) to Form2. 好吧,所以我想将我的姓名(文本框)和生日(dateTimePicker)的信息传输到Form2。 So, basically i have a form1 with name,birthdate and btn1 and when I click btn1 I go to Form2. 因此,基本上我有一个具有名称,生日和btn1的form1,当我单击btn1时,我进入了Form2。 I have another button btn2 in form2. 我在form2中还有另一个按钮btn2。 Question: So how can I display value of form1 textbox and datetimepicker when I click my btn2 in Form2. 问题:因此,当我在Form2中单击btn2时,如何显示form1文本框和datetimepicker的值。

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2();
        this.Hide();
        f2.Show();

    }

    private void button2_Click_1(object sender, EventArgs e)
    {

    }

In the event where you want to move from form1 to form2 如果要从Form1移到Form2

Form2 obj = new Form2(textbox1.Text,DateTime.Value);
this.Hide();
Form2.Show();

In form 2 在表格2中

public form2()
{
InitializeComponent();
//if you need something
}
string someName="";
DateTime dt;
public form2(string name,Datetime value)
{
InitializeComponent();
someName=name;
dt=value;
}

Now your someName will have that value 现在您的someName将具有该值

The quick way is to make sure that the controls on Form2 are public, then populate them like this... 快速的方法是确保Form2上的控件是公共的,然后像这样填充它们...

Form2 f2 = new Form2();
f2.ControlName.Text = this.ControlName.Text;
f2.Show();

However I would encapsulate this into a public function. 但是,我会将其封装到一个公共函数中。

Form2 f2 = new Form2();
f2.Populate(txtValue1.Text, dtDateTime.Value);
f2.Show();

and in Form2 并在Form2中

public void Populate(string Value1, DateTime Value2)
{
   txtValue1.Text = Value1;
   dtValue2.Value = Value2;
}

Form1 表格1

        public Form1()
        {
            InitializeComponent();
            Load += delegate
            {
                var frm2 = new Form2 { DateValue = dateTimePicker1.Value.ToShortDateString() };
                frm2.Show();
            };
        }

Form2 表格2

        public string DateValue
        {
            get { return textBox1.Text; }
            set { textBox1.Text = value; }
        }

        public Form2()
        {
            InitializeComponent();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM