Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Friends

How to check how many form is active in an application? and how to close any one form if multiple forms are open? please help.
Posted
Updated 12-Apr-11 1:12am
v2
Comments
Henry Minute 12-Apr-11 7:58am    
Can you please make it clear if this is for an MDI application or an SDI application.

Hey there is Active form Property present to get active form
like
Form.ActiveForm


And if you want to close this form then just called close method of it.

Form.ActiveForm.close()


Try its.
its just suggestion!
 
Share this answer
 
Comments
Antaru 12-Apr-11 7:40am    
thanks for your suggestion
i was try this method but main form is close with another from
i want close only particular child form
If this is a standard WinForms app (as opposed to an MDI app), there is only one active form unless you have modeless forms open, and that form is the one you're looking at.

Now, if you;'re talking about how many forms are currently *open*, then you have to set the parent property in each form as it is open, which means you have to pass the parent form to the child (I'd do that in the constructor). Once you've done that, you can travers the forms backwards from the current form by cascading each Parent property until you read one that doesn't have a parent specified.

It's kind pointless to want to know the count, but that's how I'd do it.
 
Share this answer
 
Comments
Antaru 12-Apr-11 8:19am    
thank you john i got a solution
try this
Form.ActiveForm.ActiveMdiChild

and also as santosh said
Form.ActiveForm.ActiveMdiChild.Close();

I hope I helped
:-)
 
Share this answer
 
Comments
Henry Minute 12-Apr-11 7:56am    
You could have the answer but the OP has not stated anywhere that their app is an MDI app. You should really ask them first to get confirmation before suggesting an answer that is so specific.
a1mimo 12-Apr-11 8:40am    
you are right it just hit me when I saw "child" that its an mdi app :-)

my bad
Antaru 12-Apr-11 8:18am    
thanks i got a solution
List<form> forms = new List<form>();
 private void btnForm1_Click(object sender, EventArgs e)
        {
            Form1 frm1 = new Form1(this);
            forms.Add(frm1);
            frm1.Show();
        }
        
        private void btnForm2_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2(this);
            forms.Add(frm2);
            frm2.Show();
        }

        
        private void CloseForms(String FormText)
        {
            int index = -1;
            for (int i = 0; i <= forms.Count - 1; i++)
            {
                if (forms[i].Text ==FormText.Trim())
                {
                    index = i;
                    break;
                }

            }
            if (index != -1)
            {
                Form form = forms[index];
                form.Close();
                forms.RemoveAt(index);
            }
        }

        private void btnForm1Close_Click(object sender, EventArgs e)
        {
            CloseForms("Form1");
        }


        private void btnForm2Close_Click(object sender, EventArgs e)
        {
            
            CloseForms("Form2");
        }
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900