Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0 C#4.0
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 21-Jan-13 19:38pm
Comments
Sergey Alexandrovich Kryukov at 22-Jan-13 1:58am
   
What did you try? Just read MDSN help page on Form...
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

            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 :)
  Permalink  
Comments
Kishor Deshpande at 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 at 23-Jan-13 6:07am
   
thanks for ur coding ....... its working :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

private void button1_Click(object sender, EventArgs e)
        {
            int count = Application.OpenForms.Count;
            MessageBox.Show("" + count);
        }
  Permalink  
v3
Comments
Sergey Alexandrovich Kryukov at 22-Jan-13 1:56am
   
Will you fix the bug in last line (format string and message box parameter)?
—SA
Kishor Deshpande at 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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

THANKS FOR UR CODING Smile | :)
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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Try with code written below:
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()));
  Permalink  
v4
Comments
Brinda Arumugam at 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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

You can determine the number of active (visible) Forms like this:
private int CountActiveForms()
 {
     int cnt = 0;
 
     foreach (Form theForm in Application.OpenForms) if (theForm.Visible) cnt++;
 
     return cnt;
 }
Try this simple test:
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());
}
  Permalink  

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

  Print Answers RSS
0 CHill60 360
1 Sarvesh Kumar Gupta 238
2 OriginalGriff 218
3 Sergey Alexandrovich Kryukov 153
4 DamithSL 153


Advertise | Privacy | Mobile
Web01 | 2.8.140709.1 | Last Updated 29 Mar 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid