C# 获取 CheckBoxList 项目值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12429655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting CheckBoxList Item values
提问by Nic
I have a CheckBoxList which I'm populating with data. When I attempt to retrieve the checked items from the list I can only grab the item ordinal, I cannot get the value.
我有一个 CheckBoxList 正在填充数据。当我尝试从列表中检索选中的项目时,我只能获取项目序号,而无法获取该值。
I've read that you can use Items[i].Value however when I try to do this I get an error stating that there is no extension method 'value'.
我读过你可以使用 Items[i].Value 但是当我尝试这样做时,我收到一个错误,指出没有扩展方法“value”。
Here's the code I'm using to try and grab the information (note the GetItemText(i) actually only gives me the item position, not the text for the item)
这是我用来尝试获取信息的代码(注意 GetItemText(i) 实际上只给我项目位置,而不是项目的文本)
private void btnGO_Click(object sender, EventArgs e)
{
for (int i = 0; i < chBoxListTables.Items.Count; i++)
{
if (chBoxListTables.GetItemChecked(i))
{
string str = chBoxListTables.GetItemText(i);
MessageBox.Show(str);
//next line invalid extension method
chBoxListTables.Items[i].value;
}
}
}
This is using .Net 4.0
这是使用 .Net 4.0
Any thoughts would be appreciated...thanks
任何想法将不胜感激...谢谢
采纳答案by Nic
This ended up being quite simple. chBoxListTables.Item[i] is a string value, and an explicit convert allowed it to be loaded into a variable. The following code works:
这最终变得非常简单。chBoxListTables.Item[i] 是一个字符串值,显式转换允许将其加载到变量中。以下代码有效:
private void btnGO_Click(object sender, EventArgs e)
{
for (int i = 0; i < chBoxListTables.Items.Count; i++)
{
if (chBoxListTables.GetItemChecked(i))
{
string str = (string)chBoxListTables.Items[i];
MessageBox.Show(str);
}
}
}
回答by Nitesh
Try to use this.
尝试使用这个。
for (int i = 0; i < chBoxListTables.Items.Count; i++)
{
if (chBoxListTables.Items[i].Selected)
{
string str = chBoxListTables.Items[i].Text;
MessageBox.Show(str);
var itemValue = chBoxListTables.Items[i].Value;
}
}
The "V" should be in CAPS in Value.
“V”应该是大写的值。
Here is another code example used in WinForm app and runs properly.
这是 WinForm 应用程序中使用的另一个代码示例,并且运行正常。
var chBoxList= new CheckedListBox();
chBoxList.Items.Add(new ListItem("One", "1"));
chBoxList.Items.Add(new ListItem("Two", "2"));
chBoxList.SetItemChecked(1, true);
var checkedItems = chBoxList.CheckedItems;
var chkText = ((ListItem)checkedItems[0]).Text;
var chkValue = ((ListItem)checkedItems[0]).Value;
MessageBox.Show(chkText);
MessageBox.Show(chkValue);
回答by tango
Instead of this:
取而代之的是:
CheckboxList1.Items[i].value;
Try This:
尝试这个:
CheckboxList1.Items[i].ToString();
It worked for me :)
它对我有用:)
回答by Ahmed El-Hansy
Try to use this :
尝试使用这个:
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < chBoxListTables.Items.Count; i++)
if (chBoxListTables.GetItemCheckState(i) == CheckState.Checked)
{
txtBx.text += chBoxListTables.Items[i].ToString() + " \n";
}
}
回答by Rohan Khude
to get the items checked you can use CheckedItemsor GetItemsChecked. I tried below code in .NET 4.5
要检查项目,您可以使用CheckedItems或GetItemsChecked。我在 .NET 4.5 中尝试了以下代码
Iterate through the CheckedItemscollection. This will give you the item number in the list of checked items, not the overall list. So if the first item in the list is not checked and the second item is checked, the code below will display text like Checked Item 1 = MyListItem2.
遍历CheckedItems集合。这将为您提供选中项目列表中的项目编号,而不是整个列表。因此,如果未选中列表中的第一项而选中第二项,则下面的代码将显示类似Checked Item 1 = MyListItem2.
//Determine if there are any items checked.
if(chBoxListTables.CheckedItems.Count != 0)
{
//looped through all checked items and show results.
string s = "";
for (int x = 0; x < chBoxListTables.CheckedItems.Count; x++)
{
s = s + (x + 1).ToString() + " = " + chBoxListTables.CheckedItems[x].ToString()+ ", ";
}
MessageBox.Show(s);//show result
}
-OR-
-或者-
Step through the Items collection and call the GetItemCheckedmethod for each item. This will give you the item number in the overall list, so if the first item in the list is not checked and the second item is checked, it will display something like Item 2 = MyListItem2.
单步执行 Items 集合并GetItemChecked为每个项目调用该方法。这将为您提供整个列表中的项目编号,因此如果未选中列表中的第一项而选中第二项,它将显示类似Item 2 = MyListItem2.
int i;
string s;
s = "Checked items:\n" ;
for (i = 0; i < checkedListBox1.Items.Count; i++)
{
if (checkedListBox1.GetItemChecked(i))
{
s = s + "Item " + (i+1).ToString() + " = " + checkedListBox1.Items[i].ToString() + "\n";
}
}
MessageBox.Show (s);
Hope this helps...
希望这可以帮助...
回答by Santos
//Simple example code:
//简单示例代码:
foreach (var item in YourCheckedListBox.CheckedItems)
{List<string>.Add(item);}
回答by spacemonkey
You can try this:-
你可以试试这个:-
string values = "";
foreach(ListItem item in myCBL.Items){
if(item.Selected)
{
values += item.Value.ToString() + ",";
}
}
values = values.TrimEnd(','); //To eliminate comma in last.
回答by Faisal Ansari
You can initialize a list of string and add those items that are selected.
您可以初始化一个字符串列表并添加那些被选中的项目。
Please check code, works fine for me.
请检查代码,对我来说很好用。
List<string> modules = new List<string>();
foreach(ListItem s in chk_modules.Items)
{
if (s.Selected)
{
modules.Add(s.Value);
}
}

