dynamically create controls

capwrmt

Member
Joined
May 27, 2003
Messages
19
Location
Connecticut, US
How would I dynamically create controls in vb.net when the program is running?
i.e. control of location of new controls,
accessing methods and properties, ect?
 
try this...
Code:
class level...

Friend WithEvents txt1 As New TextBox()

inside any routine...

        With txt1
            .Location = New Point(100, 100)
            .Size = New Size(200, 50)
            .Text = "Hello"
            .TabIndex = 0
            .Visible = True

        End With
        Me.Controls.Add(txt1)
 
ok, i tried your suggestion and I am getting a runtime error ...
An unhandled exception of type System.OutOfMemoryException occurred in system.windows.forms.dll

Additional information: Error creating window handle.

Here is the code that I am using:

Code:
Private Sub frm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        main sub is used to initialize database connections and form objects
        main()
        addControls() add dynamic form controls
end sub

Private Sub addControls()
       .....database connection code ....

        Dim i As Integer = 1
        add a row of controls for each record
        Do Until objRs.EOF
            lbl = New Label()
            cbo = New ComboBox()
            textFirst = New TextBox()
            textLast = New TextBox()
            ReDim Preserve lbl(i)
            ReDim Preserve cbo(i)
            ReDim Preserve textFirst(i)
            ReDim Preserve textLast(i)
            With lbl(i)
                .Text = objRs.Fields("TeamName").Value
                .Location = New Point(8, 104 + i * 40)
                .Size = New Size(100, 50)
                .Name = lbl(i).ToString
            End With
            With cbo(i)
                .Items.AddRange(New Object() {"Active", "Bye"})
                .Text = "Active"
                .Location = New Point(112, 96 + i * 40)

                .Size = New System.Drawing.Size(121, 29)
                AddHandler .SelectedValueChanged, AddressOf handlercboclick
                .Name = cbo(i).ToString
            End With
            With textFirst(i)
                .Location = New System.Drawing.Point(248, 96 + i * 40)

                .Text = ""
                .Name = textFirst(i).ToString
            End With
            With textLast(i)
                .Location = New System.Drawing.Point(368, 96 + i * 40)

                .Text = ""
                .Name = textLast(i).ToString
            End With

            Controls.Add(lbl(i))
            Controls.Add(cbo(i))
            Controls.Add(textFirst(i))
            Controls.Add(textLast(i))

            objRs.MoveNext()
            i = i + 1
        Loop
        
    End Sub
any suggestions on how to make this work would be greatly appreciated.
Thanks.
 
What was the solution to your problem? Taking a quick glance at your code Im assuming the bottleneck is with the redim preserves.
 
I attempted to debug the code by first commenting out the database code. Result - the dynamic code worked fine.
Therein, I discovered that I neglected to include the "objCon = new adodb.connection" constructors.

So, my main problem was with the database constructors.

However, I also decided that using control arrays was not necessary and were a waste of memory. So, instead I just created a loop:

Code:
do until objrs.eof
lbl=new label
btn=new button
with lbl
.text="new label"
...
end with
with btn
.text="new button"
...
end with
me.controls.add(lbl)
me.controls.add(btn)
objrs.movenext
loop
 

Similar threads

Back
Top