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









up vote
0
down vote

favorite












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.










share|improve this question























  • Just so you know, this is a terrible assignment. 2D arrays have their place but they are something that is rarely used. For some reason, they tend to get taught early on in a course though, and the examples used to demonstrate their use are almost always bad because they are not something that any sane person would use a 2D array for.
    – jmcilhinney
    Nov 9 at 1:13










  • One of the reasons that arrays don't get used so much is that they are fixed-size, i.e. once you create one, you cannot change its size. That means that there is not actually any way to remove elements from an array. You can set an element to its default value, e.g. Nothing for a String, but you cannot remove the element. You can use ReDim Preserve or Array.Resize to create a new array of a new size and copy elements from an existing array but that only works for 1D arrays.
    – jmcilhinney
    Nov 9 at 1:17










  • In your case, "removing" something from the array basically means creating a new array with one less "row" and then copying all the elements before and after the "row" you want to remove. That is cumbersome and completely unnecessary these days. A good developer would define a type for a single record and then create a collection containing instances of that type. Removing an item would involve calling the Remove method of that collection. You could even bind the collection to the ListBox. In your case, you'll still use a Remove method on the ListBox, or maybe RemoveAt.
    – jmcilhinney
    Nov 9 at 1:19











  • I know i'm having a hard time with this topic and I've been creating different code just so I can follow what my teachers prompt.
    – ihcxx
    Nov 9 at 2:10














up vote
0
down vote

favorite












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.










share|improve this question























  • Just so you know, this is a terrible assignment. 2D arrays have their place but they are something that is rarely used. For some reason, they tend to get taught early on in a course though, and the examples used to demonstrate their use are almost always bad because they are not something that any sane person would use a 2D array for.
    – jmcilhinney
    Nov 9 at 1:13










  • One of the reasons that arrays don't get used so much is that they are fixed-size, i.e. once you create one, you cannot change its size. That means that there is not actually any way to remove elements from an array. You can set an element to its default value, e.g. Nothing for a String, but you cannot remove the element. You can use ReDim Preserve or Array.Resize to create a new array of a new size and copy elements from an existing array but that only works for 1D arrays.
    – jmcilhinney
    Nov 9 at 1:17










  • In your case, "removing" something from the array basically means creating a new array with one less "row" and then copying all the elements before and after the "row" you want to remove. That is cumbersome and completely unnecessary these days. A good developer would define a type for a single record and then create a collection containing instances of that type. Removing an item would involve calling the Remove method of that collection. You could even bind the collection to the ListBox. In your case, you'll still use a Remove method on the ListBox, or maybe RemoveAt.
    – jmcilhinney
    Nov 9 at 1:19











  • I know i'm having a hard time with this topic and I've been creating different code just so I can follow what my teachers prompt.
    – ihcxx
    Nov 9 at 2:10












up vote
0
down vote

favorite









up vote
0
down vote

favorite











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.










share|improve this question















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.







vb.net






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 1:11









jmcilhinney

24.8k21932




24.8k21932










asked Nov 9 at 1:04









ihcxx

1




1











  • Just so you know, this is a terrible assignment. 2D arrays have their place but they are something that is rarely used. For some reason, they tend to get taught early on in a course though, and the examples used to demonstrate their use are almost always bad because they are not something that any sane person would use a 2D array for.
    – jmcilhinney
    Nov 9 at 1:13










  • One of the reasons that arrays don't get used so much is that they are fixed-size, i.e. once you create one, you cannot change its size. That means that there is not actually any way to remove elements from an array. You can set an element to its default value, e.g. Nothing for a String, but you cannot remove the element. You can use ReDim Preserve or Array.Resize to create a new array of a new size and copy elements from an existing array but that only works for 1D arrays.
    – jmcilhinney
    Nov 9 at 1:17










  • In your case, "removing" something from the array basically means creating a new array with one less "row" and then copying all the elements before and after the "row" you want to remove. That is cumbersome and completely unnecessary these days. A good developer would define a type for a single record and then create a collection containing instances of that type. Removing an item would involve calling the Remove method of that collection. You could even bind the collection to the ListBox. In your case, you'll still use a Remove method on the ListBox, or maybe RemoveAt.
    – jmcilhinney
    Nov 9 at 1:19











  • I know i'm having a hard time with this topic and I've been creating different code just so I can follow what my teachers prompt.
    – ihcxx
    Nov 9 at 2:10
















  • Just so you know, this is a terrible assignment. 2D arrays have their place but they are something that is rarely used. For some reason, they tend to get taught early on in a course though, and the examples used to demonstrate their use are almost always bad because they are not something that any sane person would use a 2D array for.
    – jmcilhinney
    Nov 9 at 1:13










  • One of the reasons that arrays don't get used so much is that they are fixed-size, i.e. once you create one, you cannot change its size. That means that there is not actually any way to remove elements from an array. You can set an element to its default value, e.g. Nothing for a String, but you cannot remove the element. You can use ReDim Preserve or Array.Resize to create a new array of a new size and copy elements from an existing array but that only works for 1D arrays.
    – jmcilhinney
    Nov 9 at 1:17










  • In your case, "removing" something from the array basically means creating a new array with one less "row" and then copying all the elements before and after the "row" you want to remove. That is cumbersome and completely unnecessary these days. A good developer would define a type for a single record and then create a collection containing instances of that type. Removing an item would involve calling the Remove method of that collection. You could even bind the collection to the ListBox. In your case, you'll still use a Remove method on the ListBox, or maybe RemoveAt.
    – jmcilhinney
    Nov 9 at 1:19











  • I know i'm having a hard time with this topic and I've been creating different code just so I can follow what my teachers prompt.
    – ihcxx
    Nov 9 at 2:10















Just so you know, this is a terrible assignment. 2D arrays have their place but they are something that is rarely used. For some reason, they tend to get taught early on in a course though, and the examples used to demonstrate their use are almost always bad because they are not something that any sane person would use a 2D array for.
– jmcilhinney
Nov 9 at 1:13




Just so you know, this is a terrible assignment. 2D arrays have their place but they are something that is rarely used. For some reason, they tend to get taught early on in a course though, and the examples used to demonstrate their use are almost always bad because they are not something that any sane person would use a 2D array for.
– jmcilhinney
Nov 9 at 1:13












One of the reasons that arrays don't get used so much is that they are fixed-size, i.e. once you create one, you cannot change its size. That means that there is not actually any way to remove elements from an array. You can set an element to its default value, e.g. Nothing for a String, but you cannot remove the element. You can use ReDim Preserve or Array.Resize to create a new array of a new size and copy elements from an existing array but that only works for 1D arrays.
– jmcilhinney
Nov 9 at 1:17




One of the reasons that arrays don't get used so much is that they are fixed-size, i.e. once you create one, you cannot change its size. That means that there is not actually any way to remove elements from an array. You can set an element to its default value, e.g. Nothing for a String, but you cannot remove the element. You can use ReDim Preserve or Array.Resize to create a new array of a new size and copy elements from an existing array but that only works for 1D arrays.
– jmcilhinney
Nov 9 at 1:17












In your case, "removing" something from the array basically means creating a new array with one less "row" and then copying all the elements before and after the "row" you want to remove. That is cumbersome and completely unnecessary these days. A good developer would define a type for a single record and then create a collection containing instances of that type. Removing an item would involve calling the Remove method of that collection. You could even bind the collection to the ListBox. In your case, you'll still use a Remove method on the ListBox, or maybe RemoveAt.
– jmcilhinney
Nov 9 at 1:19





In your case, "removing" something from the array basically means creating a new array with one less "row" and then copying all the elements before and after the "row" you want to remove. That is cumbersome and completely unnecessary these days. A good developer would define a type for a single record and then create a collection containing instances of that type. Removing an item would involve calling the Remove method of that collection. You could even bind the collection to the ListBox. In your case, you'll still use a Remove method on the ListBox, or maybe RemoveAt.
– jmcilhinney
Nov 9 at 1:19













I know i'm having a hard time with this topic and I've been creating different code just so I can follow what my teachers prompt.
– ihcxx
Nov 9 at 2:10




I know i'm having a hard time with this topic and I've been creating different code just so I can follow what my teachers prompt.
– ihcxx
Nov 9 at 2:10












2 Answers
2






active

oldest

votes

















up vote
0
down vote













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.



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





share|improve this answer




















  • I will try to ask him once I see him, I am really having a hard time to understand or formulate a code with that assignment that we have. I appreciate your help and I will study that code. thank you
    – ihcxx
    Nov 10 at 7:29

















up vote
0
down vote













This should work, though it's really the wrong way to approach this:



LBProducts = Form ListBox



lblQuantity = Form Label



lblPrice = Form Label



btnDelete = Form Button



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





share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53218384%2fhow-to-remove-an-item-in-a-two-dimensional-array-with-a-list-box%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    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.



    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





    share|improve this answer




















    • I will try to ask him once I see him, I am really having a hard time to understand or formulate a code with that assignment that we have. I appreciate your help and I will study that code. thank you
      – ihcxx
      Nov 10 at 7:29














    up vote
    0
    down vote













    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.



    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





    share|improve this answer




















    • I will try to ask him once I see him, I am really having a hard time to understand or formulate a code with that assignment that we have. I appreciate your help and I will study that code. thank you
      – ihcxx
      Nov 10 at 7:29












    up vote
    0
    down vote










    up vote
    0
    down vote









    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.



    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





    share|improve this answer












    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.



    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






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 10 at 3:20









    Mary

    2,6332618




    2,6332618











    • I will try to ask him once I see him, I am really having a hard time to understand or formulate a code with that assignment that we have. I appreciate your help and I will study that code. thank you
      – ihcxx
      Nov 10 at 7:29
















    • I will try to ask him once I see him, I am really having a hard time to understand or formulate a code with that assignment that we have. I appreciate your help and I will study that code. thank you
      – ihcxx
      Nov 10 at 7:29















    I will try to ask him once I see him, I am really having a hard time to understand or formulate a code with that assignment that we have. I appreciate your help and I will study that code. thank you
    – ihcxx
    Nov 10 at 7:29




    I will try to ask him once I see him, I am really having a hard time to understand or formulate a code with that assignment that we have. I appreciate your help and I will study that code. thank you
    – ihcxx
    Nov 10 at 7:29












    up vote
    0
    down vote













    This should work, though it's really the wrong way to approach this:



    LBProducts = Form ListBox



    lblQuantity = Form Label



    lblPrice = Form Label



    btnDelete = Form Button



    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





    share|improve this answer


























      up vote
      0
      down vote













      This should work, though it's really the wrong way to approach this:



      LBProducts = Form ListBox



      lblQuantity = Form Label



      lblPrice = Form Label



      btnDelete = Form Button



      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





      share|improve this answer
























        up vote
        0
        down vote










        up vote
        0
        down vote









        This should work, though it's really the wrong way to approach this:



        LBProducts = Form ListBox



        lblQuantity = Form Label



        lblPrice = Form Label



        btnDelete = Form Button



        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





        share|improve this answer














        This should work, though it's really the wrong way to approach this:



        LBProducts = Form ListBox



        lblQuantity = Form Label



        lblPrice = Form Label



        btnDelete = Form Button



        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






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 10 at 18:17

























        answered Nov 10 at 17:49









        Nathan Champion

        2269




        2269



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53218384%2fhow-to-remove-an-item-in-a-two-dimensional-array-with-a-list-box%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

            Crossroads (UK TV series)

            ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế