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?

Community
  • 1
  • 1
Glen Hunter
  • 65
  • 1
  • 2
  • 11

1 Answers1

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();
John Koerner
  • 37,428
  • 8
  • 84
  • 134
  • 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 '13 at 04:30
  • @siride True, but you could leave a default constructor in there as well if you wanted. – John Koerner Jan 20 '13 at 04:34