简体   繁体   English

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

[英]How to remove an item in a two dimensional array with a list box

Hi I have an assignment for coding and I am having a hard time to figure out how to code it. 嗨,我有一个编码任务,我很难弄清楚如何编码。 My teacher wanted us to build a program that uses a list box that holds product names and a 2-D array that holds quantity in stock and price. 我的老师希望我们构建一个程序,该程序使用一个保存产品名称的列表框和一个保存库存数量和价格的二维数组。 Then in the one of the buttons in the application, which is the remove button, the item in the list box as well as the data from the array should be removed. 然后,在应用程序中的一个按钮(即“删除”按钮)中,应删除列表框中的项目以及数组中的数据。 When the user deletes an item, not only must the list loose the name of the item but the 2-D array must also be readjusted. 当用户删除项目时,不仅列表必须松开项目的名称,而且二维数组也必须重新调整。

I am sorry. 对不起。 My brain just doesn't want to do what your teacher wants. 我的大脑只是不想做你老师想要的。 If you can make sense of the following, maybe you can convince your teacher not to ask you to use 2D arrays in this instance. 如果您能理解以下内容,也许您可​​以说服老师在这种情况下不要要求您使用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

This should work, though it's really the wrong way to approach this: 这应该起作用,尽管这实际上是错误的处理方式:

LBProducts = Form ListBox LBProducts =表单列表框

lblQuantity = Form Label lblQuantity =表单标签

lblPrice = Form Label lblPrice =表单标签

btnDelete = Form Button 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM