简体   繁体   中英

Button help c# Visual Studio

I have a button on a widows form that isn't working. the form is called form2

 private void button1_Click(object sender, EventArgs e)
    {
        Form1.setPlayerNames(p1.Text, p2.Text);
        this.Close();

    }

the code seems to check two TextBoxes, and assigns them a value in the next form, form1 which is supposed to be linked. for some reason the link doesn't work.

First, before you open the the form2 , set the global variable which is

public string textbox1Value = "";
public string textbox2Value = "";

Then open the the form2 something like form2.ShowDialog();

when calling button function, do this...

 private void button1_Click(object sender, EventArgs e)
    {
        textbox1Value = p1.Text;
        textbox2Value = p2.Text;
        this.Close();

    }

After close the form2, below the form2.ShowDialog(); put this,

Form1.setPlayerNames(form2.textbox1Value, form2.textbox2Value );

Try.

And i realize one thing is, Form1 is appeared as light blue color, are you trying to call the class function inside the form1? Without declare the variable?

ater working it a while I seemed to figure it out. This is what I had on form2:

    private void button1_Click(object sender, EventArgs e)
    {
        Form1.setPlayerNames(p1.Text, p2.Text);
        Form1 f1 = new Form1();
        this.Close();
        f1.ShowDialog();
    }

on form1 i had this:

 private void Form1_Load(object sender, EventArgs e)
    {
        label1.Text = player1;
        label3.Text = player2;

    }

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