-2

I'm using WinForms. I have 2 Forms, Form1 (Main Form) and Form2. I have 2 variables in Form1 which is Computer_Selected and Default_Selected. I change one of the variable when i click on a button in Form1 and then show Form2. In Form2 i want to know the variables bool value. How can I do this?

Form 1

    public bool Computer_Selected; 
    public bool Default_Selected;

    private void btn_Public_Kiosk_Click(object sender, EventArgs e)
    {
        Computer_Selected= true;
        Default_Selected = false;
        //show form2 and hide this form-(form1)...
    }

Form 2

    private void Form1_Load(object sender, EventArgs e)
    {
         Console.WriteLine("Bool Value = " + Computer_Selected);
    }
taji01
  • 2,527
  • 8
  • 36
  • 80
  • 2
    https://msdn.microsoft.com/en-us/library/f6525896(v=vs.90).aspx – BugFinder Feb 14 '17 at 10:37
  • 2
    You can find some useful options here: [Interaction between forms — How to change a control of a form from another form?](http://stackoverflow.com/a/38769212/3110834) – Reza Aghaei Feb 14 '17 at 10:42
  • You can try to pass the value to the second form, I think this is the syntax `private void btn_Public_Kiosk_Click(object sender, EventArgs e){ Computer_Selected= true; Default_Selected = false; Form2 frm2 = new Form2(Computer_Selected); frm2.Show();}` then in form2 `public Form2(bool computerSelected){InitializeComponent(); //use value as needed}` – Nope Feb 14 '17 at 10:49

1 Answers1

0

If you will have only one instance of Form1, than you can set that variables to static

public static bool Computer_Selected; 
public static bool Default_Selected;

Then in your Form2 you can access them:

private void Form2_Load(object sender, EventArgs e)
{
     Console.WriteLine("Bool Value = " + Form1.Computer_Selected);
}
Tomas Chabada
  • 2,869
  • 1
  • 17
  • 18
  • This worked thanks. I don't know why someone down-voted your question at first. – taji01 Feb 14 '17 at 10:52
  • @taji01 likely because it's one of the more basic questions for winforms that you'll find and should have been flagged instead of answered. – James Gould Feb 14 '17 at 10:54
  • @JayGould Even though it was basic to a lot of people, I googled this question before I wrote it. I couldn't find a question that would pass a variable between the forms, so i thought id ask. All i could find was pass data between form or people demonstrating how to do this with a textbox in YouTube. – taji01 Feb 14 '17 at 10:59
  • I haven't done winforms in years since I moved to the web but even though, would you not rather create a solution that doesn't hard-wire dependencies of forms? Why should form2 know about form1? If all you need is a bool value, why not pass it through the form2 constructor or if an instance of form2 already exists, populate a public variable on form2, etc.. I'm genuinely asking because it seems strange to me to have that dependency. – Nope Feb 14 '17 at 11:01
  • @taji01 literally the first result for "winforms variable between forms" is a stackoverflow link that answers your question: http://stackoverflow.com/questions/4587952/passing-data-between-forms – James Gould Feb 14 '17 at 11:06
  • @Fran if you're following an MVVM pattern now, you should have the values held in your VM and then accessed by a view, but for this particular question passing the value is fine. – James Gould Feb 14 '17 at 11:16
  • @JayGould and that's exactly what i wasn't looking. The answer provided in that link you searched up creates a new instance of Form1, and in my case i wasn't looking to creating a new instance of form1 – taji01 Feb 14 '17 at 16:34