Click here to Skip to main content
12,444,517 members (51,161 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#3.0 C# 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
   
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 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 :)
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
   
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.
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 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.
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
Top Experts
Last 24hrsThis month


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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100