470,849 Members | 1,129 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Setting an object to a User Control

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

Mar 1 '06 #1
3 1297
private Accounts myAccount;

myAccount = new Accounts();

frmMain.controls.Add(myAccount);

This shouldn't have any problem.

Tedmond

"Dave" wrote:
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

Mar 2 '06 #2
Sorry, yours is in VB verion.

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

"Tedmond" wrote:
private Accounts myAccount;

myAccount = new Accounts();

frmMain.controls.Add(myAccount);

This shouldn't have any problem.

Tedmond

"Dave" wrote:
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

Mar 2 '06 #3


"Tedmond" wrote:
Sorry, yours is in VB verion.

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

"Tedmond" wrote:
private Accounts myAccount;

myAccount = new Accounts();

frmMain.controls.Add(myAccount);

This shouldn't have any problem.

Tedmond

"Dave" wrote:
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


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.
Mar 2 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Amir Eshterayeh | last post: by
12 posts views Thread by CLEAR-RCIC | last post: by
8 posts views Thread by David Lozzi | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.