0

I am sure this is an easy question to answer, but I am having difficulty implementing it how I need.

I am using Visual Studio 2013 and C#

I have two forms, one that loads when the app starts up which contains user-selectable settings such as user id, how many puzzle pieces that would like on screen... and a confirm button.

The second form is like a puzzle grid, which is generated when the user clicks the confirm button on the first form.

When the person has correctly solved the puzzle, a message box pops up with the time it took to solve.

What I want to be able to do is add the user id field into the messagebox string.

I have seen many examples of using event args and getting and setting fields but most are assuming one form is generated from a previous form, where I just want to 'grab' the information from one form and store it on the second form for use in a string.

Links to tutorials would also be appreciated if that is easier.


I found out what I was doing wrong, with the help of everyone's answers.

I had the variables declared on the first form, but they were declared in the textbox_leave and updownBox_leave methods when they should have been declared at the very top of the class.

Then I just called Form1.IdString and Form1.puzzleNumberString from my Form2 and what-do-you-know everything went as I thought it should.

smokeAndMirrors
  • 155
  • 1
  • 5
  • 18
  • duplicate of: http://stackoverflow.com/questions/1559770/send-values-from-one-form-to-another-form-in-c-sharp-winforms-application – Christophe De Troyer Jan 20 '14 at 02:23
  • @ChristopheDeTroyer I had read that question before posting, it was close to what I was asking, but their program path was quite different to the approach I was taking. Thank you for taking time to view my question though. – smokeAndMirrors Jan 20 '14 at 02:47

2 Answers2

3

It's pretty simple.All you need to do is pass your id variable to second form constructor.For example in your Confirm Button click:

Form2 f2 = new Form2(myVariable);
f2.Show();

And you should add a constructor to the Form2:

public string ID { get; set; }

public Form2(string id)
{
    InitializeComponent();
    ID = id;
}

Then you can use ID variable in your second form.

Selman Genç
  • 100,147
  • 13
  • 119
  • 184
  • Thank you for your comment. It didn't quite work for me. I will probably just restart the project with a different arrangement of Forms so that some tutorials will be easier to follow. – smokeAndMirrors Jan 20 '14 at 02:46
0

In your puzzle form create a global Form variable, then give the constructor a Form f parameter and set your global form variable as the passed parameter:

public Form form = null;
public PuzzleForm(Form f) //puzzle form constructor
{
    form = f;
}

then go into your first form and where you create an instance of the puzzle form change it so that it passes an instance of this.

PuzzleForm pf = new PuzzleForm(this);

Now from inside your Puzzle form you can access your Form1 variables like so MessageBox.Show("UserID: "+form.userID.Text);

Spark
  • 1,007
  • 1
  • 8
  • 26