467,161 Members | 843 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,161 developers. It's quick & easy.

vb.net loading a form dynamically by name

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

Feb 2 '07 #1
  • viewed: 8090
Share:
5 Replies
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

"marfi95" <ma*****@gmail.comschreef in bericht
news:11**********************@j27g2000cwj.googlegr oups.com...
>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

Feb 2 '07 #2
marfi95 wrote:
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[s] 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.
Feb 2 '07 #3
On Feb 2, 6:54 am, "Phill W." <p-.-a-.-w-a-...@o-p-e-n-.-a-c-.-u-k>
wrote:
marfi95 wrote:
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[s] 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

Feb 2 '07 #4
marfi95 wrote:
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.
Feb 5 '07 #5

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.N ame
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

"marfi95" <ma*****@gmail.comwrote in message
news:11**********************@j27g2000cwj.googlegr oups.com...
>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

Feb 8 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Eric | last post: by
1 post views Thread by kanones | last post: by
2 posts views Thread by nemesis.saurabh@gmail.com | last post: by
5 posts views Thread by news.microsoft.com | last post: by
1 post views Thread by =?Utf-8?B?Qi4gQ2hlcm5pY2s=?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.