如何在VB中创建一个带有按钮add_subjects
的表单,每次点击该按钮会添加一个textbox
和相应的标签,除此之外还需要为每个在运行时创建的textbox
添加3个按钮 - 添加、编辑和删除
。当点击每个textbox
相应的Add_button
时,它会将textbox
的值传递给标签。
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
因此,我们动态生成所有控件的名称、更改位置、添加处理程序并将控件添加到表单中。
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
在这里,我们使用发送者的编号找到Label和TextBox(sender.Name.Substring(9)将删除AddButton并留下数字),并将Label.Text更改为TextBox.Text。
获取所有标签值并将它们插入数据库:
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
像文本框这样的控件只是 Textbox 类的一个对象。为了让窗体显示该对象,需要将其添加到窗体的 Controls 属性中。要创建一个新的文本框只需执行以下操作:
Dim newTB as New Textbox
newTB.Name = "tbNew"
'Set location, size and so on if you like
Me.Controls.Add(newTB)
如果您希望控件能够响应事件,您需要为您想要的事件向该控件添加一个事件处理程序。此处理程序将该事件引用到您选择的方法。
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
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
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
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
您可以使用与上面相同的代码,并最终使用控件的父属性。 因为控件(TextBox、Button等)位于“容器”(表单、GroupBox等)之内。就像这样...
...
Dim textbox1 As New TextBox
textbox1.Name = "Textbox1" 'or other
...
textbox1.parent = Me 'Me = the form
...