vb.net 以编程方式向面板添加控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3219155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:56:23  来源:igfitidea点击:

Adding controls to a panel programmatically

vb.netwinforms

提问by madlan

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)

回答by Hans Passant

.Size = New System.Drawing.Size(300, 24)

.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.

那是你的问题,你把面板做得太小了。第一个单选按钮的位置在 (77, 139) 处,您必须将面板的高度设置为至少 139 + 17 = 156 才能完整查看它。

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.

这是使这种代码正确的技巧。在解决方案资源管理器窗口中,找到“显示所有文件”图标并单击它。这会显示您的解决方案中的所有文件。一个节点出现在您的表单旁边。单击它并双击 .Designer.vb 文件。找到 InitializeComponent() 方法。观察当您在窗体上放置控件并设置它们的属性时此代码如何变化。从这里复制并粘贴代码。

Using a UserControl can also be useful.

使用 UserControl 也很有用。

回答by Oded

If you want to add a group of radio buttons to a form, use RadioButtonGroup, not an array of radio buttons.

如果要将一组单选按钮添加到表单中,请使用RadioButtonGroup,而不是一组单选按钮。

回答by artan

your problem is at here:

你的问题在这里:

your setting is for button(0)only , correct it !

您的设置仅供button(0)参考,请更正!

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