Setting an object to a UserControl

G

Guest

If I have a name of a User Control in my project, how can I set an Object to
that User Control?

ie;

I have a User Control, called "Accounts" in my project
I tried the following code

strUserControlName = "Accounts"
frmMain.Controls.Add(strUserControlName)

But, of course, the above code doesn't work, because the second line of code
wants a User Control parameter, not a string parameter.

so, I need to do something like this:

dim objUserControl as UserControl
set objUserControl = UserControl(strUserControlName)

How do I do this? Your help would be appreciated.
Dave
 
H

Herfried K. Wagner [MVP]

Dave said:
If I have a name of a User Control in my project, how can I set an Object
to
that User Control?

ie;

I have a User Control, called "Accounts" in my project
I tried the following code

strUserControlName = "Accounts"
frmMain.Controls.Add(strUserControlName)

But, of course, the above code doesn't work, because the second line of
code
wants a User Control parameter, not a string parameter.

Check out the code snippets at
<URL:http://dotnet.mvps.org/dotnet/code/techniques/#ClassByName>.
 
G

Guest

Herfried K. Wagner said:
Herfried;
I don't think that answers my question, at least as far as I can tell, not
being able to read German. The examples on your page seem to be detailing
creating new objects.

I've already got the UserControls created. They are in my project already. I
need to pick one, based on a user request, add it to the controls in my
current form and display the form.

I've already got ucAccounts.vb, ucInventory.vb, ucOrders.vb
if the user picks "Accounts" from a list, I need to add the ucAccounts to my
form, and display that User Control.

So, I need something like

Select Case UserSelection
case "Accounts"
frmMain.Controls.Add(ucAccounts)
case "Inventory"
frmMain.Controls.Add(ucInventory)
case "Orders"
frmMain.Controls.Add(ucOrder)
End Select

But, because I've got about 30 User Controls, and the number will be
growing, I'd really rather do it elegantly, rather than with a gigantic
Select statement.
 
A

Armin Zingler

Dave said:
Herfried;
I don't think that answers my question, at least as far as I can
tell, not being able to read German. The examples on your page seem
to be detailing creating new objects.

I've already got the UserControls created. They are in my project
already. I need to pick one, based on a user request, add it to the
controls in my current form and display the form.



In the project, you've created classes, not objects. If you want to
create an object by it's class name, Herfried's link is the
way to go.

I've already got ucAccounts.vb, ucInventory.vb, ucOrders.vb
if the user picks "Accounts" from a list, I need to add the
ucAccounts to my form, and display that User Control.

So, I need something like

Select Case UserSelection
case "Accounts"
frmMain.Controls.Add(ucAccounts)
case "Inventory"
frmMain.Controls.Add(ucInventory)
case "Orders"
frmMain.Controls.Add(ucOrder)
End Select

But, because I've got about 30 User Controls, and the number will be
growing, I'd really rather do it elegantly, rather than with a
gigantic Select statement.


See Herfried's link. It creates a new object based on the class name.


Armin
 

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