0

Possible Duplicate:
Passing variable between winforms

I have two forms one called Form1 the other called TicTacToeMainMenu.

In TicTacToeMainMenu I have created two variables

string Player1;
string Player2;

I have assigned two text fields to both variables.

pvpPl1.Text = Player1;
pvpPl2.Text = Player2;

I would like to grab the string values from TicTacToeMainMenu and use them in another form, Form1 how would I do this?

5

1 Answer 1

2

If you are instantiating form1 from the TicTacToeMainMenu form, then you can pass the variable into the constructor of Form1:

public string Player1 { get; set; }
public string Player2 { get; set; }
public Form1(string player1, string player2)
{

    InitializeComponent();

    this.Player1 = player1;
    this.Player2 = player2;

}

Then to call it, you simply:

Form1 f = new Form1(Player1, Player2);
f.ShowDialog();
2
  • You can't then design the form in the Visual Studio designer. Not everyone cares about that, but I thought I'd point it out anyway. The alternative is to have properties that are intended to be set during form initialization.
    – siride
    Jan 20, 2013 at 4:30
  • @siride True, but you could leave a default constructor in there as well if you wanted. Jan 20, 2013 at 4:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.