Switching between Forms in Winforms - c#

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

Related

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

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

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;

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.

Hiding all open forms

I have three forms.
Lets say A, B, C.
Form A opens form B and form B then opens form C.
I have added button Hide all open forms in form C.
Now how do I hide all three forms with this button ?
I know one way is using ShowWindow Api, but I don't want to use Api calls.
Edit : Thanks to SoMoS.
for (int i = Application.OpenForms.Count - 1; i >= 0; i += -1)
{
if (!object.ReferenceEquals(Application.OpenForms[i], this))
{
Application.OpenForms[i].Hide();
}
}
this.Hide();
Or
In form A (thanks to ho1)
B frm = new B();
frm.Owner = this;
frm.Show();
In form B
C frm = new C();
frm.Owner = this;
frm.Show();
In form C's button click event.
Owner.Owner.Hide();
Owner.Hide();
Hide();
Or thanks to Wim Coenen
foreach (Form var in Application.OpenForms)
{
var.Hide();
}
Thanks.
You just need to access this collection:
Application.OpenForms
Then you just need to iterate over all the items and hide the ones you want (you can check by title for example) or just hide all of them.
Hope it helps.
This works:
Owner.Owner.Hide();
Owner.Hide();
Hide();
Or if you are uncertain how many forms there will be in the chain you could just have a recursive method.
Though this depends on A being the owner of B etc, which you can arrange by sending this in as the parameter to the calls to Show when you show the forms.
Instead of hiding all forms, you could use the fact that minimizing a form, automatically minimizes all it's child forms. So once C.Owner = B, B.Owner = A, you could just use (in your Click handler in A):
WindowState = FormWindowState.Minimized
I was looking for the refresh form and performclick, before it should be close form, it helps to me aswell
void formcheck()
{
foreach (Form var in System.Windows.Forms.Application.OpenForms.OfType<Main>())
{
var.Hide();
}
Main notify = new Main();
notify.Show();
notify.btn_notify.PerformClick();
}
Thanks.
Form2 NewForm = new Form2();
this.Hide(); //Hide Current form
NewForm..ShowDialog(); //Show new form
this.Show(); //Show Previous form After close new form

Categories

Resources