MilkToast
MilkToast

Reputation: 211

Interaction between forms

I am using visual studio 2012 (windows form application) and I have two forms, one with a label and the other with a button. I want it so that when you click the button the label on the other form goes up by one. I already have:

Label1 = Label1 + 1

I just need to know how to make the connection with the two forms. Maybe call a function? Btw I am new to the program and script so in simple terms plz.

Upvotes: 0

Views: 96

Answers (1)

Meysam Tolouee
Meysam Tolouee

Reputation: 579

Here is a sample I create for you. Add Fomr2 Like this:

public partial class Form2 : Form
{

    private void button1_Click(object sender, EventArgs e)
    {
        Form1.Instance.Controls.Find("label1", true).First().Text = "Some thing";
    }
}

And Form1:

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

    private static Form1 _Instance;
    public static Form1 Instance
    {
        get { return _Instance; }
        set { Instance = value; }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        new Form2().Show();
    }

Upvotes: -1

Related Questions