Open Form from Name #3

  • Thread starter Thread starter vbdev
  • Start date Start date
V

vbdev

Hi,

This is my 3rd post on the issue, but I am getting closer to a solution:

I have 1 project with 50 winforms name test01 to test50. These forms have a
public method named: MyVal

Right now I have the following code (where fName holds the form name):

string st = "init";
if (fName == "Test01")

{

Test01 myForm = new Test01();

myForm.MyVal = st;

myForm.ShowDialog();

st = myForm.MyVal;

}

else if (fName == "Test02")

{

Test02 myForm = new Test02();

myForm.MyVal = st;

myForm.ShowDialog();

st = myForm.MyVal;

}

..... all the way the Test50!!!!!!!!!!!!!!!!!!

This looks stupid! that's 49 elseif!

I am trying to make one generic call where I am feeding only the form name:
TestXX as a string:

If I have a var fName that holds my current form name, I would like to have

something(fName) myForm = new something(fName)

myForm.MyVal = st;

myForm.ShowDialog();

st = myForm.MyVal;

Any idea?



Thanks

vbdev
 
vbdev said:
Hi,

This is my 3rd post on the issue, but I am getting closer to a solution:

I have 1 project with 50 winforms name test01 to test50. These forms have a
public method named: MyVal

Right now I have the following code (where fName holds the form name):

string st = "init";
if (fName == "Test01")

{

Test01 myForm = new Test01();

myForm.MyVal = st;

myForm.ShowDialog();

st = myForm.MyVal;

}

else if (fName == "Test02")

{

Test02 myForm = new Test02();

myForm.MyVal = st;

myForm.ShowDialog();

st = myForm.MyVal;

}

.... all the way the Test50!!!!!!!!!!!!!!!!!!

This looks stupid! that's 49 elseif!

I am trying to make one generic call where I am feeding only the form name:
TestXX as a string:

If I have a var fName that holds my current form name, I would like to have

something(fName) myForm = new something(fName)

myForm.MyVal = st;

myForm.ShowDialog();

st = myForm.MyVal;

Any idea?



Thanks

vbdev
You should be able to do it using reflection.
Not quite as fast though, but you probably wont notice it.
You might need to define an interface that exposes MyVal as well.

Cheers
JB
 
Yes, I sure that I could. But after wasting hours, and going nowhere, the
question is how?

Thanks

vbdev
 
vbdev said:
Yes, I sure that I could. But after wasting hours, and going nowhere, the
question is how?

Assembly ThisAssembly = Assembly.GetExecutingAssembly();
Form ActivatedForm =
(Form)ThisAssembly.CreateInstance(<NamespaceQualifiedClassName>);
((IMyVal)ActivatedForm).Value = 10;
ActivatedForm.Show();

JB
<snip>
 
vbdev said:
I have 1 project with 50 winforms name test01 to test50. These forms have
a public method named: MyVal

I am trying to make one generic call where I am feeding only the form
name: TestXX as a string:

If I have a var fName that holds my current form name, I would like to
have

something(fName) myForm = new something(fName)
myForm.MyVal = st;
myForm.ShowDialog();
st = myForm.MyVal;

Any idea?


Hi. You should create an interface for the method. For example:

public interface IFoo {
string MyVal();
}



Then in your window class, implement the interface:

public class Test01 : System.Windows.Forms.Form, IFoo {
// Other normal window stuff in the class.

// Implement IFoo.MyVal
string IFoo.MyVal() {
return "xyz"; // Whatever
}
}



Then in your consumer:

// Instantiate the window by name.
System.Runtime.Remoting.ObjectHandle Obj =
System.Activator.CreateInstanceFrom("MyAssembly", "Test01");
object c = Obj.Unwrap();

// Show the window. This works regardless of the window type instantiated.
((System.Windows.Forms.Form) c).ShowDialog();

// Fetch value. This works regardless of which window type is instantiated.
string st = ((IFoo) c).MyVal();

// Important! Dispose the form.
((IDisposable) c).Dispose();


I typed this all from memory, so there may be a few typos. But it should be
generally correct.

-- Alan
 
vbdev said:
Hi,

This is my 3rd post on the issue, but I am getting closer to a solution:

I have 1 project with 50 winforms name test01 to test50. These forms have
a public method named: MyVal

Right now I have the following code (where fName holds the form name):

string st = "init";
if (fName == "Test01")

{

Test01 myForm = new Test01();

myForm.MyVal = st;

myForm.ShowDialog();

st = myForm.MyVal;

}

else if (fName == "Test02")

{

Test02 myForm = new Test02();

myForm.MyVal = st;

myForm.ShowDialog();

st = myForm.MyVal;

}

.... all the way the Test50!!!!!!!!!!!!!!!!!!

This looks stupid! that's 49 elseif!

I am trying to make one generic call where I am feeding only the form
name: TestXX as a string:

If I have a var fName that holds my current form name, I would like to
have

something(fName) myForm = new something(fName)

myForm.MyVal = st;

myForm.ShowDialog();

st = myForm.MyVal;

Any idea?



Thanks

vbdev

First i would try to use polymorphy in the right way:

BaseForm myForm;
string st = "init";
switch (fName)
{
case "Test01":
myForm = new Text01();
break;
case "Test02";
myForm = new Test02();
break;
........
}
myForm.MyVal = st;

myForm.ShowDialog();

st = myForm.MyVal;

BaseForm would be a Class deriving from System.Windows.Forms.Form, and
containing the member MyVal.
All ypur Forms should derive from BaseForm.

Christof
 
Christof Nordiek said:
First i would try to use polymorphy in the right way:

BaseForm would be a Class deriving from System.Windows.Forms.Form, and
containing the member MyVal.
All ypur Forms should derive from BaseForm.

What you have will work, but it's better suited for std C++, which lacks
reflection and interfaces.

The code I have posted is better for at least 2 reasons:
- It uses dynamic class instantiation to avoid large switch statements,
- It uses an interface, which is superior to your inheritance model.

-- Alan
 
Back
Top