[英]How can I get the string value of a checked item from a CheckedListBox?
[英]How to get value of checked item from CheckedListBox?
我在 C# 中的 WinForm 上使用了 CheckedListBox。 我已經限制了這個控件,如下所示 -
chlCompanies.DataSource = dsCompanies.Tables[0];
chlCompanies.DisplayMember = "CompanyName";
chlCompanies.ValueMember = "ID";
我可以獲得已檢查項目的索引,但是如何獲得已檢查項目的文本和值。 而是如何通過 CheckedItems 枚舉訪問文本和值?
感謝您分享您的時間。
將它轉換回它的原始類型,如果你綁定一個表,它將是一個 DataRowView,然后你可以從相應的列中獲取 Id 和 Text:
foreach(object itemChecked in checkedListBox1.CheckedItems)
{
DataRowView castedItem = itemChecked as DataRowView;
string comapnyName = castedItem["CompanyName"];
int? id = castedItem["ID"];
}
編輯:我意識到它綁定到數據表有點晚了。 在這種情況下,想法是相同的,如果您想使用該類,您可以將其轉換為DataRowView
然后使用其Row
屬性來獲取DataRow
。
foreach (var item in checkedListBox1.CheckedItems)
{
var row = (item as DataRowView).Row;
MessageBox.Show(row["ID"] + ": " + row["CompanyName"]);
}
您需要將項目轉換或解析為它們的強類型等效項,或者使用System.Data.DataSetExtensions
命名空間來使用下面演示的DataRowExtensions.Field
方法:
foreach (var item in checkedListBox1.CheckedItems)
{
var row = (item as DataRowView).Row;
int id = row.Field<int>("ID");
string name = row.Field<string>("CompanyName");
MessageBox.Show(id + ": " + name);
}
您需要強制轉換該項目以訪問您的類的屬性。
foreach (var item in checkedListBox1.CheckedItems)
{
var company = (Company)item;
MessageBox.Show(company.Id + ": " + company.CompanyName);
}
或者,您可以使用OfType
擴展方法來獲取強類型結果,而無需在循環內顯式轉換:
foreach (var item in checkedListBox1.CheckedItems.OfType<Company>())
{
MessageBox.Show(item.Id + ": " + item.CompanyName);
}
您可以遍歷CheckedItems
屬性:
foreach(object itemChecked in checkedListBox1.CheckedItems)
{
MyCompanyClass company = (MyCompanyClass)itemChecked;
MessageBox.Show("ID: \"" + company.ID.ToString());
}
http://msdn.microsoft.com/en-us/library/system.windows.forms.checkedlistbox.checkeditems.aspx
要獲取 CheckedListBox 中的所有選定項目,請嘗試以下操作:
在這種情況下,ths 值是一個字符串,但它與其他類型的對象一起運行:
for (int i = 0; i < myCheckedListBox.Items.Count; i++)
{
if (myCheckedListBox.GetItemChecked(i) == true)
{
MessageBox.Show("This is the value of ceckhed Item " + myCheckedListBox.Items[i].ToString());
}
}
foreach (int x in chklstTerms.CheckedIndices)
{
chklstTerms.SelectedIndex=x;
termids.Add(chklstTerms.SelectedValue.ToString());
}
我已經在這篇文章中發布了
GetItemValue
擴展方法Get the value for a listbox item by index 。 此擴展方法適用於所有ListControl
類,包括CheckedListBox
、ListBox
和ComboBox
。
現有的答案都不夠通用,但該問題有一個通用的解決方案。
在所有情況下,無論數據源的類型如何,都應根據ValueMember
計算項目的基礎Value
。
CheckedListBox
的數據源可能是一個DataTable
也可能是一個包含對象的List<T>
,如List<T>
,因此CheckedListBox
控件的項可能是DataRowView
、復雜對象、匿名類型、主要類型和其他類型。
GetItemValue 擴展方法
我們需要一個GetItemValue
,它的工作原理類似於GetItemText
,但返回一個對象,即項目的基礎值,而不管您添加為項目的對象類型如何。
我們可以創建GetItemValue
擴展方法來獲取類似於GetItemText
項目值:
using System;
using System.Windows.Forms;
using System.ComponentModel;
public static class ListControlExtensions
{
public static object GetItemValue(this ListControl list, object item)
{
if (item == null)
throw new ArgumentNullException("item");
if (string.IsNullOrEmpty(list.ValueMember))
return item;
var property = TypeDescriptor.GetProperties(item)[list.ValueMember];
if (property == null)
throw new ArgumentException(
string.Format("item doesn't contain '{0}' property or column.",
list.ValueMember));
return property.GetValue(item);
}
}
使用上述方法,您無需擔心ListBox
設置,它將返回項目的預期Value
。 它適用於List<T>
、 Array
、 ArrayList
、 DataTable
、匿名類型列表、主要類型列表以及您可以用作數據源的所有其他列表。 下面是一個使用示例:
//Gets underlying value at index 2 based on settings
this.checkedListBox.GetItemValue(this.checkedListBox.Items[2]);
由於我們創建了GetItemValue
方法作為擴展方法,因此當您要使用該方法時,不要忘記包含放置類的命名空間。
此方法也適用於ComboBox
和CheckedListBox
。
埃及開發博客:在 vb.net 中獲取 CheckedListBox 中選中項的值
使用數據綁定 CheckedListBox 后,您可以獲得選中項的值
For i As Integer = 0 To CheckedListBox1.CheckedItems.Count - 1
Dim XDRV As DataRowView = CType(CheckedListBox1.CheckedItems(i), DataRowView)
Dim XDR As DataRow = XDRV.Row
Dim XDisplayMember As String = XDR(CheckedListBox1.DisplayMember).ToString()
Dim XValueMember As String = XDR(CheckedListBox1.ValueMember).ToString()
MsgBox("DisplayMember : " & XDisplayMember & " - ValueMember : " & XValueMember )
Next
現在您可以使用循環中 2 個變量 XDisplayMember 和 XValueMember 中 CheckedListBox 中選中項的值或顯示
希望有用。
嘗試:
foreach (var item in chlCompanies.CheckedItems){
item.Value //ID
item.Text //CompanyName
}
你可以試試這個:
string s = "";
foreach(DataRowView drv in checkedListBox1.CheckedItems)
{
s += drv[0].ToString()+",";
}
s=s.TrimEnd(',');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.