open form from string form name and check if the form is already opened or not c#

Hi,

I am trying to open a form from string form name
and it works
then i want to check if the form is already opened or not

this is my button click event

_projectname = "myCollections";
_formname = "myGames";

object myobject = new object();
Type myForm = Assembly.GetExecutingAssembly().GetType(_projectname + ".Forms." + _formname);

if (myForm != null)
{
     myobject = Activator.CreateInstance(myForm);

     //PROBLEM HERE
     Form checkform = myglobal_methods.IsFormAlreadyOpen(typeof((Form)myobject)));

     ((Form)myobject).Show();
}

then i have a method to check if the form is already opened

public static Form IsFormAlreadyOpen(Type FormType)
{
     foreach (Form OpenForm in Application.OpenForms)
     {
          if (OpenForm.GetType() == FormType)
          return OpenForm;
      }
     return null;
}  

the problem is that (Form)myobject is not a Form.

How i suppose to handle this?


Thank you
elmbrookAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SriVaddadiCommented:
Pass the ObjectHandle instance you get from Activator.GetInstance method to IsFormAlreadyOpen.
In IsFormAlreadyOpen iterate thru each open form and create an instance of ObjectHandle from openform.Handle and compare the objects
0
elmbrookAuthor Commented:
Ho you suppose to do that?
I already do that

//PROBLEM HERE
Form checkform = myglobal_methods.IsFormAlreadyOpen(typeof((myobject ));

not working
0
SriVaddadiCommented:
You are sending typeof(myobject) instead send myobject itself
Form checkform = myglobal_methods.IsFormAlreadyOpen(myobject);
public static Form IsFormAlreadyOpen(ObjectHandle FormType)
{
     ObjectHandle ha = null;
     foreach (Form OpenForm in Application.OpenForms)
     {
          ha = new ObjectHandle(OpenForm.Handle);
         if (ha.Equals(FormType))
          return OpenForm;
      }
     return null;
}  
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

elmbrookAuthor Commented:
Hi got an error here saying that

cannot convert from object to system.runtime.remoting.objecthandle
0
SriVaddadiCommented:
replace object myobject = new object(); with ObjectHandle myobject = null;
0
japeteCommented:
You have to fix your code this way:
_projectname = "myCollections";
_formname = "myGames";

object myobject = new object();
Type myForm = Assembly.GetExecutingAssembly().GetType(_projectname + ".Forms." + _formname);

if (myForm != null)
{
     myobject = Activator.CreateInstance(myForm);

     //PROBLEM HERE
     Form checkform = myglobal_methods.IsFormAlreadyOpen(myForm);

     ((Form)myobject).Show();
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SriVaddadiCommented:
Here is the snippet. Try this out and keep posted. Also if your question is resolved then please mark it as solution.

_projectname = "myCollections";
_formname = "myGames";

object myobject = null;
Type myForm = Assembly.GetExecutingAssembly().GetType(_projectname + ".Forms." + _formname);

if (myForm != null)
{
     myobject = Activator.CreateInstance(myForm);

     //PROBLEM HERE
     Form checkform = myglobal_methods.IsFormAlreadyOpen(myobject);

     checkform.Show();

}

then i have a method to check if the form is already opened

public static Form IsFormAlreadyOpen(ObjectHandle FormType)
{
     ObjectHandle ha = null;
     foreach (Form OpenForm in Application.OpenForms)
     {
          ha = new ObjectHandle(OpenForm.Handle);
         if (ha.Equals(FormType))
          return OpenForm;
      }
     return null;
}  
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.