How can I use variables from Form1 in Form2? - c#

I have 2 forms: Form1 and Form2. I opened Form2 using:
Form2 newForm2 = new Form2(this);
And now I want to access some variables or methods from Form1 that are set in public like: public int counter;
But when I try this from Form2 it gives me an error:
Error 4 'System.Windows.Forms.Form' does not contain a definition for 'StartGame' and no extension method 'StartGame' accepting a first argument of type 'System.Windows.Forms.Form' could be found (are you missing a using directive or an assembly reference?)
Edit:
In form 1:
Form2 newForm2 = new Form2(answer, button3, button4, button5, button6, this, fiftyfifty, web, change);
newForm2.Show();
In form 2:
Form originalParent;
public Form2(int answer, Button button3, Button button4, Button button5, Button button6, Form parentform, int fiftyfifty, int web, int change)
{
InitializeComponent();
originalParent = parentform;
}
and I'm trying to access this like originalParent."public Method here" and it gives me that error.

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.

Related

Refresh an open form from another open form

I want to refresh an already opened form (form1) from another opened form's (form2) button_click(). In form1 I display the data saved by form2 and when form1 is already opened I want it to refresh if new data is saved on form2 in order to display it.
The problem is that I've tried iterating through `Application.Openforms`, but it turns out that it is read-only and I cannot access the form once found and I don't know how to access *form1* from *form2*, since I can't simply find it.
How can I access *form1* from *form2*?
Edit:
Form1 is actually opened from Form2.
The problem with Application.Openforms is that , as I've stated, a read-only list of forms already opened and I cant actually access the forms through it. They simply don't have the methods for it, I sugest you try using Application.OpenForms and look it up if you don't know how it works.
Also it's pointless to show what I've already tried because it includes Application.OpenForms, but for the sake of information:
FormCollection of = Application.OpenForms;
foreach (var f in of)
{
if (f.GetType().ToString() == "Kontrl_Doc.Visualizar")
{
f.Refresh();
}
}
When I click the button (button_click()) in Form2 it checks if Form1 is open or not. If Form1 isn't open it opens one and if it is than I'd like it to refresh it. Simultaneously, it closes Form2 and opens Form2 again, in order to reset is fields.
What I wan to do is, if the form1 is already opened , it form2 should tell it to refresh the already opened window with the form 1.
"Form1 is actually opened from Form2" - If this is the case, then just call Refresh using the form variable that you have in Form2. If necessary, make that a private field in the Form2 class or store it in an array for later use.
For example:
(Somewhere in Form2)
Form1 form1 = new Form1();
form1.Show();
(Inside the button click in Form2)
form1.Refresh();
you can use events. In form2 you place this code
public event Action ReloadForm1;
//on the place where you will reload form1
ReloadForm1();
and in form1 if you have opening form2:
form2.ReloadForm1 += Reload;
//outside method
void Reload()
{
this.Reload();
}
Create a void method in form1 and add the components u want to refresh maybe you want to reload a dropdown from db
public void Refresh()
{
...
}
then open a dialog of form2
catch the dialog result

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?
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.
Form3 F3 = new Form3(mainForm);
F3.Show();

find form instance from other class

I have Main form with list of data inside listBox. On button click I'm opening new form to create new data object (Main form is inactive in background), when new data is submitted listobox inside main form should be populated with that new object.
I was thinking following:
When Form2 is submitted I was thinking to find MainForm instance and kill that instance and after that it should be easy, load again list of data from the db and display in the listbox.
Question is:
If Form1 is created and on some event Form2 is instantiated with showDialog so Form1 is inactive until data is submitted how to find Form1 instance before Form2 is closed?
So again, how to find instance of Form1 class from Form2 class?
Thanks
You can get a reference to any of the application's currently open forms by using the Application.OpenForms property. Forms in this FormCollection can be accessed by index like so:
var form1 = Application.OpenForms[0];
or by the form's Name property like so:
Form form1 = Application.OpenForms["Form1"];
Hope this helps.
After getting the instance of an open form, I needed to call a method from that form, so this worked for me:
if (System.Windows.Forms.Application.OpenForms["Form1"] != null)
{
Form1 form1 = Application.OpenForms["Form1"] as Form1;
form1.yourMethodCall();
}
if you call
Form1.ShowDialog(this)
then you'll be able to get a reference to the calling form with
this.Owner.Name
in the second form (Form2 in your case)
see http://msdn.microsoft.com/en-us/library/system.windows.forms.form.showdialog.aspx

Get Application first run Form

I have two Forms in a project.
When my application runs, Form1 is opened. After that I am opening Form2.
How can I access Form1 from Form2 with reflection?
Why would you want to use reflection for this?
When you create the second form, just pass in a reference to the first one:
// I assume it's code within Form1 which opens Form2
Form2 form2 = new Form2(this);
form2.Show();
That's assuming you're happy to add a constructor with Form2 as a parameter. Alternatively, make it a property in Form2:
Form2 form2 = new Form2 { Form1 = this };
form2.Show();
if you open just one instance of form2 you can do this too: Form2 f2 = Application.OpenForms["Form2"];

Is there an accepted practice for cross-form control manipulation?

Let's say I have two forms: Form1 and Form2. Form1 has a textbox, Textbox1 and Button1.
When Button1 is clicked, a new instance of Form2 is created and shown. If Form2 needs to access Textbox1, how should expose it? Should Form2 have a public variable to hold a reference to the textbox? Or, when Form2 closes, should it invoke some public method on Form1 that updates the textbox? Thanks for your advice.
You should probably add a public property to the first form that exposes the textbox's text.
However, much more importantly, you should name your controls and forms.
There is (almost) nothing worse than a form with controls textBox1, button1, comboBox13, checkBox37.
If Form2 needs the text box value from Form1 when it loads, I would add the value to Form2's constructor method and pass it in that way.
If Form1 needs to obtain a new value entered in Form2 you can create a delegate with an Event that passes the value back to an assigned event handler in Form1.
Creating a public property may be the fastest solution, but I would try to stay away from circular references between forms if that is the case.
Hope this helps!

Categories

Resources