简体   繁体   English

如何在检查列表框中获取当前的检查项目

[英]How to get current Checked Item in a checkedlistbox

我有一个列表框,我试图在ItemCheck Handler中获取当前选中的项目,但我无法->我可以使用属性chckdLstBox_Metabolites.CheckedItems获取CheckedItems列表,但是我如何获取刚刚检查过的项目? ???

You can use the event's ItemCheckEventArgs: 您可以使用事件的ItemCheckEventArgs:

private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
    {
        //Note: MessageBox is for demo use only 
        MessageBox.Show("Selected Index: " + e.Index.ToString());
        MessageBox.Show("Current Value: " + e.CurrentValue.ToString());
        MessageBox.Show("New Value: " + e.NewValue.ToString());
        //Getting the item would be:
        string currentItem = (string)this.checkedListBox1.Items[e.Index];
        MessageBox.Show("Current Item: " + currentItem);
    } 

The ItemCheckEventArgs argument in your handler will give you the index of the item that is going to have its status changed. 处理程序中的ItemCheckEventArgs参数将为您提供将要更改其状态的项目的索引。 It has properties for the current value as well as a property to get or set the new value. 它具有当前值的属性以及获取或设置新值的属性。

To get the item itself, you can use a line of code like below. 要获取商品本身,可以使用如下代码行。

object o = checkedListBox1.Items[e.Index]; // e is ItemCheckEventArgs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM