How to get current Checked Item in a checkedlistbox

≡放荡痞女 提交于 2019-12-24 06:33:59

问题


I have a list box and i am trying to get currently checked item inside ItemCheck Handler , but i couldn't , ->I can get List of CheckedItems using property chckdLstBox_Metabolites.CheckedItems But how do i get the item that is checked just before????


回答1:


You can use the event's 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);
    } 



回答2:


The ItemCheckEventArgs argument in your handler will give you the index of the item that is going to have its status changed. 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


来源:https://stackoverflow.com/questions/2535613/how-to-get-current-checked-item-in-a-checkedlistbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!