all groups > dotnet windows forms > april 2007 >
You're in the

dotnet windows forms

group:

Change UserControl on Form at run-time (VB)


Change UserControl on Form at run-time (VB) Nick Locke
4/28/2007 12:00:00 AM
dotnet windows forms:
Apologies if this is easy and I'm missing something!

I have a collection of User Controls (all inherited from the same root). I
have a form on which one of the controls is the user control ancestor (which
is great at design-time because I get all the Intellisense and so on).

However, at run time, I need to programatically "tell" the form to load in
one of the concrete User Controls (a different one each time), rather than
the ancestor.

So, effectively, I am looking for a piece of VB code which will allow me to
specify the Class Name of a control to load into a "placeholder" control.

How do I code that please?

Thanks,

Nick

Re: Change UserControl on Form at run-time (VB) Bryan Phillips
4/29/2007 12:00:17 AM
How does this work for you?

// I am assuming the class is in the same project as the form.
Dim t As Type = Me.GetType().Assembly.GetType("Namespace.ClassName");

// Get the default constructor of the class.
Dim uc As UserControl = t.GetConstructors()[0].Invoke(null);

// Add the control to the form.
Me.Controls.Add(uc);


--
Bryan Phillips
MCSD, MCDBA, MCSE
Blog: http://bphillips76.spaces.live.com
Web Site: http://www.composablesystems.net



[quoted text, click to view]
Re: Change UserControl on Form at run-time (VB) Nick Locke
4/29/2007 6:12:24 PM

[quoted text, click to view]

Bryan, thanks. This looks like it will do the job for me, except that I am
coming unstuck on the second line. VB does not like the
GetConstructors()[0].Invoke(null) construct. After the (), intellisense
does not offer me Invoke. After [0], intellisense just evaporates
altogether.

I have tried splitting it up to create an array of ConstructorInfo objects
first and then index into that. Doing it that way lets me do an Invoke (it
also dislikes the null):

Dim ci() As Reflection.ConstructorInfo
ci = t.GetConstructors()
Dim uc As UserControl = ci(0).Invoke(New Object)

but that falls over at run-time, telling me to use "new" when defining the
array (which of course can't be done).

I'm probably missing something very silly now I suspect - and would
appreciate what will hopefully be the last gentle prod towards success!

Thanks.

Re: Change UserControl on Form at run-time (VB) Bryan Phillips
4/29/2007 6:24:08 PM
Sorry. The [] are used in C# just use () instead:

Dim uc As UserControl = t.GetConstructors()(0).Invoke(null)

And delete the semi-colons too.



--
Bryan Phillips
MCSD, MCDBA, MCSE
Blog: http://bphillips76.spaces.live.com
Web Site: http://www.composablesystems.net



[quoted text, click to view]
Re: Change UserControl on Form at run-time (VB) Nick Locke
4/29/2007 9:08:26 PM

[quoted text, click to view]

No need to be sorry - I should have been able to work that one out! Just to
complete the thread, this is what I ended up with:

Dim t As Type = Me.GetType().Assembly.GetType("Whatever.TestTest")
Dim params() As Object

Dim uc As UserControl = t.GetConstructors()(0).Invoke(params)

Me.Controls.Add(uc)

Many thanks for your help, Nick.






AddThis Social Bookmark Button