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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. Buttons

Buttons

Scheduled Pinned Locked Moved Visual Basic
question
6 Posts 3 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

    I have a button that when clicked the total of set of figures in a listbox is displayed in a textbox. How can I disable that button so that if there is nothing in the listbox and it is clicked, my program does not crash, and it is enabled again when there are contents in the listbox.

    P 1 Reply Last reply
    0
    • R roger6897

      I have a button that when clicked the total of set of figures in a listbox is displayed in a textbox. How can I disable that button so that if there is nothing in the listbox and it is clicked, my program does not crash, and it is enabled again when there are contents in the listbox.

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

      listbox.Items.Count will give you the number of items in the listbox. button.Enabled enables/disables the button.

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

      D 1 Reply Last reply
      0
      • P pmarfleet

        listbox.Items.Count will give you the number of items in the listbox. button.Enabled enables/disables the button.

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

        D Offline
        D Offline
        dexter9703
        wrote on last edited by
        #3

        index = listbox1.SelectedIndex listbox1.Items.RemoveAt(index) The above code is used to remove an item from the listbox. I have tried listbox1.items.count but program still crashed when button was clicked and there were no items in the listbox

        P 1 Reply Last reply
        0
        • D dexter9703

          index = listbox1.SelectedIndex listbox1.Items.RemoveAt(index) The above code is used to remove an item from the listbox. I have tried listbox1.items.count but program still crashed when button was clicked and there were no items in the listbox

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

          It's pretty obvious that this code isn't going to work if there are no items in the listbox. listbox1.SelectedIndex will be -1 if there are no items in the list. Don't execute the second line of code if this is the case.

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

          D 1 Reply Last reply
          0
          • P pmarfleet

            It's pretty obvious that this code isn't going to work if there are no items in the listbox. listbox1.SelectedIndex will be -1 if there are no items in the list. Don't execute the second line of code if this is the case.

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

            D Offline
            D Offline
            dexter9703
            wrote on last edited by
            #5

            Is there a line of code I can use then if index = listbox1.SelectedIndex listbox1.Items.RemoveAt(index) is used, so that the program does not crash when index =-1 I tried using an if statement: if listbox1.selectedindex =-1 then msgbox(" "), but it still crashed.

            P 1 Reply Last reply
            0
            • D dexter9703

              Is there a line of code I can use then if index = listbox1.SelectedIndex listbox1.Items.RemoveAt(index) is used, so that the program does not crash when index =-1 I tried using an if statement: if listbox1.selectedindex =-1 then msgbox(" "), but it still crashed.

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

              You should check the details of the error and amend your code accordingly.

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

              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