开发者

How to Transferring value from form1 to form 2 when form1 is open?

开发者 https://www.devze.com 2023-04-06 14:24 出处:网络
I am working on a simple project, however i am a newcomer in C sharp. Basically i am android developer but i am learning c#. I have two forms say for example -- form1 and form2. On form1 there is butt

I am working on a simple project, however i am a newcomer in C sharp. Basically i am android developer but i am learning c#. I have two forms say for example -- form1 and form2. On form1 there is button. On click of this button form2开发者_Go百科 is open and certain value from form1 has to be transferred to form2. How to transfer it.

Please help me as i don't have any idea.


In the From2 class you should add the public property for example and on Form2 designer add the Label control add in form load event set to the label Text property value from MyProperty

public partial class Form2 : Form
{
    public string MyProperty { get; set; }

    public Form2()
    {
        InitializeComponent();
    }



    private void Form2_Load(object sender, EventArgs e)
    {
        label1.Text = MyProperty;
    }
}

after that in the button click event handler in Form1 add the following code

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 form2 = new Form2();
        form2.MyProperty = "This is from Form1";
        form2.Show();
    }
}

that all what you need to do

0

精彩评论

暂无评论...
验证码 换一张
取 消