dynamically adding controls

jnash

Well-known member
Joined
Oct 20, 2006
Messages
111
Programming Experience
Beginner
dynamically adding controls to a form

basically my app loads forms from a text file and i want a check box to appear on a form for every form loaded up, im still really new to this so if anyone can help i will really appreciate it ive enclosed my code, the first check box appears on the form fine, its just none of the others do!

VB.NET:
        Dim i As Integer

        i = 1
        While numforms > i
            Dim bt As New CheckBox
            bt.Text = Val(i)
            frmExplore.Controls.Add(bt)
            i = i + 1
        End While

        frmExplore.Show()
thanks
 
How are you setting the value of the variable numforms?
Should "While numforms > i" be "While numforms >= i"? (I can't be sure since I don't know how numforms is being set).
Did you debug and step through your code to see the value of numforms?
 
numforms has the right value, ive now changed the form to a listview and it adds dynamically fine, just upsetting i cant add buttons to a form in runtime
unless im doing it wrong, but the loop is fine
 
Referring to the current form instance is done with the 'Me' keyword.
Me.Controls.Add(thenewcontrol)
 
its not the current form, its another form

frmExplore

i want to addd a button to it every time (in this case numforms is 6) so 6 times but once it loops round and has finished it only shows the form with one button! with 1 on it as the text!
 
it could be that you're not setting the location property for the different checkboxs (or buttons, you refer to both in your posts) and they are overlapping.
 
thats what i was thinking however if that was the case shouldnt the button on top be labelled 6 and not 1?
 
Is it the intension to set all controls at same location here?
 
Back
Top