Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. listBox Item Removal

listBox Item Removal

Scheduled Pinned Locked Moved C#
questionhelp
13 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    electriac
    wrote on last edited by
    #1

    //My list box contains some empty lines or lines just containing a "\t" and sometimes I want to remove one of these.
    //If the selected line contains text the line is removed without problem but when the selected line is blank "", " ", or "\t"
    // the removal goes to the first blank line and removes it rather than the selected line.

    selLoc = ListBox1.SelectedIndex;
    listBox1.Items.Remove(listBox1.SelectedItems[0]);
    // Removes The first occurance of empty line (not selected)

    // and this is the same
    listBox1.Items.Remove(listBox1.Items[selLoc]);
    // Removes The first occurance of empty line (not selected)

    //How can I remove a blank selected line?</pre>

    E B B 3 Replies Last reply
    0
    • E electriac

      //My list box contains some empty lines or lines just containing a "\t" and sometimes I want to remove one of these.
      //If the selected line contains text the line is removed without problem but when the selected line is blank "", " ", or "\t"
      // the removal goes to the first blank line and removes it rather than the selected line.

      selLoc = ListBox1.SelectedIndex;
      listBox1.Items.Remove(listBox1.SelectedItems[0]);
      // Removes The first occurance of empty line (not selected)

      // and this is the same
      listBox1.Items.Remove(listBox1.Items[selLoc]);
      // Removes The first occurance of empty line (not selected)

      //How can I remove a blank selected line?</pre>

      E Offline
      E Offline
      electriac
      wrote on last edited by
      #2

      Found solution selLoc = listBox1.SelectedIndex; listBox1.Items.RemoveAt(selLoc);

      V 1 Reply Last reply
      0
      • E electriac

        Found solution selLoc = listBox1.SelectedIndex; listBox1.Items.RemoveAt(selLoc);

        V Offline
        V Offline
        V 0
        wrote on last edited by
        #3

        5 because you found the answer I was about to give you, yourself :-D

        V.
        (MQOTD Rules )

        1 Reply Last reply
        0
        • E electriac

          //My list box contains some empty lines or lines just containing a "\t" and sometimes I want to remove one of these.
          //If the selected line contains text the line is removed without problem but when the selected line is blank "", " ", or "\t"
          // the removal goes to the first blank line and removes it rather than the selected line.

          selLoc = ListBox1.SelectedIndex;
          listBox1.Items.Remove(listBox1.SelectedItems[0]);
          // Removes The first occurance of empty line (not selected)

          // and this is the same
          listBox1.Items.Remove(listBox1.Items[selLoc]);
          // Removes The first occurance of empty line (not selected)

          //How can I remove a blank selected line?</pre>

          B Offline
          B Offline
          BobJanova
          wrote on last edited by
          #4

          The reason that this happens is because Remove uses equality checking to work out which line you meant – and the first line with the same text will match. (I really don't think it should do that, but that's a separate discussion.) The answer, as you discovered, is to use RemoveAt.

          E D 2 Replies Last reply
          0
          • B BobJanova

            The reason that this happens is because Remove uses equality checking to work out which line you meant – and the first line with the same text will match. (I really don't think it should do that, but that's a separate discussion.) The answer, as you discovered, is to use RemoveAt.

            E Offline
            E Offline
            electriac
            wrote on last edited by
            #5

            thanks for the explanation. I didn't feel I had an answer although the fix worked.

            1 Reply Last reply
            0
            • B BobJanova

              The reason that this happens is because Remove uses equality checking to work out which line you meant – and the first line with the same text will match. (I really don't think it should do that, but that's a separate discussion.) The answer, as you discovered, is to use RemoveAt.

              D Offline
              D Offline
              DaveyM69
              wrote on last edited by
              #6

              BobJanova wrote:

              I really don't think it should do that, but that's a separate discussion

              ... I agree, and you are correct about the equality check. It should use reference checking IMO but then that wouldn't work with boxed value types. Hmm..

              Dave
              Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
              BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

              E 1 Reply Last reply
              0
              • E electriac

                //My list box contains some empty lines or lines just containing a "\t" and sometimes I want to remove one of these.
                //If the selected line contains text the line is removed without problem but when the selected line is blank "", " ", or "\t"
                // the removal goes to the first blank line and removes it rather than the selected line.

                selLoc = ListBox1.SelectedIndex;
                listBox1.Items.Remove(listBox1.SelectedItems[0]);
                // Removes The first occurance of empty line (not selected)

                // and this is the same
                listBox1.Items.Remove(listBox1.Items[selLoc]);
                // Removes The first occurance of empty line (not selected)

                //How can I remove a blank selected line?</pre>

                B Offline
                B Offline
                Braj_12
                wrote on last edited by
                #7

                Use listBox1.Items.RemoveAt(ListBox1.SelectedIndex); in place of your code then it will remove your selected item from List Box.

                E 1 Reply Last reply
                0
                • D DaveyM69

                  BobJanova wrote:

                  I really don't think it should do that, but that's a separate discussion

                  ... I agree, and you are correct about the equality check. It should use reference checking IMO but then that wouldn't work with boxed value types. Hmm..

                  Dave
                  Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
                  BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                  E Offline
                  E Offline
                  electriac
                  wrote on last edited by
                  #8

                  Yeah! I will just add it to my list of MS idiosyncratic anomalies.

                  D 1 Reply Last reply
                  0
                  • B Braj_12

                    Use listBox1.Items.RemoveAt(ListBox1.SelectedIndex); in place of your code then it will remove your selected item from List Box.

                    E Offline
                    E Offline
                    electriac
                    wrote on last edited by
                    #9

                    As I discovered but the listBox1.Remove is logical. Why create this special case.

                    1 Reply Last reply
                    0
                    • E electriac

                      Yeah! I will just add it to my list of MS idiosyncratic anomalies.

                      D Offline
                      D Offline
                      DaveyM69
                      wrote on last edited by
                      #10

                      I've just had a quick play with this and actually, it does use reference equality. What's happening is due to string interning - if you don't know what that is have a google :) So, when you add your strings to the list box, if they are the same text then they are actually the same reference, therefore it will remove the first one it finds. A quick solution is to wrap a string in another class and use that in place of string.

                      public class MyString
                      {
                      private string value;

                      public MyString(string value)
                      {
                          this.value = value;
                      }
                      
                      public static implicit operator string(MyString myString)
                      {
                          return myString.value;
                      }
                      public static implicit operator MyString(string value)
                      {
                          return new MyString(value);
                      }
                      
                      public override string ToString()
                      {
                          return value;
                      }
                      

                      }

                      MyString one = " ";
                      MyString two = "2";
                      MyString three = " ";

                      listBox.Items.Add(one);
                      listBox.Items.Add(two);
                      listBox.Items.Add(three);

                      Selecting the third item and calling listBox.Items.Remove(listBox.SelectedItem); successfully removes the third item.

                      Dave
                      Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
                      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                      B E 2 Replies Last reply
                      0
                      • D DaveyM69

                        I've just had a quick play with this and actually, it does use reference equality. What's happening is due to string interning - if you don't know what that is have a google :) So, when you add your strings to the list box, if they are the same text then they are actually the same reference, therefore it will remove the first one it finds. A quick solution is to wrap a string in another class and use that in place of string.

                        public class MyString
                        {
                        private string value;

                        public MyString(string value)
                        {
                            this.value = value;
                        }
                        
                        public static implicit operator string(MyString myString)
                        {
                            return myString.value;
                        }
                        public static implicit operator MyString(string value)
                        {
                            return new MyString(value);
                        }
                        
                        public override string ToString()
                        {
                            return value;
                        }
                        

                        }

                        MyString one = " ";
                        MyString two = "2";
                        MyString three = " ";

                        listBox.Items.Add(one);
                        listBox.Items.Add(two);
                        listBox.Items.Add(three);

                        Selecting the third item and calling listBox.Items.Remove(listBox.SelectedItem); successfully removes the third item.

                        Dave
                        Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
                        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                        B Offline
                        B Offline
                        BobJanova
                        wrote on last edited by
                        #11

                        Oh, my mistake then. E: Although your test doesn't actually test what you think it does. The MyString class has no equality checking, so it will always use reference equality. Try adding Equals, ==, != and Hashcode to it and see what the result is.

                        D 1 Reply Last reply
                        0
                        • D DaveyM69

                          I've just had a quick play with this and actually, it does use reference equality. What's happening is due to string interning - if you don't know what that is have a google :) So, when you add your strings to the list box, if they are the same text then they are actually the same reference, therefore it will remove the first one it finds. A quick solution is to wrap a string in another class and use that in place of string.

                          public class MyString
                          {
                          private string value;

                          public MyString(string value)
                          {
                              this.value = value;
                          }
                          
                          public static implicit operator string(MyString myString)
                          {
                              return myString.value;
                          }
                          public static implicit operator MyString(string value)
                          {
                              return new MyString(value);
                          }
                          
                          public override string ToString()
                          {
                              return value;
                          }
                          

                          }

                          MyString one = " ";
                          MyString two = "2";
                          MyString three = " ";

                          listBox.Items.Add(one);
                          listBox.Items.Add(two);
                          listBox.Items.Add(three);

                          Selecting the third item and calling listBox.Items.Remove(listBox.SelectedItem); successfully removes the third item.

                          Dave
                          Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
                          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                          E Offline
                          E Offline
                          electriac
                          wrote on last edited by
                          #12

                          Since my list box is populated from several functions and has over 50,000 lines it would seem the simple "RemoveAt" solves the problem. But thanks for the clarification and example.

                          1 Reply Last reply
                          0
                          • B BobJanova

                            Oh, my mistake then. E: Although your test doesn't actually test what you think it does. The MyString class has no equality checking, so it will always use reference equality. Try adding Equals, ==, != and Hashcode to it and see what the result is.

                            D Offline
                            D Offline
                            DaveyM69
                            wrote on last edited by
                            #13

                            Yes, that was deliberate as the OP was wanting reference checking so two identical strings were treated as different objects. Even with those added it behaves as expected - it's just the interning of the string class that screws it! Example below. Warning: No null checking on value

                            using System;
                            using System.Windows.Forms;

                            namespace WindowsFormsApplication1
                            {
                            public partial class Form1 : Form
                            {
                            // listBox added in designer
                            // buttonRemove added in designer
                            // buttonRemove Click handler added in designer
                            public Form1()
                            {
                            InitializeComponent();
                            listBox.Items.Add(new MyString(" "));
                            listBox.Items.Add(new MyString("2"));
                            listBox.Items.Add(new MyString(" "));
                            }

                                private void buttonRemove\_Click(object sender, EventArgs e)
                                {
                                    if (listBox.SelectedItem != null)
                                        listBox.Items.Remove(listBox.SelectedItem);
                                }
                            }
                            

                            }

                            public class MyString : IEquatable
                            {
                            private string value;

                            public MyString(string value)
                            {
                                this.value = value;
                            }
                            
                            public static implicit operator MyString(string value)
                            {
                                return new MyString(value);
                            }
                            public static implicit operator string(MyString myString)
                            {
                                return myString.value;
                            }
                            public static bool operator ==(MyString first, MyString other)
                            {
                                if (object.ReferenceEquals(first, other))
                                    return true;
                                if (object.ReferenceEquals(null, first) || object.ReferenceEquals(null, other))
                                    return false;
                                return first.value == other.value;
                            }
                            public static bool operator !=(MyString first, MyString other)
                            {
                                return !(first == other);
                            }
                            
                            public override bool Equals(object obj)
                            {
                                return Equals(obj as MyString);
                            }
                            public bool Equals(MyString other)
                            {
                                if (object.ReferenceEquals(null, other))
                                    return false;
                                return value.Equals(other.value);
                            }
                            public override int GetHashCode()
                            {
                                return value.GetHashCode();
                            }
                            public override string ToString()
                            {
                                return value;
                            }
                            

                            }

                            Dave
                            Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us.

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups