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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Windows Form with Variable number of Fields

Status
Not open for further replies.

jmarkus

Technical User
Oct 15, 2002
124
CA
I would like to generate a Form using code based on a variable number of fields. Essentially I am creating code a simple dialog box which sometimes might have 5 fields and sometimes 15. Currently I only know how to manually set each field using:

Code:
Me.TextBox1 = New System.Windows.Forms.TextBox()
Me.Label1 = New System.Windows.Forms.Label()
Me.TextBox2 = New System.Windows.Forms.TextBox()
Me.Label2 = New System.Windows.Forms.Label()
.
.
.
etc...

But what I would really like is to index Me.TextBox? and Me.Label? so I can manipulate it in a loop.

Is this possible?

Thanks,
Jeff
 
You use the Load function to instantiate new controls - usually into a control array. (VB6) Once instantiated, you can set things up on the control properties, just like any other control. (Note that control arrays start from index 0.)

What we tend to do is define the controls for element 0, placing them on the form. Subsequent controls are placed in a vertical stack.
Finally, be sure to adjust your form size to accommodate the new controls.

Code:
iGrids = UBound(asctmRepGrid)
For iCurrent = 1 To iGrids
  Call Load(cmdTab(iCurrent))

  cmdTab(iCurrent).Top = cmdTab(iCurrent - 1).Top + cmdTab(iCurrent - 1).Height
  cmdTab(iCurrent).Caption = asctmRepGrid(iCurrent).cTabCaption
  cmdTab(iCurrent).Visible = True
Next


 
You may also consider using a grid instead. DataGridView maybe?

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 

Yes it is possible. Here's some code to get you started.

Dim tb(4) TextBox 'array of TextBox

For i As Integer = 0 to 4 'loop 5 times
tb(i) = New TextBox​
tb(i).Name = "TextBox" & i.ToString​
Me.Controls.Add(tb(i))​
tb(i).Left = 10​
If i = 0 Then
tb(i).Top = 10​
Else
tb(i).top = 10 + tb.Height * i​
EndIf​
tb(i).Show​
Next

This creates an array of 5 TextBox objects, instantiates them and adds them to the form. You can then access them in code with either the Name (TextBox0, TextBox1, etc.), or by the array (tb(0), tb(1), etc.).


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top