Change UserControl on Form at run-time (VB)

  • Thread starter Thread starter Nick Locke
  • Start date Start date
N

Nick Locke

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
 
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 said:
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, 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.
 
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



Bryan said:
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, 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.
 
Bryan said:
Sorry. The [] are used in C# just use () instead:

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

And delete the semi-colons too.

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.
 
Back
Top