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. Get the value from a checklistbox

Get the value from a checklistbox

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

    I have a checklistBox where i populate only name. But the valueMember is an id. The user can select several name. I have this code to read the checklistbox (only checked value) and extract the valuemember. but i only get the item it is selected not all chechking if (chkListClienteEmail.CheckedItems.Count != 0) { for (int i = 0; i <=;= chkListClienteEmail.CheckedItems.Count - 1; i++) { string s = chkListClienteEmail.SelectedValue.ToString(); more code... } }

    L A 2 Replies Last reply
    0
    • L Luis M Rojas

      I have a checklistBox where i populate only name. But the valueMember is an id. The user can select several name. I have this code to read the checklistbox (only checked value) and extract the valuemember. but i only get the item it is selected not all chechking if (chkListClienteEmail.CheckedItems.Count != 0) { for (int i = 0; i <=;= chkListClienteEmail.CheckedItems.Count - 1; i++) { string s = chkListClienteEmail.SelectedValue.ToString(); more code... } }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You didn't mention .SelectionMode (but did mention "only" and "not all") [CheckedListBox.SelectionMode Property (System.Windows.Forms) | Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.checkedlistbox.selectionmode?view=net-5.0)

      It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

      L 1 Reply Last reply
      0
      • L Lost User

        You didn't mention .SelectionMode (but did mention "only" and "not all") [CheckedListBox.SelectionMode Property (System.Windows.Forms) | Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.checkedlistbox.selectionmode?view=net-5.0)

        It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

        L Offline
        L Offline
        Luis M Rojas
        wrote on last edited by
        #3

        I have chkListClienteEmail.SelectionMode = SelectionMode.One; The problem is: if the user check 3 clients, even i read every checked=true, i just get one selectedValue(valueMember) and it is the Last one, which of course it is selected on the form. I want that on every loop, if the item it is checked, i need that valuemember.

        L 2 Replies Last reply
        0
        • L Luis M Rojas

          I have chkListClienteEmail.SelectionMode = SelectionMode.One; The problem is: if the user check 3 clients, even i read every checked=true, i just get one selectedValue(valueMember) and it is the Last one, which of course it is selected on the form. I want that on every loop, if the item it is checked, i need that valuemember.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          You're no really "indexing" in your for loop (nothing happens with i). Use a foreach to iterate either the indexes or the "checked" items.

          Quote:

          The following example enumerates the checked items in the CheckedListBox.

          [CheckedListBox.CheckedItems Property (System.Windows.Forms) | Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.checkedlistbox.checkeditems?view=net-5.0)

          It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

          1 Reply Last reply
          0
          • L Luis M Rojas

            I have chkListClienteEmail.SelectionMode = SelectionMode.One; The problem is: if the user check 3 clients, even i read every checked=true, i just get one selectedValue(valueMember) and it is the Last one, which of course it is selected on the form. I want that on every loop, if the item it is checked, i need that valuemember.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Look at your loop. You extract every email value to the string s, so at the end of the loop it contains the last selected item.

            1 Reply Last reply
            0
            • L Luis M Rojas

              I have a checklistBox where i populate only name. But the valueMember is an id. The user can select several name. I have this code to read the checklistbox (only checked value) and extract the valuemember. but i only get the item it is selected not all chechking if (chkListClienteEmail.CheckedItems.Count != 0) { for (int i = 0; i <=;= chkListClienteEmail.CheckedItems.Count - 1; i++) { string s = chkListClienteEmail.SelectedValue.ToString(); more code... } }

              A Offline
              A Offline
              Alan N
              wrote on last edited by
              #6

              There is some confusion here as selection and checking are quite separate things and a selected item need not be checked. There is a SelectedValue property but there is no CheckedValue property. I'll assume that the checkedlistbox item collection stores instances of a class such as

              class NameID {
              public String Name {get; set;};
              public Int32 ID {get; set;}
              }

              To iterate though the ID's of the checkeditems do something like this

              for (int i = 0; i <= chkListClienteEmail.CheckedItems.Count - 1; i++)
              {

              Object rawObj = chkListClienteEmail.CheckedItems[i];
              NameID typedObj = (NameID)rawObj;

              string s = typedObj.ID.ToString();

              more code...
              }

              L 1 Reply Last reply
              0
              • A Alan N

                There is some confusion here as selection and checking are quite separate things and a selected item need not be checked. There is a SelectedValue property but there is no CheckedValue property. I'll assume that the checkedlistbox item collection stores instances of a class such as

                class NameID {
                public String Name {get; set;};
                public Int32 ID {get; set;}
                }

                To iterate though the ID's of the checkeditems do something like this

                for (int i = 0; i <= chkListClienteEmail.CheckedItems.Count - 1; i++)
                {

                Object rawObj = chkListClienteEmail.CheckedItems[i];
                NameID typedObj = (NameID)rawObj;

                string s = typedObj.ID.ToString();

                more code...
                }

                L Offline
                L Offline
                Luis M Rojas
                wrote on last edited by
                #7

                That is correct, i am very sorry, because i did not explain me well. Thanks to all

                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