Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Windows Form with Variable number of Fields

Windows Form with Variable number of Fields

Windows Form with Variable number of Fields

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:


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()

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?


RE: Windows Form with Variable number of Fields

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.


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

RE: Windows Form with Variable number of Fields

Crud - sorry. I thought I was in the VB6 forum.
Basically the same idea, but in .NET you do not have control arrays so you have to generated unique names for each of the new controls. Also, you need to define the address of the handler for the control.

There's an example here:

RE: Windows Form with Variable number of Fields

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.

RE: Windows Form with Variable number of Fields

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
tb(i).Left = 10
If i = 0 Then
tb(i).Top = 10
tb(i).top = 10 + tb.Height * i

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!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close