How can I use variables from Form1 in Form2?

Your Form2 constructor is defined to get a generic Form as parameter in the constructor. You need to get a form of type Form1 so change your Form2 constructor to.

Your Form2 constructor is defined to get a generic Form as parameter in the constructor. You need to get a form of type Form1, so change your Form2 constructor to: private Form1 originalParent; public Form2( int answer, Button button3, Button button4, Button button5, Button button6, Form1 parentform, int fiftyfifty, int web, int change) { InitializeComponent(); originalParent = parentform; }.

From your posted code, I assume you've written a constructor for Form2 which takes an instance of a Form. Edit this constructor so that it takes an instance of Form1 instead. Or just cast the Form instance as Form1.

Cast the ref to the type of Form1 in Form2, then access the public function of Form1.

You need to call StartGame on an instance of Form1, not System.Windows.Forms.Form. If Form1 is the Owner of Form2 then you'd need to cast the Owner as type Form1. If Form1 is a parameter to the Ctor of Form2 then you'd need to make sure the Ctor param was defined as type Form1 and keep a reference to Form1 from a Form2 instance.

I assume that the this argument to your Form2 constructor is the Form1 instance, and that this code is therefore called from Form1. I also assume that you have a private member of Form2 private Form _form1; whose value is assigned in the constructor. If these assumptions are correct, you can fix this by changing the declaration to private Form1 _form1;.

You will also need to change the type of the constructor parameter from Form to Form1 (hat off to MusiGenesis).

You can check to see if you're form 2 get the instance of form 1 where the variable is defined. Once you have the instance ID for the form1 instance you're calling form 2 from, just make a new form 1 ref. I.E. Form1 frm1; public find(Form1 callingform) { InitializeComponent(); frm1 = callingform; } then just call form 2 form2(this).

Depends on why you need to do this, you could also define the desired variable or method as static. On Form1: public static int counter; On Form2 you can access it without passing the parentform instance as an argument to the Form2 constructor like this: Form1.counter.

1- About the answers given you will need to select those controls and set their modifiers to public, on the control's property pane, if you want Form2 to interact with them. 2- This part isn't an answer to your question, but it might help you understand why you shouldn't do things as you are doing. I have made an application with almost the same code as you but someone advised me that it wasn't a good practice or even OOP-like, so I posted a question to try and learn a bit more.

Take a look, there's code that helps you set things diferently and even in the same way you're trying to do.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions