在运行时添加按钮

时间:2010-12-19 15:56:05

标签: vb.net


我想在表格上放几个按钮。这个数字在设计时是未知的。实际上,每个按钮都代表在组合框中输入的项目。因此,如果用户添加项目,则应通过代码添加表单上的按钮。 请告知如何做到这一点?

感谢
Furqan

2 个答案:

答案 0 :(得分:2)

您可以通过简单地循环遍历任何数字(在这种情况下来自组合框)并在将它们添加到表单之前创建所需数量的按钮来完成此操作。

For i As Integer = 0 To myComboBox.Items.Count - 1
   Dim newButton = new Button()

   // Add some properties, etc. to the button
   newButton.Text = myComboBox.Items(i).ToString()

   MyForm.Controls.Add(newButton)
Next

答案 1 :(得分:1)

您可以使用以下功能:

Sub AddButton(ByVal label As String, ByVal location As Point)

Dim b As Button

b = New Button
b.Location = location
b.Text = label
Me.Controls.Add(b)

End Sub