开发者

Adding buttons at runtime

I want to place a few buttons on my form. The number is unknown at design time. Actually each button will represent an item entered in combo box. So if user adds an开发者_JAVA技巧 item, a button on the form should be added by the code. Please advise how to do it?

Thanks

Furqan


You can do this by simply looping over any number (in this case from a combo box) and creating the required number of buttons before adding them to the form.

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


You can use a function like this:

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜