Dynamically adding a user control

tverney

Newcomer
Joined
Nov 30, 2004
Messages
6
There has to be a simple answer to this. I created a user control called ctlAuditDetailExpert in VB.Net. I can manually add it to a parent windows form successfully.

The goal is to allow the user to add new instances of cltAuditDetailExpert at runtime. I attempted to add the user control as follows

Sub Test(lpName as string)
Dim ctl as New ctlAuditDetailExpert
with ctl
.location = New Point(50,50)
.visible = true
.show()
.name = lpName
end With
End Sub

The code run fine, no error messages but the newly added user control is not seen on the parent form. What am I missing?
 
You have to add it to the parent form with the Controls.AddRange method. Also, in addition, you might consider using an array as a class member that holds the newly created instances of the controls (instead just using a variable with function-level scope). This will allow you to reference the object later.
 
Man... You're creating the control alright but you're missing the part where you tell where to put it!!

Try adding domething like: Me.Controls.Add(ctl)

That will work...

Alex :p
 
That works!! Thanks so much

AlexCode said:
Man... You're creating the control alright but you're missing the part where you tell where to put it!!

Try adding domething like: Me.Controls.Add(ctl)

That will work...

Alex :p

Alex, thanks for the help! This was driving me nuts.
 
No swett...
This can become a little harder when we need this kind of behaviour but at design-time...

At Run-Time it's straight-forward!

Alex :p
 
Thanks for pointing out the obvious

Mister E said:
You have to add it to the parent form with the Controls.AddRange method. Also, in addition, you might consider using an array as a class member that holds the newly created instances of the controls (instead just using a variable with function-level scope). This will allow you to reference the object later.

Mister E,
Thanks thinking one step ahead of me when it came to the control collection. The obvious didn't come clear until later in the process; then I re-read your response.
 
Back
Top