Groups | Blog | Home
all groups > vb.net controls > may 2004 >

vb.net controls : Finding dynamic control


felad NO[at]SPAM walla.co.il
5/31/2004 3:20:00 AM
Hi

I have form that it's controls are being built dynamically based on a
database ( VB.net )
I have created the controls dynamically and associate an event for it
( Based on http://support.microsoft.com/default.aspx?scid=kb;en-us;Q311321
)
1. Is there a better way for creating dynamic controls ?
2. How can I access one of the control I have created lets say
Dropdownlist named "dlVersionName" ?

Dim asm As [Assembly]
asm = GetType(Form).Assembly
ControlObject = asm.CreateInstance(ControlType)
ControlObject.Name = <ControlName>
ControlObject.Location = New System.Drawing.Point(20, 20)
DropDownList = ControlObject
DropDownList.Visible = True
TabPage.Controls.Add(DropDownList)
AddHandler DropDownList.SelectedIndexChanged, AddressOf
ctlVersion_SelectedIndexChanged

Now I want to do something like
str = dlVersionName.SelectedValue

nobody NO[at]SPAM nospam.net
6/3/2004 7:46:59 PM
No, you can't acess dlVersionName like
str = dlVersionName.SelectedValue
in VB or many compiled language, if you create it dynamically. (Maybe you
could do similar things in a script language).

Basically, if you do know the type of control you want to create when you
code, but don't know how many of them you will create. You don't have to do
it like your post. You can do something like:

DropDownList = new ComboBox()
DropDownList .Name = <ControlName>
DropDownList .Location = New System.Drawing.Point(20, 20)
DropDownList.Visible = True
TabPage.Controls.Add(DropDownList)
...

If you don't know what type you will create, but you will create it limited
times. you can define a member variable to hold the control, like

Dim dlVersionName as Control

so you can access it easily later.


If you don't meet any of those, you might want to create a hashtable to
hold the those controls, so you can map the name to the control easily.
AddThis Social Bookmark Button