Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

  • Moderators
Posted

try this...

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

Visit...Bassic Software
Posted

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:

 

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.

Posted
What was the solution to your problem? Taking a quick glance at your code I'm assuming the bottleneck is with the redim preserves.
Gamer extraordinaire. Programmer wannabe.
Posted

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:

 

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...