Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamic controls

Status
Not open for further replies.

mushin

Programmer
Joined
Oct 4, 2000
Messages
49
I need to create dynamic listboxes/comboboxes at runtime when a user clicks inside a specific text box. The idea is that when he/she clicks a listbox will display allowing a selection which I will store in the field originally clicked in. There is no room to design in all of the listboxes I need, so I want just one that can be used over and over.

Can VB do this ? What is the creation syntax ?

TIA
 
Option Explicit

Dim WithEvents txtMain As TextBox
Dim WithEvents lstMain As ListBox

'Create and Set Up Controls
Set txtMain = Me.Controls.Add("vb.textbox", "txtMain", Me)
txtMain.Text = ""
txtMain.Visible = True

etc... Eric De Decker
vbg.be@vbgroup.nl

Licence And Copy Protection AxtiveX
Source CodeBook for the programmer
 
Hi,
You can create a control dynamically in VB by using the LOAD statement. Ex:

Load lstNames(Index)

where lstnames is the name of your control. Index is an integer.

To do it you haev to create a listbox control with an index of 0 and set visible to false.
Use the following code to do the rest.

Private Sub lstFldValues_Click(Index As Integer)
'Your listbox with field values in it.
'Assign selected item into textbox
Text1(Index).Text = lstFldValues(Index).List(lstFldValues(Index).ListIndex)
'Unload dynamically created control.
Unload lstFldValues(Index)
End Sub

Private Sub Text1_Click(Index As Integer)
'Your textboxes for your fields.
'Load a dynamically craeted control. NOTE. Index cannot be 0 since your hiden one
os set at 0.
Load lstFldValues(Index)
'Insert your own routine to populate listbox.
lstFldValues(Index).AddItem "Fldvalue1"
lstFldValues(Index).AddItem "Fldvalue2"
lstFldValues(Index).AddItem "Fldvalue3"
'Set left, top properties of listbox as needed to position it according to your text 'boxes
'Show the newly created control.
lstFldValues(Index).Visible = True
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top