Adding a pair of controls at Runtime, to a form
I've read many posts about dyamically adding controls at runtime, I think I know how to do it, but am confused if I need to use the VBControlExtender or not.
I want to read in a file where each line in a file contains a variable name, a value that is assigned to that variable.
For each line in the file, I want to add a Label and Text Box pair to the form.
The label will be on the left side of the text box and the width of the label will vary.
Would I be better off making a class which contains a variable for a Label Control and another variable for theTextBox control, then adding an instance of that class to a control array, dynamically creating a label and text box ,assigning them to the new instance of the class,
for each line in the file, or something totally different?
thanks
[845 byte] By [
cappy2112] at [2007-11-19 14:07:32]

# 1 Re: Adding a pair of controls at Runtime, to a form
No... not quite. I don't think you are quite understanding how control arrys work.
place a text box control on your form.
place a label control on your form.
Name them as you like.
Set the "index" property of both to be 0, and make both of them hidden (visible = false).
These will now basically act as templates for you... and have created two control arrays.
Now, read each line from the file. For each variable, add to the control arrays like this:
(I named my text boxes txtTextBoxes and Labels lblLabels)
Load txtTextBoxes(i) 'Where i is a counter, used to count the number of lines read
Load lblLabels(i)
txtTextBoxes(i).Visible = True
txtTextBoxes(i).Top = txtTextBoxes(i).Height * i
lblLabels(i).Visible = True
lblLabels(i).Top = txtTextBoxes(i).Top
You'll have to decide which settings you need to set to what... but that should get you started. You may also want to use the TAG property to store the name of the variable from the file.
txtTextBoxes(i).Tag = VariableName
LblLabels(i).Tag = VariableName
Does that help you?