Does anyone know how to maximize a form from another form, in c#?
I tried the code below but it won't work:
Form1 form1 = new Form1();
form1.WindowState = FormWindowState.Maximized;
Any ideas?
Well two possible problems, either you don't get any form at all, then the solution is to Show the form.
Form1 form1 = new Form1();
form1.WindowState = FormWindowState.Maximized;
form1.Show();
But I guess that you already have a form1 loaded somewhere, then you can not use
Form1 form1 = new Form1();
because then you creates a new form that you don't display, remove this line and find a way to pass a reference to form1 from where it was originally created to the method where the above code is located.
Go To Load Form Or Button As View Code and use This Code :
C#
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
I hope it will be useful for you ;)
Related
I'm facing a problem with form show. I have a main form where I have my GUI and I choose an option that creates an instance of a form. For example in my main form I have:
Form2 f2 = new Form2();
f2.Show();
The problem is that the form shows for about 1-2 secs and then goes behind the main form.
I tried some instructions in the main form below f2.Show() command like
f2.BringtoFront();
this.SendtoBack();
Also I added commands to the new form (Form2) load method:
this.BringtoFront();
this.Activate();
this.Focus();
Nothing of the above commands seems to be a solution for this. Only when I use f2.ShowDialog(); instruction in my main form but I don't want to do that because I need immediate access to both of these forms at the same time.
Any help? Thanks
If you don't want your second form to never go behind your main form then pass the owner in the overload of Show method that accepts the owner parameter
Form2 f2 = new Form2();
f2.Show(this); // Assuming this code runs inside the main form
If you remove or change it to comment this.SendtoBack(); :
f2.BringtoFront();
//this.SendtoBack();
it'll be OK!
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
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!!!
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"];
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();
}