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. How to tell if something is selected in my listbox?

How to tell if something is selected in my listbox?

Scheduled Pinned Locked Moved C#
questiontutorial
4 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.
  • D Offline
    D Offline
    dazinith
    wrote on last edited by
    #1

    i have a listbox and a button.. when you click on the button i want to remove what is selected in the listbox, but i need to be able to tell if something is selected in the box or not..

    if (this.lvSDeduc.SelectedItems == null)
    { MessageBox.Show("You must select something to remove before clicking the remove button.");
    return; }

    ListViewItem objItem = this.lvSDeduc.SelectedItems[0];

    this doesnt work, so when it gets to the SelectedItems[0] i crash since there is nothing selected.. any suggestions? thanks in advance! still a newb.. cut me some slack :P -dz

    D M 2 Replies Last reply
    0
    • D dazinith

      i have a listbox and a button.. when you click on the button i want to remove what is selected in the listbox, but i need to be able to tell if something is selected in the box or not..

      if (this.lvSDeduc.SelectedItems == null)
      { MessageBox.Show("You must select something to remove before clicking the remove button.");
      return; }

      ListViewItem objItem = this.lvSDeduc.SelectedItems[0];

      this doesnt work, so when it gets to the SelectedItems[0] i crash since there is nothing selected.. any suggestions? thanks in advance! still a newb.. cut me some slack :P -dz

      D Offline
      D Offline
      Dimitris Iliopoulos
      wrote on last edited by
      #2

      use this code if (this.lvSDeduc.SelectedItems.count == 0) { MessageBox.Show("You must select something to remove before clicking the remove button."); return; }ListViewItem objItem = this.lvSDeduc.SelectedItems[0]; i think it will work From Greece: Dimitris Iliopoulos dimilio@yahoo.com

      D 1 Reply Last reply
      0
      • D Dimitris Iliopoulos

        use this code if (this.lvSDeduc.SelectedItems.count == 0) { MessageBox.Show("You must select something to remove before clicking the remove button."); return; }ListViewItem objItem = this.lvSDeduc.SelectedItems[0]; i think it will work From Greece: Dimitris Iliopoulos dimilio@yahoo.com

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

        doh, should have thought of that one :) thanks a ton! still a newb.. cut me some slack :P -dz

        1 Reply Last reply
        0
        • D dazinith

          i have a listbox and a button.. when you click on the button i want to remove what is selected in the listbox, but i need to be able to tell if something is selected in the box or not..

          if (this.lvSDeduc.SelectedItems == null)
          { MessageBox.Show("You must select something to remove before clicking the remove button.");
          return; }

          ListViewItem objItem = this.lvSDeduc.SelectedItems[0];

          this doesnt work, so when it gets to the SelectedItems[0] i crash since there is nothing selected.. any suggestions? thanks in advance! still a newb.. cut me some slack :P -dz

          M Offline
          M Offline
          Manster
          wrote on last edited by
          #4

          Try this in your button message handler: string sSelectedItem = m_ListBox.SelectedItem.ToString(); if( sSelectedItem != null ) { m_ListBox.Items.Remove( sSelectedItem ); } Hope this helps

          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