如何删除带有列表框的二维数组中的项目

时间:2018-11-09 01:04:39

标签: vb.net

嗨,我有一个编码任务,我很难弄清楚如何编码。我的老师希望我们构建一个程序,该程序使用一个保存产品名称的列表框和一个保存库存数量和价格的二维数组。然后,在应用程序中的一个按钮(即“删除”按钮)中,应删除列表框中的项目以及数组中的数据。当用户删除项目时,不仅列表必须松开项目的名称,而且二维数组也必须重新调整。

2 个答案:

答案 0 :(得分:0)

对不起。我的大脑只是不想做你老师想要的。如果您能理解以下内容,也许您可​​以说服老师在这种情况下不要要求您使用2D阵列。

Public Class Product
    'These are automatic properties
    'The compiler provides the getter, setter and backer fields.
    Public Property Name As String
    Public Property Quantiy As Integer
    Public Property Price As Double
    'ToString is a method inherited from Object. It will return a fully
    'qualified name of the Type, not what we want in a ListBox
    'The ListBox will call .ToString on Product and we will get the Name
    'property of the Product object.
    Public Overrides Function ToString() As String
        Return Name
    End Function

    Public Sub New(prodName As String, prodQuantity As Integer, prodPrice As Double)
        'We add a parameterized Constructor to make it easy to add our Product objects
        'to the ListBox. Intelisense will help out once we type the New keyword
        Name = prodName
        Quantiy = prodQuantity
        Price = prodPrice
    End Sub

End Class

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'The New keyword calls the Constructor of the Product class
        'so a Product object is added to the ListBox
        ListBox1.Items.Add(New Product("Bell", 30, 2.98))
        ListBox1.Items.Add(New Product("Book", 7, 200))
        ListBox1.Items.Add(New Product("Candle", 42, 14.99))
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
        'When a Product is deleted from the ListBoX with the RemoveItem button
        'this event will fire. (The index changed) -1 means nothing is selected which is
        'what happens after the delete. If Nothing is selected our cast will fail and
        'we will get an exception.
        If ListBox1.SelectedIndex <> -1 Then
            'The ListBox items are objects but underneath the objects are Products
            'so we can Cast the object back to a Product
            Dim p As Product = CType(ListBox1.SelectedItem, Product)
            'All the properties of the Procuct are then available
            MessageBox.Show($"Product Name is {p.Name}. There are {p.Quantiy} on hand. The price is {p.Price:N2}")
        End If
    End Sub

    Private Sub btnRemoveItem_Click(sender As Object, e As EventArgs) Handles btnRemoveItem.Click
        ListBox1.Items.Remove(ListBox1.SelectedItem)
    End Sub

End Class

答案 1 :(得分:0)

这应该有效,尽管这实际上是错误的处理方式:

LBProducts =表单列表框

lblQuantity =表单标签

lblPrice =表单标签

btnDelete =表单按钮

Public Class Form1
'5 Rows, (Price, Qty)
Private ProductArray(5, 1) As Integer

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    LBProducts.Items.Add("Apples")
    LBProducts.Items.Add("Bananas")
    LBProducts.Items.Add("Grapes")
    LBProducts.Items.Add("Oranges")
    LBProducts.Items.Add("Peaches")

    For x = 0 To 5
        ProductArray(x, 0) = x
        ProductArray(x, 1) = 1
    Next
End Sub

Private Sub LBProducts_SelectedIndexChanged(sender As Object, e As EventArgs) Handles LBProducts.SelectedIndexChanged
    Dim Index = LBProducts.SelectedIndex()

    If Index >= 0 Then
        lblPrice.Text = "Price: " & ProductArray(Index, 0)
        lblQuantity.Text = "Qty: " & ProductArray(Index, 1)
    End If

End Sub

Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
    Dim Index = LBProducts.SelectedIndex()
    If Index >= 0 Then
        LBProducts.Items.RemoveAt(Index)

        Dim NewArray(UBound(ProductArray) - 1, 1) As Integer

        Dim i As Integer = 0
        For x = 0 To UBound(ProductArray)
            If x <> Index Then
                NewArray(i, 0) = ProductArray(x, 0)
                NewArray(i, 1) = ProductArray(x, 1)
                i += 1
            End If
        Next

        ProductArray = NewArray

    End If
End Sub
End Class