0

I'm using .net CF 3.5 to develop an application for Windows Mobile 5.0

I have a ListBox with a list of numbers in. When they click on a number in the list, it should be deleted from the list. I can get the selected item's index just fine from the list - However, I cannot seem to delete it without the application crashing. There are no errors, it will just exit with code 0.

private void DeleteOrder(object sender, EventArgs e) {
  string s = (string) orders_list.SelectedItem;
  int size = orders_list.Items.Count;


  bool con = true;

  for (int i = 0; i < size; i++) {
    Debug.Write("\nIS: " + i + "\n");
    if (con) {
      if (s != null || s != "") {
        if (orders_list.GetItemText(orders_list.Items[i]) != null) {
          if (orders_list.GetItemText(orders_list.Items[i]).ToString() == s) {
            if (orders_list.Items[i] != null) {
              Debug.Write("ORDER IS : " + orders_list.Items[i].ToString());
              orders_list.Items.Remove(orders_list.Items[i].ToString());
            }

            con = false;
          }
        }
      }
    }
  }

  input_scan.Text = "";
  this.BackColor = Color.Lime;

  input_scan.Focus();
}
}
}

As you can see, I've tried doing

orders_list.Items.Remove(orders_list.Items[i].ToString());

to see if I can remove the string object and I've tried using RemoveAt() to remove by index, but each time the application will keep crashing.

Any help would be appreciated as something so simple such as removing from a collection has become rather a challenge.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
Connor Brady
  • 81
  • 2
  • 10
  • 1
    I strongly suspect that an exception is being thrown. You need to find out what that exception is. – Jon Skeet Dec 06 '17 at 14:49
  • `if(a) if(b) if (c) if (d) { }` if better formulated as `if(a && b && c && d) {}`. Because C# does a [Short-Circuit Evaluation (c-sharpcorner)](http://www.c-sharpcorner.com/article/short-circuit-evaluation-in-c-sharp/), both perform with equal efficency. Se also: [&& Operator (C# Reference)](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-and-operator) and [Is relying on && short-circuiting safe in .NET? (SO)](https://stackoverflow.com/questions/4820610/is-relying-on-short-circuiting-safe-in-net). – Olivier Jacot-Descombes Dec 06 '17 at 15:03
  • What is the exception you are getting? If you wrap the logic in a try/catch, you can log the details. – tj-cappelletti Dec 06 '17 at 18:52
  • 1
    The way this function is called is missing. Possibly you are trying a cross thread operation, which is not allowed (but the exception message is missing). You may need to use a dlegate to remove the Item. – josef Dec 07 '17 at 05:29
  • This is incredibly odd but I have thrown the method into a Try/Catch and it appears to work flawlessly without any issues described above. Thank you to @virusstorm for suggesting this. I'll mark as solved and update my answer. – Connor Brady Dec 11 '17 at 09:13
  • 1
    @ConnorBrady The try/catch just prevents the error from crashing your application. You still need to get to the root cause of the exception if you want to fix your application correctly. – tj-cappelletti Dec 12 '17 at 02:15

0 Answers0