-1

i was doing an ordering interface and i got stuck at the point of the delete.

e.g: order interface

at starting, i will click the 'Add to Order' button, which does the multiplication of the foodprice and quantity then store into an array.

private int c = 0;
private decimal[] ordersarray = new decimal[10];
List<decimal> items = new List<decimal>();

private void fconfirmbutton_Click(object sender, EventArgs e)
    {
        int fquantity;
        if (int.TryParse(fquantitytextbox.Text, out fquantity))
        {
            fquantity = int.Parse(fquantitytextbox.Text);
            if (fquantity.ToString() != "" + 0)
            {
                decimal mulxprice, quan, total;
                mulxprice = decimal.Parse(fpricetextbox.Text);
                quan = int.Parse(fquantitytextbox.Text);
                total = mulxprice * quan;
                cartlistbox.Items.Add(foodnametextbox.Text + "  x" + quan.ToString() + " ($" + total.ToString() + ") ");
                subtotalbox.Text = total.ToString();
                decimal b;
                try
                {
                    if (decimal.TryParse(subtotalbox.Text, out b))
                    {
                        ordersarray[c] = b;
                        c++;
                        subtotalbox.Text = "";
                    }
                }
                catch
                {
                    MessageBox.Show("Sorry, the order system can only takes up to 10 orders at a time only.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                fquantitytextbox.Clear();
                fquantitytextbox.Focus();
            }

after it is stored into the cartlistbox and ordersarray, i try to delete it by using :

EDIT :

 private void deletebutton_Click(object sender, EventArgs e)
    {
        try
        {
            if (this.cartlistbox.SelectedIndex >= 0)
            {
                cartlistbox.Items.Remove(cartlistbox.SelectedItem);
                cartlistbox.Refresh();
            }
            else
            {
                MessageBox.Show("Error, please select an item to delete.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

for instance, i might order 2 items, e.g:

listbox example

so i attempt to delete item 'b', by selecting it from the listbox and clicking the 'Delete' button, however it always ends up with "index was outside the bounds of the array". so i understand that this was sort of the array crashing after removing of the array value completely, but i have no idea how.

im sorry as this may be confusing but i hope you understand what i might need.

Feii Momo
  • 13
  • 3
  • Do you want to know why it is throwing the error or the solution to prevent it? – Josh Adams Jan 11 '18 at 18:48
  • 1
    why do you set the selected item to 0? : `cartlistbox.SelectedItem = 0;` – Mong Zhu Jan 11 '18 at 19:00
  • @JoshAdams yes, i want to know the solution to solve it. basically after i delete a value that has been stored in the array, it'll crash. so what should i do to prevent this from happening? create a new array? or is it possible to replace that specific selected value to "0" again so its possible for another value to be store in the array. – Feii Momo Jan 12 '18 at 17:17
  • @MongZhu sorry it was unnecessary in the code so i removed it now, but i still did not find a way to get to the solution. – Feii Momo Jan 12 '18 at 17:28

1 Answers1

-3

Try to change this:

cartlistbox.Items.Remove(cartlistbox.SelectedItem);

to this:

cartlistbox.Items.Remove(cartlistbox.SelectedIndex);
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
  • have a look at the [documentation of Remove](https://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.objectcollection.remove(v=vs.110).aspx) method. It takes the `object` to be removed as parameter. not the index – Mong Zhu Jan 11 '18 at 18:59
  • 1
    [RemoveAt](https://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.objectcollection.removeat(v=vs.110).aspx) takes the index – Mong Zhu Jan 11 '18 at 18:59