Click here to Skip to main content
15,911,360 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
private void changePasswordToolStripMenuItem_Click(object sender, EventArgs e)
     {
         if (Application.OpenForms.OfType<frmChangePassword>().Count() >= 1)
         {
             Application.OpenForms.OfType<frmChangePassword>().First().Activate();
         }
         else
         {
             frmChangePassword d = new frmChangePassword();
             d.MdiParent = this;
             d.Show();
         }
     }

this is code i am using for creating object of a form and calling it
and i want it as..
C#
private void changePasswordToolStripMenuItem_Click(object sender, EventArgs e)
       {
         OpenForm(frmChangePassword);
       }
 private void OpenForm(Form frm)
       {

            if (Application.OpenForms.OfType<frm>().Count() >= 1)
            {
                Application.OpenForms.OfType<frm>().First().Activate();
            }
            else
            {
                frm = new frm();
                frm.MdiParent = this;
                frm.Show();
            }

       }

something like this but it gives me an Error ..
frmChangePassword' is a 'type' but is used like a 'variable'
Posted
Updated 5-Jul-15 20:02pm
v2

1 solution

You can't pass the type of a form to a method expecting an instance; and you can't use an instance to create a new instance: your code is confused as to what exactly it is trying to do.
And even if you could, it wouldn't be a lot of use as the instance you create is discarded as soon as code exits the code block of the else condition, and is no longer directly accessible to the application.

It looks like you are trying to ensure that you only get a single instance of the passowrd form.

The way I would do it is a lot simpler: keep a class level variable which refers to the existing instance of the frmChangePassword class. Initially null, when you go to use it, you check for that. If not null, activate it.
If it is null, create a new instance, assign it to the variable, and add a handler to it's FormClosed event. In the event handler, you set the variable back to null.
 
Share this answer
 
Comments
Madhuri Gamane 6-Jul-15 2:48am    
in my another i have used same solution, coz i was having only 7-8 forms, but in this case i am having more than 50 forms, so will it feasible to declare all form's variable as Global ??
OriginalGriff 6-Jul-15 2:53am    
Yes, just use an array and constant indexes.

But...isn't 50 forms open in a single MDI app going to be a little...um...confusing...for the user? Not to mention take a lot of screen real estate?
Have you considered tabs and grouping "logical" forms in a similar way to how VS does it? Might be easier for your users, and easier to manage as well.
Madhuri Gamane 6-Jul-15 4:10am    
no actually having three authentication as Accountant, supervisor and many users.
I will try for using array..
thanx :-)

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