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. Deleting Items and Selecting next one on ListBox on C#

Deleting Items and Selecting next one on ListBox on C#

Scheduled Pinned Locked Moved C#
csharpdatabasetutorialquestion
8 Posts 4 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.
  • T Offline
    T Offline
    three leaf
    wrote on last edited by
    #1

    Hi Does anyone know how to delete an item in a ListBox and then, select the next one or previous one in the listbox? Currently, I was using the next code:

    int index = listBox1.SelectedIndex;
    listBox1.Items.RemoveAt(index);
    Reestablecer();
    int cuenta = listBox1.Items.Count;
    if (index < cuenta)
    {
    index++;
    listBox1.SelectedIndex = index;
    }
    else
    {
    index--;
    listBox1.SelectedIndex = index;
    }

    but i have a event on SelectedIndexChanged on the ListBox, so when I delete the item in the listbox, automatically the code on the SelectIndexChanged is executed and i get errors because the value of the property listbox.selecteditem is null. Any ideas of how to solve it? thanks in advance. :((

    ~ Bizarre what men fin attractive ~

    P M realJSOPR 3 Replies Last reply
    0
    • T three leaf

      Hi Does anyone know how to delete an item in a ListBox and then, select the next one or previous one in the listbox? Currently, I was using the next code:

      int index = listBox1.SelectedIndex;
      listBox1.Items.RemoveAt(index);
      Reestablecer();
      int cuenta = listBox1.Items.Count;
      if (index < cuenta)
      {
      index++;
      listBox1.SelectedIndex = index;
      }
      else
      {
      index--;
      listBox1.SelectedIndex = index;
      }

      but i have a event on SelectedIndexChanged on the ListBox, so when I delete the item in the listbox, automatically the code on the SelectIndexChanged is executed and i get errors because the value of the property listbox.selecteditem is null. Any ideas of how to solve it? thanks in advance. :((

      ~ Bizarre what men fin attractive ~

      P Offline
      P Offline
      Parwej Ahamad
      wrote on last edited by
      #2

      When you have written this code ? Did you try to catch last removed index value in SelectIndexChanged and manipulated your existing code as you mentioned

      int cuenta = listBox1.Items.Count;
      if (index < cuenta)
      {
      index++;
      listBox1.SelectedIndex = index;
      }
      else
      {
      index--;
      listBox1.SelectedIndex = index;
      }

      Parwej Ahamad g.parwez@gmail.com

      1 Reply Last reply
      0
      • T three leaf

        Hi Does anyone know how to delete an item in a ListBox and then, select the next one or previous one in the listbox? Currently, I was using the next code:

        int index = listBox1.SelectedIndex;
        listBox1.Items.RemoveAt(index);
        Reestablecer();
        int cuenta = listBox1.Items.Count;
        if (index < cuenta)
        {
        index++;
        listBox1.SelectedIndex = index;
        }
        else
        {
        index--;
        listBox1.SelectedIndex = index;
        }

        but i have a event on SelectedIndexChanged on the ListBox, so when I delete the item in the listbox, automatically the code on the SelectIndexChanged is executed and i get errors because the value of the property listbox.selecteditem is null. Any ideas of how to solve it? thanks in advance. :((

        ~ Bizarre what men fin attractive ~

        M Offline
        M Offline
        Michael Bookatz
        wrote on last edited by
        #3

        Your problem is the remove index, when you remove an item you automatically move everything up so don't need to change selected index

        T 1 Reply Last reply
        0
        • T three leaf

          Hi Does anyone know how to delete an item in a ListBox and then, select the next one or previous one in the listbox? Currently, I was using the next code:

          int index = listBox1.SelectedIndex;
          listBox1.Items.RemoveAt(index);
          Reestablecer();
          int cuenta = listBox1.Items.Count;
          if (index < cuenta)
          {
          index++;
          listBox1.SelectedIndex = index;
          }
          else
          {
          index--;
          listBox1.SelectedIndex = index;
          }

          but i have a event on SelectedIndexChanged on the ListBox, so when I delete the item in the listbox, automatically the code on the SelectIndexChanged is executed and i get errors because the value of the property listbox.selecteditem is null. Any ideas of how to solve it? thanks in advance. :((

          ~ Bizarre what men fin attractive ~

          realJSOPR Offline
          realJSOPR Offline
          realJSOP
          wrote on last edited by
          #4

          int index = listBox1.SelectedIndex;
          if (index >= 0)
          {
          listBox1.Items.RemoveAt(index);
          Reestablecer();
          // you only need to change the selected index if it is > the new last item
          int lastItem = listBox1.Items.Count - 1;
          if (index > lastItem)
          {
          listBox1.SelectedIndex = lastItem;
          }
          }

          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
          -----
          "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

          T 1 Reply Last reply
          0
          • M Michael Bookatz

            Your problem is the remove index, when you remove an item you automatically move everything up so don't need to change selected index

            T Offline
            T Offline
            three leaf
            wrote on last edited by
            #5

            I tried to only remove the item, but the program still executes the selectindexchanged statement. I get the error NullRefefenceException.

            ~ Bizarre what men fin attractive ~

            1 Reply Last reply
            0
            • realJSOPR realJSOP

              int index = listBox1.SelectedIndex;
              if (index >= 0)
              {
              listBox1.Items.RemoveAt(index);
              Reestablecer();
              // you only need to change the selected index if it is > the new last item
              int lastItem = listBox1.Items.Count - 1;
              if (index > lastItem)
              {
              listBox1.SelectedIndex = lastItem;
              }
              }

              "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
              -----
              "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

              T Offline
              T Offline
              three leaf
              wrote on last edited by
              #6

              your code worked fine! Now i just need to check it few more times, because after i delete few items it gets again the null reference exception thank you :) ;)

              ~ Bizarre what men fin attractive ~

              realJSOPR 2 Replies Last reply
              0
              • T three leaf

                your code worked fine! Now i just need to check it few more times, because after i delete few items it gets again the null reference exception thank you :) ;)

                ~ Bizarre what men fin attractive ~

                realJSOPR Offline
                realJSOPR Offline
                realJSOP
                wrote on last edited by
                #7

                Try this:

                int index = listBox1.SelectedIndex;
                if (index >= 0)
                {
                listBox1.Items.RemoveAt(index);
                Reestablecer();
                // you only need to change the selected index if it is > the new last item
                int lastItem = listBox1.Items.Count - 1;

                // if there are items in the listbox
                if (lastItem >= 0)
                {
                    // if the current index is > than the last item
                    if (index > lastItem)
                    {
                        // change the selected index
                        listBox1.SelectedIndex = lastItem;
                    }
                }
                else
                {
                    // no items in the listbox, so we make the selected index -1
                    listBox1.SelectedIndex = -1;
                }
                

                }

                "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                -----
                "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                1 Reply Last reply
                0
                • T three leaf

                  your code worked fine! Now i just need to check it few more times, because after i delete few items it gets again the null reference exception thank you :) ;)

                  ~ Bizarre what men fin attractive ~

                  realJSOPR Offline
                  realJSOPR Offline
                  realJSOP
                  wrote on last edited by
                  #8

                  Or this:

                  int index = listBox1.SelectedIndex;
                  if (index >= 0)
                  {
                  listBox1.Items.RemoveAt(index);
                  Reestablecer();
                  // you only need to change the selected index if it is > the new last item
                  int lastItem = listBox1.Items.Count - 1;

                  listBox1.SelectedIndex = Math.Min(lastItem, index);
                  

                  }

                  "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                  -----
                  "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                  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