Setting an object to a User Control

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
 
G

Guest

private Accounts myAccount;

myAccount = new Accounts();

frmMain.controls.Add(myAccount);

This shouldn't have any problem.

Tedmond
 
G

Guest

Sorry, yours is in VB verion.

Dim myAccount As Accounts
myAccount = New Accounts
frmMain.Controls.Add(myAccount)
 
G

Guest

Tedmond said:
Sorry, yours is in VB verion.

Dim myAccount As Accounts
myAccount = New Accounts
frmMain.Controls.Add(myAccount)

While that will work, the problem is, Tedmond, I have about 30 different
user controls that the user can choose from. The thing I'm trying to avoid is
setting up a Select statement with 30 choices in it. What I'm trying to find
is a way, based on a string containing the exact name of the user control, to
generate the form.controls.add(usercontrol) statement out of it.

I need something like:

dim gUserControl as UserControl
gUserControl = MyProject.Component(StringNameOfUserControl)
Form.Controls.Add(gUserControl)

But, I need some correct syntax for that middle line of code. That's what I
don't know how to do.
 

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