elmbrook
asked on
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.GetExecutingAssem bly().GetT ype(_proje ctname + ".Forms." + _formname);
if (myForm != null)
{
myobject = Activator.CreateInstance(m yForm);
//PROBLEM HERE
Form checkform = myglobal_methods.IsFormAlr eadyOpen(t ypeof((For m)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
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.GetExecutingAssem
if (myForm != null)
{
myobject = Activator.CreateInstance(m
//PROBLEM HERE
Form checkform = myglobal_methods.IsFormAlr
((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
ASKER
Ho you suppose to do that?
I already do that
//PROBLEM HERE
Form checkform = myglobal_methods.IsFormAlr eadyOpen(t ypeof((myo bject ));
not working
I already do that
//PROBLEM HERE
Form checkform = myglobal_methods.IsFormAlr
not working
You are sending typeof(myobject) instead send myobject itself
Form checkform = myglobal_methods.IsFormAlr eadyOpen(m yobject);
public static Form IsFormAlreadyOpen(ObjectHa ndle FormType)
{
ObjectHandle ha = null;
foreach (Form OpenForm in Application.OpenForms)
{
ha = new ObjectHandle(OpenForm.Hand le);
if (ha.Equals(FormType))
return OpenForm;
}
return null;
}
Form checkform = myglobal_methods.IsFormAlr
public static Form IsFormAlreadyOpen(ObjectHa
{
ObjectHandle ha = null;
foreach (Form OpenForm in Application.OpenForms)
{
ha = new ObjectHandle(OpenForm.Hand
if (ha.Equals(FormType))
return OpenForm;
}
return null;
}
ASKER
Hi got an error here saying that
cannot convert from object to system.runtime.remoting.ob jecthandle
cannot convert from object to system.runtime.remoting.ob
replace object myobject = new object(); with ObjectHandle myobject = null;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
In IsFormAlreadyOpen iterate thru each open form and create an instance of ObjectHandle from openform.Handle and compare the objects