Adding controls to a panel programmatically
I'm trying to add a group of four radio buttons to a form. There are other radio buttons so I'm grouping them by placing them on a Panel. However using the below I just get the panel added to the form without the radio buttons... Am I doing something wrong here?
Dim arrRButton(3) As RadioButton
arrRButton(0) = New RadioButton
arrRButton(1) = New RadioButton
arrRButton(2) = New RadioButton
arrRButton(3) = New RadioButton
With arrRButton(0)
.AutoSize = True
.Checked = True
.Location = New System.Drawing.Point(77, 139)
.Name = "RadioButton5"
.Size = New System.Drawing.Size(55, 17)
.TabIndex = 48
.TabStop = True
.Text = "NEAR"
.UseVisualStyleBackColor = True
End With
'.... etc
'Panel2
Dim Panel2 As New Panel
With Panel2
.Controls.Add(arrRButton(0开发者_开发百科))
.Controls.Add(arrRButton(1))
.Controls.Add(arrRButton(2))
.Controls.Add(arrRButton(3))
.Location = New System.Drawing.Point(61, 130)
.Name = "Panel2"
.Size = New System.Drawing.Size(300, 24)
End With
Me.Controls.Add(Panel2)
.Size = New System.Drawing.Size(300, 24)
There's your problem, you made the panel too small. The first radio button's Location is at (77, 139), you'll have to set the panel's height to at least 139 + 17 = 156 to see it in full.
Here's a trick to get this kind of code right. In the Solution Explorer window, locate the "Show All Files" icon and click it. That shows all files in your solution. A node appears next to your form. Click it and double-click the .Designer.vb file. Locate the InitializeComponent() method. Observe how this code changes as you drop controls on the form and set their properties. Copy and paste code from this.
Using a UserControl can also be useful.
If you want to add a group of radio buttons to a form, use RadioButtonGroup
, not an array of radio buttons.
your problem is at here:
your setting is for button(0)
only , correct it !
for n=0 to 3
With arrRButton(n)
.AutoSize = True
.Checked = True
.Location = New System.Drawing.Point((55*n) +5, 5) '
.Name = "RadioButton5"+ n.tostring()
.Size = New System.Drawing.Size(55, 17)
.TabIndex = 48
.TabStop = True
.Text = "NEAR"
.UseVisualStyleBackColor = True
End With
next
精彩评论