Eugenio Olivieri
Eugenio Olivieri

Reputation: 21

Call a third Form from the first Form

I have a "beginner" simple situation here:

i have my main form with a button calling a second form (form2) whit mainform parameters as:

In Form1 :

button_click

Form2 F2 = new Form2(this);
F2.Show();

In Form2 :

public class Form2(Form1 form1)    
InitializeComponent(); mainForm = form1;

Ok now i have a Form3(Form1 form1) and i want to call it (show) from Form2 but when i put the code in the second form (Form2):

button_click

Form3 F3 = new Form3(this);    
F3.Show();

gives me an error . I tried putting (Form1 form1) instead of (this) but it doesnt work.

How to call Form3 form Form2?

Upvotes: 0

Views: 407

Answers (2)

Matteo Mosca
Matteo Mosca

Reputation: 7448

Your attemps show a lack of understanding how parameters are passed to methods, it's not strictly related to winforms.

Anyway, you have declared a Form3 that takes an instance of Form1 as a parameter. If inside your Form2 code you do new Form3(this) the this will reference the instance of the object you're currently in, which is an instance of Form2, and it does not match the form signature.

Also, you can't pass a parameter to a method declaring it's type like you did - new Form3(Form1 form1) - as it does not make any sense and it is not valid syntax.

Since you have stored the Form1 instance reference in a local variable mainForm and your Form3 requires an instance of Form1 you should instantiate it like this: new Form3(mainForm). Make sure the mainForm variable is accessible from where your instantiate Form3.

Upvotes: 1

ASh
ASh

Reputation: 35713

Form3 F3 = new Form3(mainForm);    
F3.Show();

Upvotes: 1

Related Questions