How to minimize a form in C# after showing another? - c#

My question is if I open a new form like this:
Admin inst = new Admin();
inst.Show();
Form1.WindowState = FormWindowState.Minimized; ///Doesn't work
How do I minimize the form I was on before opening the other one, without having to open another instance of Form1 ? If I use this.WindowState... it minimizes the whole program. IE: Form1

Hide your form first then show the Admin form.

this.WindowState = FormWindowState.Minimized
//Show your form here

#IceDawg ur question is hard to understand...but r u trying to do this
Form2 form = new Form2();
form.Show();
this.WindowState = FormWindowState.Minimized;

Related

Switching between Forms in Winforms

How to switch between forms in winforms ?
I tried below code:
this.hide();
Form1 f = new Form1();
f.Show();
Here the result was not as expected, the new panel displayed with different location from parent panel. Any ways to make the navigation perfect ?
You can use CenterParent flag, providing your parent window in Show function.
var frm = new Form2();
frm.StartPosition = FormStartPosition.CenterParent;
frm.Show(this);

Opening a Form from Another Form C#

I have forms 'Form1','Form2' and a mdi. Mdi contains a splitcontainer. In split container there is two panels first one for menu and the other for displaying forms when we click on the menu. My issue is I want to call Form2 from Form1 when I click on an icon in the Form1. I wrote the bellow code in Form1 's icon click. But the Form2 is not showing. I wrote another alternative code like Form2.show() in this case Form2 is displaying but not fit in the panel2 of the Mdi. It is displaying like a popup.
This is the code that I wrote in the Form1 icon click.
private void icon_Click(object sender, EventArgs e)
{
this.Close();
Form2 obj2 = new Form2 ();
obj2 .Show();
obj2 .Location = new Point(0, 0);
obj2 .TopLevel = false;
Mdi Objmdi = new Mdi();
Objmdi.splitContainerControl1.Panel2.Controls.Add(obj2); Objmdi.splitContainerControl1.Panel2.Controls["Form2"].BringToFront();
}
Create an object reference to form 2 in the button_click event and call the 'show' function.
Form1 main = new Form1();
main.Hide();
Form2 second = new Form2();
second.Show();
second.Width = this.Width;
second.Height = this.Height;
second.StartPosition = FormStartPosition.Manual;
second.Location = new Point(this.Location.X, this.Location.Y);
this.Visible = false;
If you want your Form2 to be displayed in some panel instead of a new window then you should create a UserControl instead of a Form and then you should be able to call Objmdi.splitContainerControl1.Panel2.Controls.Add(myNewUserControl);
When a form is closed, all resources created within the object are closed and the form is disposed.
So when you call this.Close(); code after this is not executed.
Change it to this.Hide();.
OR
Change it to this.Hide(); and pass Form1's reference to Form2, which should close it.
Also,
You are creating a new MDI in the below line:
Mdi Objmdi = new Mdi();
Objmdi.splitContainerControl1.Panel2.Controls.Add(obj2); Objhome.splitContainerControl1.Panel2.Controls["Form2"].BringToFront();
Instead of creating a new MDI use existing MDI's reference to do this operation.
Thirdly, Controls["Form2"] try changing to Controls["obj2"]

MDI Child Forms Opening each other with the same parent form

So I have 3 Forms, lets call them Form1, Form2, and Form3.
I have sent the IsMDIParent Property to true for Form1.
When I launch the app, It loads Form2 as an MDI Child using
Form2 frm = new Form2();
frm.MdiParent = this;
frm.Show();
And that works fine. What I then want to do is click a button withing the 2nd form that will close Form2 and open up Form3 as a child form of Form1.
I tried
SecondForm SecondFormMDI = new SecondForm();
SecondFormMDI.MdiParent = Form1;
SecondFormMDI.Show();
on the button click event in Form2, but it would not work.
Do I have to always launch a Child form from the parent form? and if so, how would i go about doing that when it is on the button click event on a child form?
Just use this.MdiParent, instead of Form1, like
SecondForm SecondFormMDI = new SecondForm();
SecondFormMDI.MdiParent = this.MdiParent;
SecondFormMDI.Show();
You can set the MDIParent of any form in design time, why do it in run-time?
Simply set the value of MDIParent property of Form2 and Form3 to Form1, and that's it.
You could create a method in your MDIForm to open a childform:
public void OpenForm(Form form)
{
form.MdiParent = this;
form.Show();
}
When you want to open a new form in another form you do something like this (example in ChildFormOne with button):
private void btnOpenChildFormTwo_Click(object sender, EventArgs e)
{
((MDIForm)this.MdiParent).OpenForm(new ChildFormTwo());
this.Close();
}
Hope this helps.
ChildForm frmChild = new ChildForm();
frmChild.MdiParent = this.MdiParent;
frmChild.Dock = DockStyle.Fill();
frmChild.Show();

Opening a child form from another child form and set MDI to parent form - how to do?

I have a MDI form. within this MDI form I can open some child forms using:
This is within MainForm
Form1 f1 = new Form1;
f1.MdiParent = this; //this refers to MainForm (parent)
f1.Show();
This works as expected!
But Now, while I am in the child form (Form1 -> f1) I want to open another form as a child for MainForm but when I use this keyword it will reffer to f1. How can I open the new form within f1 and set its MdiParent to MainForm ?
Try assigning the parent form of your first child from:
Form2 f2 = new Form2;
f2.MdiParent = this.ParentForm; //this refers to f1's parent, the MainForm
f2.Show();
Hope this helps.
Let us suppose that the second form is f2.Then, the code in form f1 to create a new form f2 in MDI parent form will be:
Form2 f2 = new Form2;
f2.MdiParent = this.MdiParent;
f2.Show();
Well, not to argue with the "solution" that was listed... but if I'm understanding the request correctly and trying the above solution didnt work i would do the following....
Form2 f2 = new Form2();
f2.MdiParent = MDIParent1.ActiveForm;
f2.Show();
Let us suppose that the second form is frm2.Then, the code in form frm1 to create a new form frm2 in MDI parent form will be: create new object then again retrived data mdiparent forms solved freeze dispose form
Dim dru as New frm2 '// another form call
dru = New frm2
dru.mdiparent = frm1 '// main forms
dru.show()
I had the same problem and tried all different solutions. Finally the one that worked for me was:
Dim ChildForm As New AddingText("")
' Make it a child of this MDI form before showing it.
ChildForm.MdiParent = MDIParent1
ChildForm.Dock = DockStyle.Fill
MDIParent1.m_ChildFormNumber += 1
ChildForm.Text = "Client Existent" & MDIParent1.m_ChildFormNumber
ChildForm.Show()
the hiccup is that could not be used in conjunction with ShowDialog(), but i can live with it.

How to open a form within a form?

I have a Parent form and i like to open a child form within the the parent form.
Can this be done? If yes please reply me with sample code .
Thanks !
Following is the code to do what you want:
Assume that button1 is in the parent form.
private void button1_Click(object sender, EventArgs e)
{
this.IsMdiContainer = true;
Form Form2 = new Form();
Form2.MdiParent = this;
Form2.Show();
}
Also the following link will provide you more better details of what you want to do:
http://www.codeproject.com/KB/cs/mdiformstutorial.aspx
Hope this helps...
I note that all the answers here assume the OP intended to use MDI Form architecture, although that's never explicitly stated.
And there is another way a Form can be made a 'Child' of another Form: by simply setting its 'TopLevel property to 'False, and then setting its 'Parent property to the other Form.
Form2 f2 = new Form2();
f2.TopLevel = false;
f2.Parent = someOtherForm;
f2.Show();
By the way I think the whole idea of 'Forms within Forms' is a BAD idea, and MDI Architecture is now, justifiably, deprecated by MS.
Much better, I believe, to make secondary Forms 'Owned, and if you must have other Containers inside a Form, use UserControls, Panels, etc.
It depends on what you mean by "within the form". If you need to have the child form shown as a control of the parent form I guess you could try ParentForm.Controls.Add(new ChildForm()). Or maybe even place the child form in an existing container in the parent form by again using the containing control's Controls collection.
HTH
inform child form that its MdiParent is current form.
MDI:
form2 frm = new form2 ();
frm.MdiParent = this;
frm.Show();
Modal dialog:
var form = new Form1();
form.Parent = this;
form.ShowDialog();
MDI child:
var newMDIChild = new Form1();
newMDIChild.MdiParent = this;
newMDIChild.Show();
Form child = new Form();
child.MdiParent = this;
child.Show();
Write these lines of code in parent form and check.
var childform = new form2();
childform.TopLevel=false;
this.Controls.add(childform);
childform.Show();
This works for me.

Categories

Resources