Martin
Martin

Reputation: 177

Passing data between forms C#

I have game with three levels on three different forms. I save each level's completion time and want to access it on the 4th form. I've done this:

public partial class Results : Form
{
    public int  time1, time2, time3;
    FormLevel1 rez1 = new FormLevel1();
    FormLevel2 rez2 = new FormLevel2();
    FormLevel3 rez3 = new FormLevel3();

    public Results()
    {
        InitializeComponent();


    }
    void Calculations()
    {
        time1 = rez1.levelTime;
        time2 = rez2.levelTime;
        time3 = rez3.levelTime;
        MessageBox.Show(time1.ToString());
        MessageBox.Show(time2.ToString());
        MessageBox.Show(time3.ToString());
    }
}

I get all zeros. I guess I'm doing something wrong. How can I solve this problem correctly? Thanks.

Upvotes: 1

Views: 95

Answers (2)

interceptwind
interceptwind

Reputation: 675

let me try to explain what you are doing wrong here (based on my assumption of where you do your Calculations(). Pardon me if I am wrong.)

public partial class Results : Form // (0)
{
    public int  time1, time2, time3;
    FormLevel1 rez1 = new FormLevel1(); //(1)
    ...

    public Results()
    {
        InitializeComponent(); 
        Calculations(); // (2) Assuming you call Calculations() here
    }

    void Calculations()
    {
        time1 = rez1.levelTime;
        MessageBox.Show(time1.ToString()); //(3)
        ...
    }
}

(0) At Time=0, you created your Results class instance

(1) At Time=0, you created your FormLevel1 class instance

(2) At Time=0, you do your Calculations()

(3) At Time=0, you show your message box of the FormLevel1's levelTime

i.e. (0) to (3) happened at (almost) the SAME time! Your FormLevel1 has no chance to do and complete it's Timer stuff BEFORE the Results do its Calculation(), hence the messageBox shows 0.

Without you explaining more about what you are trying to achieve here OR giving us your FormLevel's code, we cannot give you more specific solutions.

One possible solution is to call your Results.Calculation() FROM your FormLevels AFTER they finish doing their Timer stuff.

How?

public partial class Results : Form 
{
    ...
    FormLevel1 rez1 = new FormLevel1(this); //pass this instance of Results into FormLevel1
    ...

    public void Calculations() //Add public keyword so that it is accessible from FormLevel1
    {
        ...
    }
}

public partial class FormLevel1: Form 
{
    Results resultForm;

    public FormLevel1(Results callingForm) //modify constructor to accept Results
    {
        resultForm = callingForm;  
    }

    void TimerCompleted() //Call this when Timer finish its stuff
    {
        resultForm.Calculations();
    }

    ...
}

Note that this may not be EXACTLY the solution you want. But this is generally the simplest way to pass data between Forms, answering your Title question. (FormLevel1 can now access ALL its callingForm's public variables and methods)

Hope that it helps your understanding.

Upvotes: 0

Gian Paolo
Gian Paolo

Reputation: 4249

With

FormLevel1 rez1 = new FormLevel1();
FormLevel2 rez2 = new FormLevel2();
FormLevel3 rez3 = new FormLevel3();

you are creating new instances of the three forms, not using the "previous" instances you want to use.

You need (e.g.) public properties to pass the 3 "previous" forms the the instance of Form4 (or any method to achieve the same).

But Actually, consider what you really need to pass to your form Results: from your code, it seems you just need to pass 3 integer (levelTime for each form)

[Edit] Just realize now your time1, time3 and time3 member variables are public. So in the calling code you can do something such:

Result resForm = new Result();
resForm.time1 = ... // have you saved result of form1 in a variable? use it here!
resForm.time2 = ... // same for form2
resForm.time3 = ... // same for form3
resForm.ShowDialog();

Upvotes: 1

Related Questions