Skip to main content
 首页 » 编程设计

c#之如何在选中的列表框中获取当前选中的项目

2024年02月24日22zengkefu

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

请您参考如下方法:

您可以使用事件的 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); 
    }