string value to new form instance?

P

Paul

Hi,

Suppose you had a listbox1 with 3 items;
Form1
Form2
Form3

Now add an event handler for listbox1.selectedindexchanged in which you wish
to create a new form instance depending on the value of the
SelectedItem.ToString value. i.e. If "Form3" is clicked then a new instance
of Form3 is created.

Assuming your project contains a windows form called "Form3" - how is this
achieved?

Thanks in advance,

Paul
 
B

BK

One way is through reflection:

Dim ExternalAssembly As System.Reflection.Assembly = _
System.Reflection.Assembly.LoadFrom(Container)
Dim CalledForm As Form = _
ExternalAssembly.CreateInstance(FullName, True)
CalledForm.FriendlyFormName = FullName
CalledForm.Show()

Where:
Container is the name of the executable or dll containing the form (eg.
"MyApp.Exe")
FullName is the qualified name of the form (eg "MyApp.Form1")

HTH
 
C

Chris

Paul said:
Hi,

Suppose you had a listbox1 with 3 items;
Form1
Form2
Form3

Now add an event handler for listbox1.selectedindexchanged in which you wish
to create a new form instance depending on the value of the
SelectedItem.ToString value. i.e. If "Form3" is clicked then a new instance
of Form3 is created.

Assuming your project contains a windows form called "Form3" - how is this
achieved?

Thanks in advance,

Paul

Simpliest way:

in selectedindexchanged event:

sub ...
select case listbox1.selectedindex
case 0
open form 1
case 1
open form 2
case else
Nothing Selected
end select
end sub


Now if you want to do it dynamically you are going to need to do this
through reflection. You'll need to do a search like:

http://www.google.com/search?hl=en&...ass+from+string+vb.net+reflection&btnG=Search
 
P

Paul

Thanks BK that's exactly the kind of thing I was looking for! Just one Q
.....

Having explored the assembly object for a while I still can't figure out how
to find out through code the "Container" value you need in the LoadFrom
method. For example, the Container value has a value of "XXX.exe" but the
"FullName" needs a value of "XXX.Form1". I'm sure the GetExecutingAssembly
method will prove useful but I'm struggling to pick out the correct way to
do this.

E.g. Let's say you have an class that is responsible for delivering form
objects (dressed up with nice titles and styles etc) based on a string value
equal to the form object name supplied to the constructor. This class could
be called from anywhere in the solution.

Thanks in advance,

Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top