简体   繁体   中英

Passing Variable Between 3 Forms

I am trying to pass a couple variables between 3 separate forms. Form 1 is where each player (2 players) picks their name and type. It then sets player 1 variables into the second form and player 2 variables into the third form.

 private void button_player2Ready_Click(object sender, EventArgs e)
    {
        p2_name = textBox_P2.Text;


        if (p2_name.Length > 0 && p2_type != null)
        {
            foreach (Control cont in groupBox_p2.Controls)
            {
                cont.Enabled = false;

            }
            player2Ready = true;

            if (player1Ready == true && player2Ready == true)
            {
                Form1 firstForm = new Form1();
                Form2 secondForm = new Form2();
                Form3 thirdForm = new Form3();

                //thirdForm.p2_name = "hello";
                thirdForm.p2_type = p2_type;
                thirdForm.p2_name = p2_name;



                secondForm.p1_name = p1_name;

                secondForm.p1_type = p1_type;



                this.Hide();
                secondForm.Show();



            }
        }
        else
        {
            MessageBox.Show("Error: You have not selected a name or type...");

        }

    }
}

The second form works fine and player 1's name is displayed in the label but I believe this is causing problems when going to 3rd form as it is resetting the name of the third form because of this " Form3 thirdForm = new Form3();"

      public string p1_name { get; set; }


    public string p1_type { get; set; }



    public string word4player2;

    public Form2()
    {
        InitializeComponent();
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        label1.Text = $"{p1_name} choose your opponents word...";

    }

    private void button_submit_Click(object sender, EventArgs e)
    {
        if (!textbox_p1Word.Text.Contains(" "))
        {
            word4player2 = textbox_p1Word.Text;
            Form1 firstForm = new Form1();
            Form2 currentForm = new Form2();
            Form3 thirdForm = new Form3();

            thirdForm.Show();
            Hide();


        }
        else
        {
            MessageBox.Show("No Spaces allowed...");

        }

    }

    private void Form2_FormClosed(object sender, FormClosedEventArgs e) //this is needed bc form1 is only hidden when form 2 opens thus not closing application properly
    {

        Application.Exit();
    }
}

This is my third form which is supposed to display player 2's name. The form opens with no errors but the spot where the name goes is blank.

    public string p2_name { get; set; }


    public string p2_type { get; set; }

    public string word4player2;

    public Form3()
    {
        InitializeComponent();
    }


    private void button_submit_Click(object sender, EventArgs e)
    {
        if (!textbox_p2Word.Text.Contains(" "))
        {
            word4player2 = textbox_p2Word.Text;
        }
        else
        {
            MessageBox.Show("No Spaces allowed...");

        }

    }

    private void Form2_FormClosed(object sender, FormClosedEventArgs e) //this is needed bc form1 is only hidden when form 2 opens thus not closing application properly
    {
        Application.Exit();
    }

    private void Form3_Load(object sender, EventArgs e)
    {
        label1.Text = $"{p2_name} choose your opponents word...";
    }

Any help would be great!

If you are not using Form3 directly from Form1, I would refrain from creating an instance of Form3 in Form1.

For passing the values to Form3, you can first pass it to Form2, and then in Form2, pass it to Form3 while invoking it.

In Form1

        Form2 secondForm = new Form2();
        secondForm.p1_name = p1_name;
        secondForm.p1_type = p1_type;

        secondForm.Show();

In Form2

        Form3 thirdForm = new Form3();
        thirdForm.p2_name = p2_name;
        thirdForm.p2_type = p2_type;
        thirdForm.Show();

You are setting up

thirdForm.p2_type = p2_type;
thirdForm.p2_name = p2_name;

in your main form, and create another instance of Form3 in Form2 and showing that one. Note that every call for the constructor of an object create a new instance of that class.

Form3 thirdForm = new Form3();

line in Form1 creates an instance of Form3 type and sets up the type and the name properties. But this instance is not shown. You create another instance of Form3 in Form2's button_submit_Click method and show that without setting up the type and name values. If this form will be called only in Form2 then you should put two properties to Form2 class for p2_type and p2_name and organize the method as

private void button_submit_Click(object sender, EventArgs e)
{
    if (!textbox_p1Word.Text.Contains(" "))
    {
        word4player2 = textbox_p1Word.Text;
        Form1 firstForm = new Form1();
        Form2 currentForm = new Form2();
        Form3 thirdForm = new Form3();

        thirdForm.p2_type = p2_type;
        thirdForm.p2_name = p2_name;

        thirdForm.Show();
        Hide();
    }
    else
    {
        MessageBox.Show("No Spaces allowed...");

    }

}

and Form1

        if (player1Ready == true && player2Ready == true)
        {
            //Form1 firstForm = new Form1();//YOU DON'T NEED IT HERE
            Form2 secondForm = new Form2();
            //Form3 thirdForm = new Form3();//YOU DON'T NEED IT HERE


            secondForm.p2_type = p2_type;
            secondForm.p2_name = p2_name;

            secondForm.p1_name = p1_name;
            secondForm.p1_type = p1_type;

            this.Hide();
            secondForm.Show();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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