Get the value from a checklistbox
-
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... } }
-
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... } }
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
-
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
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.
-
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.
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
-
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.
-
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... } }
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...
} -
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...
}That is correct, i am very sorry, because i did not explain me well. Thanks to all