Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
Dear Sir/Madam ,

i have little doubt in c# windows application. i Created 3 Forms in my application.

1.Login
2.Reg
3.Display

while run the program i want get active form count .

for ex :
i will open login form . The count will display 1. and i will open second form before close first form the count will display 2.

can u any one help me !
Posted
Comments
Sergey Alexandrovich Kryukov 22-Jan-13 1:58am    
What did you try? Just read MDSN help page on Form...
—SA

C#
int count = 0;
for (int i = 0; i < Application.OpenForms.Count; i++)
{
    if (Application.OpenForms[i].Visible == true)
        count++;
}
label1.Text = count.ToString(); // Active Forms value :)
 
Share this answer
 
Comments
Kishor Deshpande 23-Jan-13 1:36am    
This solution worked, try this :)
Can you please check if it works for you, if it does, can you please upvote my answer?
Brinda Arumugam 23-Jan-13 6:07am    
thanks for ur coding ....... its working :)
C#
private void button1_Click(object sender, EventArgs e)
        {
            int count = Application.OpenForms.Count;
            MessageBox.Show("" + count);
        }
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 22-Jan-13 1:56am    
Will you fix the bug in last line (format string and message box parameter)?
—SA
Kishor Deshpande 22-Jan-13 1:59am    
MessageBox.Show(string.Format("No. of open Forms={0}",Application.OpenForms.Count.ToString()));
This will do I guess.
Brinda Arumugam 22-Jan-13 4:35am    
THANKS FOR UR CODING :)
I GOT ANSWER BUT THAT COUNT WITH HIDDEN FORMS ALSO. BUT I NEED ONLY ACTIVE FORMS COUNT.
CAN U PLZ TELL ME FOR THIS PROB.
THANKS FOR UR CODING :)
I GOT ANSWER BUT THAT COUNT WITH HIDDEN FORMS ALSO. BUT I NEED ONLY ACTIVE FORMS COUNT.
CAN U PLZ TELL ME FOR THIS PROB.
 
Share this answer
 
Try with code written below:
C#
int count=0;
for(int i=0; i<Application.OpenForms.Count;i++)
{
    if(Application.OpenForms[i].Visible==true)//will not count hidden forms
       count++;
}
MessageBox.Show(string.Format("Active Forms: {0}", count.ToString()));
 
Share this answer
 
v4
Comments
Brinda Arumugam 22-Jan-13 23:30pm    
thanks for ur coding kishor deshpande :)

in that coding

Application.OpenForms.Item - Not available
[i'm using c#3.5 version]

can u plz help me
Kishor Deshpande 23-Jan-13 1:30am    
Hi Brinda, I've modified Application.OpenForms.Item to Application.OpenForms[i], It's working fine for me as I've tested it on my system.
You can determine the number of active (visible) Forms like this:
C#
private int CountActiveForms()
 {
     int cnt = 0;

     foreach (Form theForm in Application.OpenForms) if (theForm.Visible) cnt++;

     return cnt;
 }
Try this simple test:
C#
private void button1_Click(object sender, EventArgs e)
{
    Form f1 = new Form();
    f1.Show();
    Form f2 = new Form();
    f2.Show();
    Form f3 = new Form();
    f3.Show();
    Form f4 = new Form();
    f4.Show();

    Console.WriteLine("Forms: Open: " + Application.OpenForms.Count.ToString() + " Active: " + CountActiveForms().ToString());

    f1.Hide();
    f4.Hide();

    Console.WriteLine("Forms: Open: " + Application.OpenForms.Count.ToString() + " Active: " + CountActiveForms().ToString());
}
 
Share this answer
 

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