Get Application first run Form - c#

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"];

Related

How to show and hide multiple forms?

I have three forms. Lets say that they are Form1, Form2 and Form3. And Form3 can be opened through Form1 or Form2. Everytime I show Form3 (form3.Show();) I hide Form1 or Form2.
How can I show again Form1 when I close Form3, if Form3 is opened via Form1?
And then show Form2 when I close Form3, if Form3 is opened via Form2?
You can put the following method on Form1 and Form2. When you want to show Form3 you call this method:
public void ShowForm3()
{
this.Hide();
using (Form3 f3 = new Form3())
{
f3.ShowDialog();
this.Show();
}
}
It will hide the current form, instantiate and show Form3 then when Form3 is closed it will re-show the current form.

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();

C# WinForms application showing many tray icons

I'm super new to C# and this is my third question here regarding it.
I'm making an app which can be minimized to the system tray. I have two forms named Form1 and Form2. What I have done so far is:
In Form1, I have a button which is showing the Form2 using this code:
this.Hide();
Form2 form2 = new Form2();
form2.Show();
The Form2 has a button which is hiding it, using this code:
this.Hide();
Now, I have the tray icon on the task bar. The tray icon has a ContexMenuStrip, and there is an option named show using this code:
Form1 form1 = new Form1();
form1.Show();
The problem is that when I click on it, a second tray icon is appearing on the taskbar. Both tray icons have the same menu and both are working. If I click on show again another window with Form1 pops up, and there are three tray icons, and so on....
Can someone help me?
It's because you are creating a new Form1 everytime.
Form1 form1 = new Form1();
You don't want to create a new Form1, you want to show the old one. Give Form2 a reference to your first form1 (call it theMainform1 for example). And then instead of
Form1 form1 = new Form1();
form1.Show();
You want to do
theMainform1.Show();
So you would have:
this.Hide();
Form2 form2 = new Form2();
form2.theMainform1 = this;
form2.Show();
The issue is that you are making a new instance of Form1. This creates a brand new window instead of reviving your old one.
Form1 form1 = new Form1();
form1.Show();
You need to have Form2 reference the original instance of Form1. You can make a constructor to pass in a self reference that would look like
Form2 form2 = new Form2(this);
You can prevent windows from having icons in the taskbar by settings ShowInTaskbar to false on the form. However, the other answers are correct when they say you're creating a new form over and over.
Why would you want two forms to show at the same time? Should they both be on the screen at the same time and be active at the same time? If so, you might try a MDI interface. http://en.wikipedia.org/wiki/Multiple_document_interface
It's possible in WinForms, but I think Microsoft is moving away from them in WPF.
Here is a working code in case that someone is looking for it:
Form1:
/* Hiding Form1 and showing Form2 */
private void btnHideForm1_Click(object sender, EventArgs e)
{
Form mod = new Form2();
mod.Owner = this;
mod.Show();
this.Hide();
}
Form2:
/* Hiding Form2 and showing Form1 */
private void btnHideForm2_Click(object sender, EventArgs e)
{
this.Owner.Show();
this.Close();
}
Thanks for your help guys!!!
I LOVE YOU!!!

Better way to Display forms one on top of another

I have a Main form.I want to launch another form from it and launch another from the launched form.I want to ensure that the Main form is not editable when sub forms are displayed so i use showdialog()
Mainform>(Showdialog)>form1>(showDialog+dispose)>form2(dispose)>Mainform
From Mainform i call form2.ShowDialog() then from form2 i use the following code to launch another form
this.visible=false;
form3.showdialog();
this.dispose();
But there are some problems in this.Is there a better way to achieve what im looking for?
edit:more description
I have a Main form,User clicks a Button on Mainform>Form1 is lauched>User clicks a Button in Form1>Form 2 is lauched(diposing/hiding form1) after form2 is closed Mainform should be brought to front and made editable,until then all other forms should be on top of Mainform and Mainform should be un-editable
The problem is that you have to specify the MainForm as the parent for (both) form2 and form3. When you use the overload of ShowDialog that has no parameters, WinForms uses the active form as the parent, so form3's parent becomes form2 automatically. You are then trying to close/dispose form2 causing form3 to become orphaned.
There are several options for getting the reference to MainForm, but the simplest is to use:
form2/3.ShowDialog(Application.OpenForms["MainForm"]);
Assuming that you have set the Name property on MainForm to "MainForm".
In your code, this.dispose() is executed only after form3 is closed. What i think you want is to close form2 after form3 was closed, so you can call this.Close() instead of this.Dispose().
this.visible=false;
form3.showdialog();
this.Close();
Or maybe, after form3 is shown up you dont need form2 any more. That meas:
this.visible=false;
//show instead of showdialog so it wont wait until form3 is closed
form3.show();
this.Close();
It looks like you are trying to implement something like a wizard. The best solution would be to launch all the child forms sequentially, in the main form.
If you need to pass data along the sequence, you should pass it from each dialog to the main form, which then passes it to the next dialog.
MainForm:
Form1 f = new Form1();
if (f.ShowDialog(this) == DialogResult.OK) {
Form2 f2 = new Form2();
f2.ShowDialog(this);
}
Form1 (button click which will open the form 2):
button1_click(object sender, EvengArgs e) {
this.DialogResult = DialogResult.OK;
Close();
}

How can I use variables from Form1 in Form2?

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.

Categories

Resources