将正确的对象数组与 vb.net 中的另一个对象数组链接
Linking correct array of objects with another array of objects in vb.net
所以我有一个带有按钮 "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",那么子产品就不会与正确的产品对齐。告诉子产品数组要与哪个产品关联的最佳方式是什么?
非常感谢。
我不知道 vb,但我知道你被困住了。我认为最简单的方法是添加一个 public 检查以查看该字段是否已初始化,例如:
Dim hasText As boolean = false
在你声明 n 的地方。
然后在您的按钮单击事件中,您将检查是否 hasText==true 并且在该场景中仅递增 n。对于我相信的每个案例陈述,您都会将 hasText 设置为 true 。
在 C++ 中,我会向 buttonClick 对象添加一个成员函数作为发送者来检查自身是否有文本,并且仅在为真时递增 n。不过,我不确定您是否可以在 vb 中做到这一点!
首先,我不会手动维护值 n,它首先不是一个昂贵的调用,而且通常与表单的性能无关。
您只有一个 try/catch 块可能会在错误后继续执行,但在良好的实践中,最好检查您试图维护的控件对象的计数以获得更健壮的代码.
其次,要解决您的问题,只需向您的按钮单击事件添加一个 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
所以我想到了这一点,并认为我会分享。这涉及到这个问题:
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)
希望这对其他人有所帮助
所以我有一个带有按钮 "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",那么子产品就不会与正确的产品对齐。告诉子产品数组要与哪个产品关联的最佳方式是什么?
非常感谢。
我不知道 vb,但我知道你被困住了。我认为最简单的方法是添加一个 public 检查以查看该字段是否已初始化,例如:
Dim hasText As boolean = false
在你声明 n 的地方。
然后在您的按钮单击事件中,您将检查是否 hasText==true 并且在该场景中仅递增 n。对于我相信的每个案例陈述,您都会将 hasText 设置为 true 。
在 C++ 中,我会向 buttonClick 对象添加一个成员函数作为发送者来检查自身是否有文本,并且仅在为真时递增 n。不过,我不确定您是否可以在 vb 中做到这一点!
首先,我不会手动维护值 n,它首先不是一个昂贵的调用,而且通常与表单的性能无关。
您只有一个 try/catch 块可能会在错误后继续执行,但在良好的实践中,最好检查您试图维护的控件对象的计数以获得更健壮的代码.
其次,要解决您的问题,只需向您的按钮单击事件添加一个 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
所以我想到了这一点,并认为我会分享。这涉及到这个问题:
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)
希望这对其他人有所帮助