如何使用 C# 获取选中列表框中新选中项目的文本

发布于 2024-10-02 23:28:46 字数 65 浏览 6 评论 0原文

我正在使用 ItemCheckEventArgs,从中可以获取索引值,但从该值我不确定如何查找已检查内容的文本内容。

I'm using the ItemCheckEventArgs and from which I can get an index value, but from this value I'm not sure how to look up what the text is of whatever was checked.

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

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

发布评论

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

评论(4

任性一次 2024-10-09 23:28:46

在 ItemCheck 事件处理程序中使用 ItemCheckEventArgs e 您可以检索相应的对象

checkedListBox1.Items[e.Index]

In ItemCheck event handler using ItemCheckEventArgs e you can retrive corresponding object

checkedListBox1.Items[e.Index]
烟酒忠诚 2024-10-09 23:28:46

这里有一些简单的代码可以解决这个问题:

public void CheckedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
    var checkedListBox = (CheckedListBox)sender;
    var checkedItemText = checkedListBox.Items[e.Index].ToString();
}

Here's some bare-bones code that should do the trick:

public void CheckedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
    var checkedListBox = (CheckedListBox)sender;
    var checkedItemText = checkedListBox.Items[e.Index].ToString();
}
云淡风轻 2024-10-09 23:28:46

CheckedListBox类有一个 CheckedItems 属性。

private void WhatIsChecked_Click(object sender, System.EventArgs e) {
    // Display in a message box all the items that are checked.

   // First show the index and check state of all selected items.
   foreach(int indexChecked in checkedListBox1.CheckedIndices) {
       // The indexChecked variable contains the index of the item.
       MessageBox.Show("Index#: " + indexChecked.ToString() + ", is checked. Checked state is:" + checkedListBox1.GetItemCheckState(indexChecked).ToString() + ".");
   }

    // Next show the object title and check state for each item selected.
    foreach(object itemChecked in checkedListBox1.CheckedItems) {

        // Use the IndexOf method to get the index of an item.
        MessageBox.Show("Item with title: \"" + itemChecked.ToString() + 
            "\", is checked. Checked state is: " + checkedListBox1.GetItemCheckState(checkedListBox1.Items.IndexOf(itemChecked)).ToString() + ".");
    }

}

The CheckedListBox class has a CheckedItems property.

private void WhatIsChecked_Click(object sender, System.EventArgs e) {
    // Display in a message box all the items that are checked.

   // First show the index and check state of all selected items.
   foreach(int indexChecked in checkedListBox1.CheckedIndices) {
       // The indexChecked variable contains the index of the item.
       MessageBox.Show("Index#: " + indexChecked.ToString() + ", is checked. Checked state is:" + checkedListBox1.GetItemCheckState(indexChecked).ToString() + ".");
   }

    // Next show the object title and check state for each item selected.
    foreach(object itemChecked in checkedListBox1.CheckedItems) {

        // Use the IndexOf method to get the index of an item.
        MessageBox.Show("Item with title: \"" + itemChecked.ToString() + 
            "\", is checked. Checked state is: " + checkedListBox1.GetItemCheckState(checkedListBox1.Items.IndexOf(itemChecked)).ToString() + ".");
    }

}
木森分化 2024-10-09 23:28:46

SelectedIndexChanged 事件中,输入以下代码

string text = (sender as CheckedListBox).SelectedItem.ToString();

Inside the SelectedIndexChanged event, put the following code

string text = (sender as CheckedListBox).SelectedItem.ToString();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文