By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,937 Members | 2,447 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,937 IT Pros & Developers. It's quick & easy.

Setting an object to a User Control

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a


"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.