0

I am making a game, and I have 3 main forms that I am using, one is the start menu, one is the main game form, and the other is a create map form. My question is directed to how can I send variables between them. I know of the classic form setup, where you show one form from the other, and pass information like this...

 Form1 form = new Form1();
 form.Show();
 form.varible = ...

But i don't like how the previous form is still showing, and form.ShowDialog isn't what I am going for either. I have a system where you cycle through forms, where it opens your new form and closes the old one, and it works great, except I don't know how to send information to the new form. I changed the Program.cs code to look like this

    public static bool OpenForm1OnClose { get; set; }
    public static bool OpenForm2OnClose { get; set; }
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        OpenForm1OnClose = true;
        OpenForm2OnClose = false;
        while (OpenForm1OnClose || OpenForm2OnClose)
        {
            if(OpenForm1OnClose) 
                Application.Run(new Form1());
            if (OpenForm2OnClose)
                Application.Run(new Form2());
        }

    }

This is just a testing program right now that switches between two forms - click one button and it goes to the other form and closes the one you were on. Here's what the button click code looks like

 private void button1_Click(object sender, EventArgs e) // form1's button
    {
        Program.OpenForm2OnClose = true;
        this.Close();
    }

That is all the code right there, nothing more to it really. Any Idea's on how to pass variables would be awesome, or if its straight up impossible, advice on a good way to switch between forms would be helpful. Thanks for reading

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
DurpBurger
  • 15
  • 4
  • Just add a class to your application that will be incharge of the dataflow. It can be a static class or a singelton, doesn't matter all that much. – Zohar Peled May 10 '15 at 19:06
  • Will look more into static classes, I am not a very experienced programmer, but I know what you are thinking, and it should work, thanks. – DurpBurger May 10 '15 at 19:17

1 Answers1

0

If you're asking what I think, the answer is simple. You just need to have your constructor take the information as its args.

private var myInfo;

public Form1(var myInfo_)
{
    InitializeComponent();
    myInfo = myInfo_;
}

I hope this is in fact what you want.

Rogue
  • 636
  • 8
  • 22
  • Clever, but unfortunately I wouldn't be able to send a variable of any use, since its in the program.cs that is calling the forms, and that has no idea what I want to send. – DurpBurger May 10 '15 at 19:16