vb.net loading a form dynamically by name

M

marfi95

I have a form that has a left and right panel. In the left panel is a
treeview. The right panel I want to change dynamically based on the
type of node selected. What I'm doing is loading the treeview nodes
through an XML file. As part of each node in the XML, I'm using an
attribute that indicates the name of a sub form to load. As part of
all these little child forms, the main control is a panel, which I
then assign its parent to the right panel of the main form. i.e.
frmSubForm.pnlSub.Parent = pnlBottomRight.

I have the tree loaded fine with no problems. I found some code using
reflection that would load the form as an object, but I haven't
figured out how to assign the panel fields parent because it doesn't
know the "type" of form.

Basically, I'm trying to load sub-forms into a panel on a main form
dynamically given the name of the form in a string.

Hope this makes sense. Is there an easier way to do what I'm trying.

Many thanks,
Mark
 
C

Cor Ligthert [MVP]

Mark,

First of all, know that trying a kind of 4th generation tool or let say
something as MS Access gives only disapointments to the one who is building
it. (Or you should be a team of 200 persons). The user wants more
possibilities as Access in all its forms.

If you don't want Midi forms, than maybe can usercontrols do a job for you
instead of forms.

Cor
 
P

Phill W.

marfi95 said:
I have a form that has a left and right panel. In the left panel is a
treeview. The right panel I want to change dynamically based on the
type of node selected.

I've done much the same in an application recently.
What I'm doing is loading the treeview nodes through an XML file.
As part of each node in the XML, I'm using an attribute that
indicates the name of a sub form to load.

OK, with you so far ...
As part of all these little child forms,

Here we part company. Your "editors" want to be UserControls, either
inherited from a base class or implementing an Interface either of which
defines how /every/ editor should behave.
the main control is a panel, which I then assign its parent to the right
panel of the main form.

Hacking Parentage of Controls can be messy, especially across multiple
Forms. UserControls are far easier to work with.
I have the tree loaded fine with no problems. I found some code using
reflection that would load the form as an object,

Object <> Variant.

You can't do much [at all] with an Object; you have to get (i.e. cast)
the loaded "Editor" into a Type that your "frame" application knows how
to deal with - either your Base class or Interface.
but I haven't figured out how to assign the panel fields parent
because it doesn't know the "type" of form.

The best you'll get out of the box is a "Forms.Form", which still isn't
particularly useful; you'd have to loop [recursively?] down through the
Controls collection to find the panel you want.

==========
Actually, I took this idea a little further. ;-)

From my file, I load a collection of Objects that
(a) can appear in various arrangements in the TreeView and
(b) can be edited by clicking on one of them (in the Tree).

Each of my my "EditableObjects" exposes a "GetTreeNode" function, which
returns a pre-formatted TreeNode (text, image, etc.) to pop into the
Tree anywhere I need it. This TreeNode has a property that points back
to the /original/ object.
This combination allows me to clear and rearrange the Tree in as many
different groupings as I need.

My "EditableObject" also exposes a "GetEditor" function, that returns a
UserControl (implementing my "IPluginEditor" Interface and) that
contains all the stuff I need to edit that /particular/
"EditableObject". All neatly self-contained.

The only code needed in the "frame" application is for the TreeView to
detect a Treenode from one of my "EditableObjects" (by its Type), get
the object itself (from the Property) and from that get it's "Editor"
and re-Parent that "Editor" into the right-hand panel.

More or less. :)

HTH,
Phill W.
 
M

marfi95

marfi95 said:
I have aformthat has a left and right panel. In the left panel is a
treeview. The right panel I want to change dynamically based on the
type of node selected.

I've done much the same in an application recently.
What I'm doing is loading the treeview nodes through an XML file.
As part of each node in the XML, I'm using an attribute that
indicates the name of a subformtoload.

OK, with you so far ...
As part of all these little child forms,

Here we part company. Your "editors" want to be UserControls, either
inherited from a base class or implementing an Interface either of which
defines how /every/ editor should behave.
the main control is a panel, which I then assign its parent to the right
panel of the mainform.

Hacking Parentage of Controls can be messy, especially across multiple
Forms. UserControls are far easier to work with.
I have the tree loaded fine with no problems. I found some code using
reflection that wouldloadtheformas an object,

Object <> Variant.

You can't do much [at all] with an Object; you have to get (i.e. cast)
the loaded "Editor" into a Type that your "frame" application knows how
to deal with - either your Base class or Interface.
but I haven't figured out how to assign the panel fields parent
because it doesn't know the "type" ofform.

The best you'll get out of the box is a "Forms.Form", which still isn't
particularly useful; you'd have to loop [recursively?] down through the
Controls collection to find the panel you want.

==========
Actually, I took this idea a little further. ;-)

From my file, Iloada collection of Objects that
(a) can appear in various arrangements in the TreeView and
(b) can be edited by clicking on one of them (in the Tree).

Each of my my "EditableObjects" exposes a "GetTreeNode" function, which
returns a pre-formatted TreeNode (text, image, etc.) to pop into the
Tree anywhere I need it. This TreeNode has a property that points back
to the /original/ object.
This combination allows me to clear and rearrange the Tree in as many
different groupings as I need.

My "EditableObject" also exposes a "GetEditor" function, that returns a
UserControl (implementing my "IPluginEditor" Interface and) that
contains all the stuff I need to edit that /particular/
"EditableObject". All neatly self-contained.

The only code needed in the "frame" application is for the TreeView to
detect a Treenode from one of my "EditableObjects" (by its Type), get
the object itself (from the Property) and from that get it's "Editor"
and re-Parent that "Editor" into the right-hand panel.

More or less. :)

HTH,
Phill W.


Thanks for the input. It sounds like I really need to be going the
direction of usercontrols. The main reason I was using sub forms was
I wanted a clean way of maintaining the UI for each of the sub forms
and not have to mess with hiding these xxx fields when this is
selected, making these visibile when xxx is selected, etc... That
makes for messy maintenance.

Are there any good sites you can point me to for using
usercontrols ? I assume by using usercontrols, I can still do this
dynamically based on the name of the control in a variable ?

Thanks !
Mark
 
P

Phill W.

marfi95 said:
Thanks for the input. It sounds like I really need to be going the
direction of usercontrols. The main reason I was using sub forms was
I wanted a clean way of maintaining the UI for each of the sub forms
and not have to mess with hiding these xxx fields when this is
selected, making these visibile when xxx is selected, etc... That
makes for messy maintenance.

Agreed!!
You can Design a UserControl in much the same way as you do a Form so,
each one stays neatly self-contained.
Are there any good sites you can point me to for using usercontrols?

'Fraid not - I usually muddle things together as I go along.
I assume by using usercontrols, I can still do this dynamically based
on the name of the control in a variable ?

Probably.
Your best bet might be to have a class that provides the "editors" and
call methods on that to get each one, something like

Class Editors
Public Function Type1Editor() As Type1Editor
Return New Type1Editor
End Function
End Class

....then...

Dim oEditors As New Editors
Dim oEditor As IEditor _
= CallByName( oEditors, "Type1Editor", vbMethod )

HTH,
Phill W.
 
K

Kevin S Gallagher

May be this might help (although several days have gone by). Of course it
needs tweaking since this is only for showing the idea. I did some of the
code while other parts are from one of the MVP's here.

Sub ShowForm(ByVal FormName As String)
Dim ProjectName As String =
Reflection.Assembly.GetExecutingAssembly.GetName.Name
Try
Dim tyOfStringVariable As Type = Type.GetType(ProjectName & "." &
FormName)
Dim frmObject As Object = Activator.CreateInstance(tyOfStringVariable)

DirectCast(frmObject, Form).StartPosition =
FormStartPosition.CenterParent
DirectCast(frmObject, Form).ShowDialog()
Catch ex As Exception
' TODO
End Try
End Sub
 

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