vb.net 如何在 VB 中在运行时动态添加文本框、标签和按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20991539/
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
How to add textboxes, labels and buttons dynamically at runtime in VB?
提问by user3172488
How to create a form with a button add_subjects
which adds one textbox
and a corresponding label on each click,3 buttons - Add, Edit and Delete
, for each textbox
created during runtime in VB
.
Once each texbox's
corresponding Add _button
is clicked, it passes textbox's
value to the label.
如何创建一个带有按钮的表单,该按钮在每次单击时add_subjects
添加一个textbox
和相应的标签3 buttons - Add, Edit and Delete
,对于textbox
在VB
. 单击每个texbox's
对应项后Add _button
,它会将textbox's
值传递给标签。
回答by Jens
A control like a textbox is just an object of the class Textbox. In order for the form to display this object it needs to be added to the form's Controls property. To create a new textbox all you need to do is
像文本框这样的控件只是 Textbox 类的一个对象。为了让表单显示此对象,需要将其添加到表单的 Controls 属性中。要创建一个新的文本框,您需要做的就是
Dim newTB as New Textbox
newTB.Name = "tbNew"
'Set location, size and so on if you like
Me.Controls.Add(newTB)
If you want your control to be able to respond to events you need to add an event handler for the event you want to the control. This handler refers the event to a method of your choice.
如果您希望您的控件能够响应事件,您需要为您希望控件的事件添加一个事件处理程序。此处理程序将事件引用到您选择的方法。
Public Class Form1
Sub CreateTB
Dim NewTB as New Textbox
newTB = New Textbox
newTB.Name = "tbNew"
AddHandler newTB.TextChanged, AddressOf HandleTextChanged
Me.Controls.Add(newTB)
End Sub
Private Sub HandleTextChanged(sender as Object, e as EventArgs)
'Handle the event
End Sub
End Class
You should make sure that the names are unique if you are creating the controls or you might run into trouble.
如果您正在创建控件,您应该确保名称是唯一的,否则您可能会遇到问题。
You can also store your created controls in an array or list as a global variable. That way you can easily access them afterwards.
您还可以将创建的控件作为全局变量存储在数组或列表中。这样您之后就可以轻松访问它们。
回答by srka
Private Property number as Integer=1
Private Sub add_subject_Click(sender As Object, e As EventArgs) Handles add_subject.Click
Dim tb As New TextBox
tb.Name="TextBox"+number.ToString
tb.Position = New Point(number*40,10) ' change this if you want
Me.Controls.Add(tb)
Dim lb As New Label
lb.Name="Label"+number.ToString
lb.Position = New Point(number*40,50) ' change this if you want
Me.Controls.Add(lb)
Dim add As New Button
add.Name="AddButton"+number.ToString
add.Position = New Point(number*40,100) ' change this if you want
AddHandler(add.Click, AdressOf(add_Click))
Me.Controls.Add(add)
Dim edit As New Button
edit.Name="EditButton"+number.ToString
edit.Position = New Point(number*40,150) ' change this if you want
AddHandler(edit.Click, AdressOf(edit_Click))'you have to make edit_Click
YourForm.Controls.Add(edit)
Dim delete As New Button
delete.Name="DeleteButton"+number.ToString
delete.Position = New Point(number*40,200) ' change this if you want
AddHandler(delete.Click, AdressOf(delete_Click))'you have to make delete_Click
Me.Controls.Add(delete)
number+=1
End Sub
So we make all controls, dynamically make names, change positions, add handlers and add controls to form.
所以我们制作所有控件,动态命名,更改位置,添加处理程序并将控件添加到表单。
Private Sub add_Click(sender As Object, e As EventArgs)
Ctype(Me.Controls.Find("Label"+sender.Name.Substring(9),True).First,Label).Text = Ctype(Me.Controls.Find("TextBox"+sender.Name.Substring(9),True).First,TextBox).Text
End Sub
Here we find Label And TextBox using sender's number(sender.Name.Substring(9) will remove AddButton and leave number) and change Label.Text to TextBox.Text.
这里我们使用发件人的号码找到标签和文本框(sender.Name.Substring(9) 将删除 AddButton 并保留号码)并将 Label.Text 更改为 TextBox.Text。
Get all label values and insert them in database:
获取所有标签值并将它们插入数据库:
Private Sub save(sender As Object, e as EventArgs) Handles button_save_subjects.Click
For i = 1 to number
Dim value As String
value = CType(Me.Controls.Find("Label"+number.ToString).First,Label).Text
'insert into database
Next
End Sub
回答by Vignesh Kumar A
Create dynamic Textbox]
创建动态文本框]
Private Sub btnCreateTextbox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateTextbox.Click
Dim textbox1 As New TextBox
textbox1.Name = "Textbox1"
textbox1.Size = New Size(170, 20)
textbox1.Location = New Point(167, 32)
GroupBox1.Controls.Add(textbox1)
End Sub
Create Dynamic Label]
创建动态标签]
Private Sub lblCreateLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblCreateLabel.Click
Dim label1 As New Label
label1.Name = "label1"
label1.Text = "Enter Name"
label1.AutoSize = True
label1.Location = New Point(80, 33)
GroupBox1.Controls.Add(label1)
End Sub
回答by eduardo figueiredo
You can use the same code as above and, in the end, use the parent property of control. Because the control (TextBox, Buttom, etc.) is "inside" of a "container" (form, groupbox, etc.). Like this...
您可以使用与上面相同的代码,最后使用控件的 parent 属性。因为控件(TextBox、Buttom 等)在“容器”(表单、分组框等)的“内部”。像这样...
...
Dim textbox1 As New TextBox
textbox1.Name = "Textbox1" 'or other
...
textbox1.parent = Me 'Me = the form
...
回答by Austin
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Call AddTextBox()
End Sub
Sub AddTextBox()
Dim i As Integer = 1
For Each ctrl In Me.Controls
If TypeOf ctrl Is TextBox Then
i = i + 1
'MsgBox(i)
End If
Next ctrl
Dim Label As New Label
Label.Name = "Label" & i
Label.Size = New Size(170, 20)
Label.Location = New Point(200, (20 + (i * 55)))
Label.Text = "Lbl" & i
Dim Textbox As New TextBox
Textbox.Name = "Textbox" & i
Textbox.Size = New Size(170, 20)
Textbox.Location = New Point(200, (38 + (i * 55)))
Me.Controls.Add(Label)
Me.Controls.Add(Textbox)
End Sub