我有一个面板,我可以添加一组组合框和文本框。我想从我的数据库中添加项目到动态添加的组合框。如何添加项目以动态添加组合框?
这是我通过点击按钮动态添加组合框的代码。
Dim number As Integer = 2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim itemCombobox As New ComboBox
itemCombobox.Name = "itemCombobox" + number.ToString
itemCombobox.Location = New Point(25, number * 29)
itemCombobox.Size = New Point(113, 23)
itemCombobox.DropDownStyle = ComboBoxStyle.DropDownList
Dim qtyTxtbox As New MaskedTextBox
qtyTxtbox.Name = "qtyTxtbox" + number.ToString
qtyTxtbox.Location = New Point(172, number * 29)
qtyTxtbox.Size = New Point(37, 20)
Dim specsBtn As New ComboBox
specsBtn.Name = "specsBtn" + number.ToString
specsBtn.Location = New Point(236, number * 29)
specsBtn.Size = New Point(113, 23)
specsBtn.DropDownStyle = ComboBoxStyle.DropDownList
Dim bmCombobox As New ComboBox
bmCombobox.Name = "bmCombobox" + number.ToString
bmCombobox.Location = New Point(385, number * 29)
bmCombobox.Size = New Point(113, 23)
bmCombobox.DropDownStyle = ComboBoxStyle.DropDownList
Panel8.Controls.Add(itemCombobox)
Panel8.Controls.Add(qtyTxtbox)
Panel8.Controls.Add(specsBtn)
Panel8.Controls.Add(bmCombobox)
number = number + 1
End Sub
这是我将项目添加到itemcombobox
的功能Private Sub load_item1()
Dim conn As New MySqlConnection
conn.ConnectionString = "server=127.0.0.1; username=root; password=p@ssw0rd; database= atos_db"
Dim reader As MySqlDataReader
Try
conn.Open()
Dim query As String
query = "select * from atos_db.item_tbl"
comm = New MySqlCommand(query, conn)
reader = comm.ExecuteReader
While reader.Read
Dim sItem = reader.GetString("item")
itemCombobox1.Items.Add(sItem)
End While
conn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
conn.Dispose()
End Try
End Sub
如何获取每个动态添加的控件的值?
答案 0 :(得分:0)
您的变量“itemCombobox”在方法“Button1_Click”中声明,因此无法在您的方法“load_item1”中使用它。 要做到这一点,你必须在你的classe声明你的变量为全局变量:
Dim itemCombobox As New ComboBox
Dim number As Integer = 2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
itemCombobox.Name = "itemCombobox" + number.ToString
itemCombobox.Location = New Point(25, number * 29)
itemCombobox.Size = New Point(113, 23)
itemCombobox.DropDownStyle = ComboBoxStyle.DropDownList
Dim qtyTxtbox As New MaskedTextBox
qtyTxtbox.Name = "qtyTxtbox" + number.ToString
qtyTxtbox.Location = New Point(172, number * 29)
qtyTxtbox.Size = New Point(37, 20)
Dim specsBtn As New ComboBox
specsBtn.Name = "specsBtn" + number.ToString
specsBtn.Location = New Point(236, number * 29)
specsBtn.Size = New Point(113, 23)
specsBtn.DropDownStyle = ComboBoxStyle.DropDownList
Dim bmCombobox As New ComboBox
bmCombobox.Name = "bmCombobox" + number.ToString
bmCombobox.Location = New Point(385, number * 29)
bmCombobox.Size = New Point(113, 23)
bmCombobox.DropDownStyle = ComboBoxStyle.DropDownList
Panel8.Controls.Add(itemCombobox)
Panel8.Controls.Add(qtyTxtbox)
Panel8.Controls.Add(specsBtn)
Panel8.Controls.Add(bmCombobox)
number = number + 1
Dim theButton As Button
theButton = New Button
theButton.Location = location
theButton.Text = label
AddHandler theButton.Click, AddressOf Me.btn_clicked
End Sub
并在方法load_item1()
中使用此变量:
Private Sub load_item1()
Dim conn As New MySqlConnection
conn.ConnectionString = "server=127.0.0.1; username=root; password=p@ssw0rd; database= atos_db"
Dim reader As MySqlDataReader
Try
conn.Open()
Dim query As String
query = "select * from atos_db.item_tbl"
comm = New MySqlCommand(query, conn)
reader = comm.ExecuteReader
While reader.Read
Dim sItem = reader.GetString("item")
itemCombobox.Items.Add(sItem)
End While
conn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
conn.Dispose()
End Try
End Sub
Private Sub btn_clicked(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim selectedBtn As Button = sender
MsgBox("you have clicked button " & selectedBtn.Name)
End Sub