从 CheckedListBox 获取 CheckBox 的标签文本

发布于 2024-11-30 03:41:43 字数 549 浏览 1 评论 0原文

我目前有一个带有几个框的 CheckedListBox。我希望能够测试列表中的每个复选框以查看它是否已选中,如果是,则将其文本值 (CheckBox.Text) 添加到字符串列表中。

这是我所拥有的:

for ( int i = 0; i < multiTaskChecks.Items.Count; i++ )
{
    if ( multiTaskChecks.GetItemChecked(i) )
    {
        checkedMultiTasks.Add(multiTaskChecks.GetItemText(i));
    }
}

使用此方法,GetItemText 返回 0、1、2、3 等,而不是我想要的文本值。我也尝试过 CheckedListBox.Text.IndexOf(i)CheckedListBox.Text.ToList(),但都没有任何运气。

我只是无法从 CheckedListBox 中获取这些复选框之一的标签文本。对此的任何帮助将不胜感激。

I currently have a CheckedListBox with several boxes. I want to be able to test every Checkbox in the list to see if it's checked, and if it is, add it's text value (CheckBox.Text) to a List of strings.

Here is what I have:

for ( int i = 0; i < multiTaskChecks.Items.Count; i++ )
{
    if ( multiTaskChecks.GetItemChecked(i) )
    {
        checkedMultiTasks.Add(multiTaskChecks.GetItemText(i));
    }
}

Using this, GetItemText is returning 0, 1, 2, 3, etc instead of the text values that I'm after. I have also tried CheckedListBox.Text.IndexOf(i), CheckedListBox.Text.ToList(), each without any luck.

I just cannot get the label text of one of these CheckBoxes from the CheckedListBox. Any help with this would be really appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

简美 2024-12-07 03:41:43

首先,您应该能够像这样循环遍历已检查的项目

foreach (var item in multiTaskChecks.CheckedItems)
{
}

,然后根据项目的类型,从中获取您想要的任何属性。听起来它只是一个文本或者你只想要字符串,所以

foreach (var item in multiTaskChecks.CheckedItems)
{
    checkedMultiTasks.Add(item.ToString());
}

或者我更喜欢

checkedMultiTasks.AddRange(multiTaskChecks.CheckedItems.
    OfType<object>().Select(‌​i => i.ToString()));

Firstly, you should be able to loop through the checked items only like so

foreach (var item in multiTaskChecks.CheckedItems)
{
}

then depending on the type of the item, get whatever property you want from it. Sounds like it is just a Text or you just want the string, so

foreach (var item in multiTaskChecks.CheckedItems)
{
    checkedMultiTasks.Add(item.ToString());
}

or I prefer

checkedMultiTasks.AddRange(multiTaskChecks.CheckedItems.
    OfType<object>().Select(‌​i => i.ToString()));
别把无礼当个性 2024-12-07 03:41:43

试试这个:

for (int i = 0; i < multiTaskChecks.Items.Count; i++)
{
    if (multiTaskChecks.GetItemChecked(i))
    {
        checkedMultiTasks.Add(multiTaskChecks.GetItemText(multiTaskChecks.Items[i]));
    }
}

ListControl.GetItemText 方法

注意 此方法的 DisplayMember 有一个警告:

如果未指定 DisplayMember 属性,则 GetItemText 返回的值是项目的值ToString 方法。否则,该方法返回在 item 参数中指定的对象的 DisplayMember 属性中指定的成员的字符串值。

Try this:

for (int i = 0; i < multiTaskChecks.Items.Count; i++)
{
    if (multiTaskChecks.GetItemChecked(i))
    {
        checkedMultiTasks.Add(multiTaskChecks.GetItemText(multiTaskChecks.Items[i]));
    }
}

ListControl.GetItemText Method

NOTE There's a caution regarding DisplayMember for this method:

If the DisplayMember property is not specified, the value returned by GetItemText is the value of the item's ToString method. Otherwise, the method returns the string value of the member specified in the DisplayMember property for the object specified in the item parameter.

羁绊已千年 2024-12-07 03:41:43

这应该有效:

var checkedMultiTasks = new List<string>();
foreach(var item in multiTaskChecks.CheckedItems) {
    checkedMultiTasks.Add(item.ToString());
}

This should work:

var checkedMultiTasks = new List<string>();
foreach(var item in multiTaskChecks.CheckedItems) {
    checkedMultiTasks.Add(item.ToString());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文