• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 726
  • Last Modified:

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
0
elmbrook
Asked:
elmbrook
  • 4
  • 2
2 Solutions
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
 
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now