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. Visual Basic
  4. Index

Index

Scheduled Pinned Locked Moved Visual Basic
questiondatabase
5 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.
  • R Offline
    R Offline
    roger6897
    wrote on last edited by
    #1

    Dim index As Integer index = listbox1.SelectedIndex listbox1.Items.RemoveAt(index) The above code is used to remove the seleceted item from a listbox. However the program crashes when the button is clicked and no item is selected. How can I correct??

    D P M 3 Replies Last reply
    0
    • R roger6897

      Dim index As Integer index = listbox1.SelectedIndex listbox1.Items.RemoveAt(index) The above code is used to remove the seleceted item from a listbox. However the program crashes when the button is clicked and no item is selected. How can I correct??

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Check the value of SelectedIndex before you use it. SelectedItem will be -1 if there is nothing selected. You might also want to check to see if the number of Items in the Items collection (see it's Count property?) is greater than zero before you try to remove something from it.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007

      1 Reply Last reply
      0
      • R roger6897

        Dim index As Integer index = listbox1.SelectedIndex listbox1.Items.RemoveAt(index) The above code is used to remove the seleceted item from a listbox. However the program crashes when the button is clicked and no item is selected. How can I correct??

        P Offline
        P Offline
        pmarfleet
        wrote on last edited by
        #3

        You asked this question yesterday and I answered it[^]. Which part of my advice did you not understand?

        Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

        CPalliniC 1 Reply Last reply
        0
        • R roger6897

          Dim index As Integer index = listbox1.SelectedIndex listbox1.Items.RemoveAt(index) The above code is used to remove the seleceted item from a listbox. However the program crashes when the button is clicked and no item is selected. How can I correct??

          M Offline
          M Offline
          Mitch F
          wrote on last edited by
          #4

          As the other posters above have stated, SelectedIndex will return -1 if there is nothing selected, so all you have to do is write an IF statement around it. Dim index As Integer index = listbox1.SelectedIndex If index > -1 Then listbox1.Items.RemoveAt(index)

          1 Reply Last reply
          0
          • P pmarfleet

            You asked this question yesterday and I answered it[^]. Which part of my advice did you not understand?

            Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            Maybe he wants to be sure, but very sure, so he asks to the whole village! :-D

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

            In testa che avete, signor di Ceprano?

            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