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.
vb.net
add a comment |
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.
vb.net
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 aString
, but you cannot remove the element. You can useReDim Preserve
orArray.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 theRemove
method of that collection. You could even bind the collection to theListBox
. In your case, you'll still use aRemove
method on theListBox
, or maybeRemoveAt
.
– 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
add a comment |
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.
vb.net
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
vb.net
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 aString
, but you cannot remove the element. You can useReDim Preserve
orArray.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 theRemove
method of that collection. You could even bind the collection to theListBox
. In your case, you'll still use aRemove
method on theListBox
, or maybeRemoveAt
.
– 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
add a comment |
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 aString
, but you cannot remove the element. You can useReDim Preserve
orArray.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 theRemove
method of that collection. You could even bind the collection to theListBox
. In your case, you'll still use aRemove
method on theListBox
, or maybeRemoveAt
.
– 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
add a comment |
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
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
add a comment |
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Nov 10 at 18:17
answered Nov 10 at 17:49
Nathan Champion
2269
2269
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 aString
, but you cannot remove the element. You can useReDim Preserve
orArray.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 theListBox
. In your case, you'll still use aRemove
method on theListBox
, or maybeRemoveAt
.– 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