如何使用按钮add_subjects
创建表单,为textbox
添加一个3 buttons - Add, Edit and Delete
以及每个点击textbox
的相应标签,用于VB
在运行时期间创建的每个texbox's
{1}}。
点击每个Add _button
对应的textbox's
后,会将{{1}}值传递给标签。
答案 0 :(得分:2)
像文本框这样的控件只是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
如果要创建控件,则应确保名称是唯一的,否则可能会遇到麻烦。
您还可以将创建的控件作为全局变量存储在数组或列表中。这样你就可以轻松访问它们了。
答案 1 :(得分:2)
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 And 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
答案 2 :(得分:1)
创建动态文本框]
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
答案 3 :(得分:0)
您可以使用与上面相同的代码,最后使用控件的父属性。 因为控件(TextBox,Buttom等)是“容器”(form,groupbox等)的“内部”。像这样......
...
Dim textbox1 As New TextBox
textbox1.Name = "Textbox1" 'or other
...
textbox1.parent = Me 'Me = the form
...
答案 4 :(得分:0)
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