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. C#
  4. Checking if any items in a listbox are selected

Checking if any items in a listbox are selected

Scheduled Pinned Locked Moved C#
questionhelp
7 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.
  • L Offline
    L Offline
    Lucy
    wrote on last edited by
    #1

    I have 2 forms. On Form1 I have two listboxes. I want to allow the user to edit items in either listboxes by the use of two edit buttons (associated with 1 listbox each). When an item in listbox1 is selected then no items in listbox2 are selected (and vic versa). So when the first edit button is clicked to edit the selected item in listbox2, form2 is shown with a textbox. In this textbox is the text of the selected item. This should work the same for the second edit button but the selected item in listbox2 should be shown in the textbox on form2. To do this I have created two public strings and two public bools in form1: public string item1 { get { return listBox1.SelectedItem.ToString(); } } public bool selectedItem1 { get { return Convert.ToBoolean(listBox1.SelectedIndex) == true; } } public string item2 { get { return listBox2.SelectedItem.ToString(); } } public bool selectedItem2 { get { return Convert.ToBoolean(listBox2.SelectedIndex) == true; } } In form2's load event I have the following if statement to check if listbox1 has a selected item or if listbox2 has a selected item: if (Form1.selectedItem1 == true) { textBox1.Text = Form1.item1; } else if (Form1.selectedItem2 == true) { textBox1.Text = Form1.item2; } I have used a break point to follow this if statement and it seems as though it is not checking which listbox has an item selected. Can anyone help please? How can I check which listbox has a selected item. Or can I turn this code into a public bool variable?: if (listBox1.SelectedIndex >= 0) Lucy

    L L 2 Replies Last reply
    0
    • L Lucy

      I have 2 forms. On Form1 I have two listboxes. I want to allow the user to edit items in either listboxes by the use of two edit buttons (associated with 1 listbox each). When an item in listbox1 is selected then no items in listbox2 are selected (and vic versa). So when the first edit button is clicked to edit the selected item in listbox2, form2 is shown with a textbox. In this textbox is the text of the selected item. This should work the same for the second edit button but the selected item in listbox2 should be shown in the textbox on form2. To do this I have created two public strings and two public bools in form1: public string item1 { get { return listBox1.SelectedItem.ToString(); } } public bool selectedItem1 { get { return Convert.ToBoolean(listBox1.SelectedIndex) == true; } } public string item2 { get { return listBox2.SelectedItem.ToString(); } } public bool selectedItem2 { get { return Convert.ToBoolean(listBox2.SelectedIndex) == true; } } In form2's load event I have the following if statement to check if listbox1 has a selected item or if listbox2 has a selected item: if (Form1.selectedItem1 == true) { textBox1.Text = Form1.item1; } else if (Form1.selectedItem2 == true) { textBox1.Text = Form1.item2; } I have used a break point to follow this if statement and it seems as though it is not checking which listbox has an item selected. Can anyone help please? How can I check which listbox has a selected item. Or can I turn this code into a public bool variable?: if (listBox1.SelectedIndex >= 0) Lucy

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, are the listbox items selected when the form loads? what happens when the selection changes after form load? My view (assuming you can select at most one item per listbox): - add OnSelectedIndexChanged event handlers; - do all your related stuff in there; - don't be surprised if OnSelectedIndexChanged also fires when an item gets unselected; - there is no need for separate variables such as selectedItem1, adding them increases the chance your object state becomes inconsistent, and that would make debugging much more complex. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


      L A 2 Replies Last reply
      0
      • L Luc Pattyn

        Hi, are the listbox items selected when the form loads? what happens when the selection changes after form load? My view (assuming you can select at most one item per listbox): - add OnSelectedIndexChanged event handlers; - do all your related stuff in there; - don't be surprised if OnSelectedIndexChanged also fires when an item gets unselected; - there is no need for separate variables such as selectedItem1, adding them increases the chance your object state becomes inconsistent, and that would make debugging much more complex. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


        L Offline
        L Offline
        Lucy
        wrote on last edited by
        #3

        Thank you for your comment. Form2 can only load when a listbox item is selected. I already have a SelectedIndexChanged event for each listbox that has an if statement similar to this: if (listBox1.SelectedIndex >= 0) { buttonEdit1.Enabled = true; listBox2.SelectedIndex = -1; } else { buttonEdit1.Enabled = false; } Lucy

        1 Reply Last reply
        0
        • L Lucy

          I have 2 forms. On Form1 I have two listboxes. I want to allow the user to edit items in either listboxes by the use of two edit buttons (associated with 1 listbox each). When an item in listbox1 is selected then no items in listbox2 are selected (and vic versa). So when the first edit button is clicked to edit the selected item in listbox2, form2 is shown with a textbox. In this textbox is the text of the selected item. This should work the same for the second edit button but the selected item in listbox2 should be shown in the textbox on form2. To do this I have created two public strings and two public bools in form1: public string item1 { get { return listBox1.SelectedItem.ToString(); } } public bool selectedItem1 { get { return Convert.ToBoolean(listBox1.SelectedIndex) == true; } } public string item2 { get { return listBox2.SelectedItem.ToString(); } } public bool selectedItem2 { get { return Convert.ToBoolean(listBox2.SelectedIndex) == true; } } In form2's load event I have the following if statement to check if listbox1 has a selected item or if listbox2 has a selected item: if (Form1.selectedItem1 == true) { textBox1.Text = Form1.item1; } else if (Form1.selectedItem2 == true) { textBox1.Text = Form1.item2; } I have used a break point to follow this if statement and it seems as though it is not checking which listbox has an item selected. Can anyone help please? How can I check which listbox has a selected item. Or can I turn this code into a public bool variable?: if (listBox1.SelectedIndex >= 0) Lucy

          L Offline
          L Offline
          led mike
          wrote on last edited by
          #4

          Lucy_H85 wrote:

          To do this I have created two public strings and two public bools in form1:

          Lucy, have you heard about Object Oriented Design? Software Design Patterns? Here is one that pertains to your post[^]

          L 1 Reply Last reply
          0
          • L led mike

            Lucy_H85 wrote:

            To do this I have created two public strings and two public bools in form1:

            Lucy, have you heard about Object Oriented Design? Software Design Patterns? Here is one that pertains to your post[^]

            L Offline
            L Offline
            Lucy
            wrote on last edited by
            #5

            Yes this is what I am currently learning. Sorry I am not an expert yet. Lucy

            L 1 Reply Last reply
            0
            • L Lucy

              Yes this is what I am currently learning. Sorry I am not an expert yet. Lucy

              L Offline
              L Offline
              led mike
              wrote on last edited by
              #6

              Lucy_H85 wrote:

              Yes this is what I am currently learning

              So am I. I doubt I will ever stop learning.

              Lucy_H85 wrote:

              Sorry I am not an expert yet.

              Neither am I, no need to apologize. :-D

              1 Reply Last reply
              0
              • L Luc Pattyn

                Hi, are the listbox items selected when the form loads? what happens when the selection changes after form load? My view (assuming you can select at most one item per listbox): - add OnSelectedIndexChanged event handlers; - do all your related stuff in there; - don't be surprised if OnSelectedIndexChanged also fires when an item gets unselected; - there is no need for separate variables such as selectedItem1, adding them increases the chance your object state becomes inconsistent, and that would make debugging much more complex. :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


                A Offline
                A Offline
                alisardar
                wrote on last edited by
                #7

                list box has event that its name is "SelectedIndexChanged" you can work with it . bye

                ali sardar

                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