Hiding all open forms - c#

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

Related

C# Win Forms not opening

I have a WinForm listbox selection of a list of forms that the user can go into.
I have a function that should open the new form
private void sendToDB_Button_Click(object sender, EventArgs e)
{
string selected_item = listBox1.SelectedItem.ToString();
Form _secondForm = new Form();
if (selected_item == "389")
_secondForm = new Forms._389_Form();
else if ( selected_item == "120" )
_secondForm = new Forms._120_Form();
//... Repeat for 30 more forms ...
this.Hide();
_secondForm.Show();
}
When running the application and I select "389" the current form closes as it should but nothing is opened in a new form. Curious if having the forms in a folder called Forms is the issue here. The line in question is _secondForm = new Forms._389_Form(); and is this breaking the application?
Instead of _secondForm.Show(); I changed it to _secondForm.ShowDialog(); and I got the expected results.
According to this link: https://stackoverflow.com/a/20859112/8149159 , The author of the answer states that:
The Show function shows the form in a non modal form. This means that you can click on the parent form.
ShowDialog shows the form modally, meaning you cannot go to the parent form
try
this.Hide();
_secondForm.ShowDialog();
//if you want close precedent form
this.Close();
Hide is for "hide" all action at the user
ShowDialog is for open the form and
Close is for close the precedent form

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

Keep form on top and open and close other forms

I'm new to C# and could use some help.
What I have so far is a set of 8 windows forms I have created in C#, these forms have basic things like text boxes, labels, radio buttons, etc. Now that I have completed making all of these forms I want to have one additional form (called the Selector form) I can use to select one of the other 8 forms. At any given time, I want the Selector form to be on top of other windows and it will have 8 radio buttons (or regular buttons, doesn't matter). When one of the buttons is clicked, the current form (not the Selector form) should disappear and a new form should appear. The name of the button will be the name of the new form that appears.
I have seen a few examples and here is the code I have so far:
void Button1Click(object sender, EventArgs e)
{
//this.Hide();
var form1 = new CASII();
form1.Closed += (sender1, args) => this.Close();
form1.Show();
}
void Button2Click(object sender, EventArgs e)
{
// this.Hide();
var form2 = new CCARAdmin();
form2.Closed += (sender1, args) => this.Close();
form2.Show();
//Application.Run(new CCARAdmin());
}
Problem I am having is I don't want to hide the Selector form, which this does, and I don't know how to identify the other form that is open to close it and then open a different form.
From starting the program the logic would be like this:
Show Selector form
When a button is clicked on the Selector form, keep the Selector form on top and show the other form with the name of the button.
When a different button is clicked on the Selector form, close the previous form that was open (not the Selector form) and open the new form corresponding to the name of the button. Keep the Selector form on top.
When the Selector form is close, application stops.
Problem I am having is I don't want to hide the Selector form, which
this does, and I don't know how to identify the other form that is
open to close it and then open a different form.
Set Selector form TopMost to True to make it always on top. Or
you can use BringToFront after opening a new form
to know other forms that are open check this answer. Or you can define each From as a field in the Selector form, and check that.
selectorForm.TopMost = true ( this will help to keep the selector form always on top).
Create a form variable in your selector form to keep the reference of your currently opened form.
Sample code for 1 button click :
Form frm = null;
void Button1Click(object sender, EventArgs e)
{
//this.Hide();
var form1 = new CASII();
if (frm == null)
{
frm = form1;
}
else
{
frm.Close();
}
form1.Show();
this.TopMost = true;
frm = form1;
}
I resolved this by setting TopMost to true and then using the following code under each one of the buttons:
for (int i = Application.OpenForms.Count - 1; i >= 0; i--)
{
if (Application.OpenForms[i].Name != "FormSelector")
Application.OpenForms[i].Close();
}
var form = new TRAC();
if (radioButton9.Checked == true)
{
form.Show();
}

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.

Close child form if already open

How can I go about closing a Child form if it's already open? And if it isn't already open, then open it?
Thank you
I already have this code, which doesn't work, obviously:
Form ibx = new inbox();
if(ibx.Visible)
ibx.Dispose();
else
ibx.Show();
All that the above does is creates a new form whether or not it's already open.
private Form frm;
public void ToggleForm() {
if(frm == null) {
frm = new Form();
frm.Show();
}
else {
frm.Close();
frm = null;
}
}
If what you want to achieve is opening a form only if it isn't already open, then bring it on front when it is, then the following code might help.
private void tsmiMenuOption_Click(object sender, EventArgs e) {
// Assuming this method is part of an MDI form.
foreach(Form child in this.MdiChildren)
if (child.Name == MyForm.Name) {
child.BringToFront();
return;
}
MyForm f = new MyForm();
f.MdiParent = this;
f.Show();
}
So, this is untested pseudo-c#-code that verifies if MyForm is already opened and contained in the MdiContainer.Children. If it is, then it brings this form (MyForm) to the front. If it is not, then it simply instantiate and opens it.
Is this about what you want?
In your main form, when you create the first childform, keep a reference to it. Currently you're creating a new childform every time you run that code and then you check if the newly created form is visible.
Also be aware that your subject talk about opening and closing but your code seems to just be dealing with hiding and showing.
Carra's code is a good sample how to do it, but be careful if the childform can be closed from anywhere else.
You need to keep a reference to ibx. Your code creates a new inbox everytime it's run.
Module Module1
Public Function InstanceNewForm(ByRef ParentForm As Form, ByRef Childform As Form) As Boolean
Dim bOpen As Boolean = False
Dim frm As Form
For Each frm In ParentForm.MdiChildren
If Childform.Name = frm.Name Then
Childform.Focus()
bOpen = True
Exit For
End If
Next
If Not bOpen Then
With Childform
.StartPosition = FormStartPosition.CenterScreen
.MdiParent = Parentform
.Show()
End With
End If
frm = Nothing
Return bOpen
End Function
End Module
The above code will check to see if a mdi form is already loaded in the parent container and if its already active will set the focus to it. Otherwise it will create the mdi form.
Just call it from anywhere the mdi form needs to be loaded. ex: call InstanceNewForm(me,form2)
Works like a charm everytime!!

Categories

Resources