所以我有一个带有“btnAddProduct”按钮的表单,我有两个列出产品和子产品的组合框阵列。从第一个组合框中选择产品时,会出现第二个组合框,以便您可以从产品子列表中进行选择。在加载表单时,将显示第一个组合框,如果要添加其他产品,请单击按钮。代码是:
Public Class Form1
Dim gbProduct(5) As GroupBox
Dim lblProduct(5) As Label
Dim cmboBoxProduct(5) As ComboBox
Dim lblSubProduct(5) As Label
Dim cmboBoxSubProduct(5) As ComboBox
Dim pnlProducts As New Panel
Dim n As Integer = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
With pnlProducts
.Width = 500
.Height = 300
.AutoScroll = True
End With
Me.Controls.Add(pnlProducts)
n = 0
addProduct()
Me.AutoScroll = True
End Sub
Private Sub addProduct()
' ----------------Add the groupbox
gbProduct(n) = New GroupBox
With gbProduct(n)
.Text = ""
.Location = New Point(10, 5 + n * 70)
.Width = 400
.Height = 70
End With
pnlProducts.Controls.Add(gbProduct(n))
'------------------ Add the product dropdown
lblProduct(n) = New Label
With lblProduct(n)
.Text = "Product"
.Location = New Point(10, 15)
.Width = 50
End With
gbProduct(n).Controls.Add(lblProduct(n))
cmboBoxProduct(n) = New ComboBox
With cmboBoxProduct(n)
.Items.Add("A")
.Items.Add("B")
.Items.Add("C")
.Text = ""
.Location = New Point(60, 15)
End With
gbProduct(n).Controls.Add(cmboBoxProduct(n))
AddHandler cmboBoxProduct(n).SelectedIndexChanged, AddressOf subProducts
End Sub
Private Sub subProducts()
Try
gbProduct(n).Controls.Remove(cmboBoxSubProduct(n))
Catch
End Try
'------------------ Add the product dropdown
lblSubProduct(n) = New Label
With lblSubProduct(n)
.Text = "Product"
.Location = New Point(190, 15)
.Width = 50
End With
gbProduct(n).Controls.Add(lblSubProduct(n))
cmboBoxSubProduct(n) = New ComboBox
Select Case cmboBoxProduct(n).Text
Case "A"
With cmboBoxSubProduct(n)
.Items.Clear()
.Items.Add("AA")
.Items.Add("AB")
.Items.Add("AC")
.Text = ""
.Location = New Point(240, 15)
End With
Case "B"
With cmboBoxSubProduct(n)
.Items.Clear()
.Items.Add("BA")
.Items.Add("BB")
.Items.Add("BC")
.Text = ""
.Location = New Point(240, 15)
End With
Case "GenieMat FF"
With cmboBoxSubProduct(n)
.Items.Clear()
.Items.Add("CA")
.Items.Add("CB")
.Items.Add("CC")
.Text = ""
.Location = New Point(240, 15)
End With
End Select
gbProduct(n).Controls.Add(cmboBoxSubProduct(n))
End Sub
Private Sub btnAddProduct_Click(sender As Object, e As EventArgs) Handles btnAddProduct.Click
n += 1 'increment array index when button clicked
addProduct()
Me.Refresh()
End Sub
End Class
只要在单击按钮之前添加子产品,此工作正常。如果先单击该按钮,则增加“n”,然后子产品不与正确的产品对齐。告诉子产品阵列与哪个产品相关联的最佳方法是什么?
非常感谢。
答案 0 :(得分:0)
我不知道vb,但我知道你被卡住了。我认为最简单的方法是添加一个公共检查以查看该字段是否已初始化,如:
Dim hasText As boolean = false
你声明n的地方。
然后在您的按钮点击事件中,您检查是否hasText == true并且仅在该场景中增加n。您可以使用我认为的每个case语句将hasText设置为true。
在C ++中,我将一个成员函数添加到buttonClick对象作为发送者,以检查自己的文本,并且只有当该值为真时才递增n。我不确定你是否可以在vb中做到这一点!
答案 1 :(得分:0)
首先,我不会手动维护值n,它首先不是一个昂贵的电话,通常与表格上的表现无关。
您只有一个try / catch块可能会在发生错误后继续执行,但在实践中,最好检查一下您尝试维护的控件对象的数量,以获得更强大的代码。 / p>
其次,要解决您的问题,只需在按钮点击事件中添加一个if块,伪代码:
If cmboBoxSubProduct has SelectedValue then ' check against text value, etc'
addProduct()
Me.Refresh()
Else
' show label above add product button explaining they need to select a subproduct'
End If
答案 2 :(得分:0)
所以我想出来并认为我会分享。这涉及到这个问题:
VB Express2010: Getting index or tag in array of PictureBox
我需要添加此点击事件:
Private Sub cmboBoxProduct_Click(sender As Object, e As EventArgs)
Dim clickedComboBoxProduct As ComboBox = CType(sender, ComboBox)
m = clickedComboBoxProduct.Tag
txtShow.Text = m
End Sub
由此事件处理程序引用它:
AddHandler cmboBoxProduct(n).Click, New EventHandler(AddressOf Me.cmboBoxProduct_Click)
希望这有助于其他任何人