No overload for '...' matches delegate 'System.Windows.Forms.ItemCheckEventHandler'

After reading this question, it became obvious to me that I was writing my event incorrectly. However, I have no idea how I can rewrite what I wrote using the Sender Object . My event adds text from the selected checkboxes to the 2D list (report) and the order in the report should be the same as the order of the selected checkboxes. In addition, you can select a maximum of two check boxes at a time. Here is the event:

void checkedListBox_ItemCheck(CheckedListBox chkdlstbx, ItemCheckEventArgs e)
    {
        int index = Convert.ToInt32(chkdlstbx.Tag);
        if ((chkdlstbx.CheckedItems.Count == 0) && (e.CurrentValue == CheckState.Unchecked))
        {
            Var.report[index].Add(chkdlstbx.Text);
        }
        if ((chkdlstbx.CheckedItems.Count == 1) && (e.CurrentValue == CheckState.Checked))
        {
            Var.report[index].RemoveAt(0);
        }
        if ((chkdlstbx.CheckedItems.Count == 1) && (e.CurrentValue == CheckState.Unchecked))
        {
            if (chkdlstbx.SelectedIndex < chkdlstbx.CheckedIndices[0])
            {
                Var.report[index].Insert(0, chkdlstbx.Text);
            }
            else
            {
                Var.report[index].Add(chkdlstbx.Text);
            }
        }
        if ((chkdlstbx.CheckedItems.Count == 2) && (e.CurrentValue == CheckState.Checked))
        {
            if (chkdlstbx.SelectedIndex == chkdlstbx.CheckedIndices[0])
            {
                Var.report[index].RemoveAt(0);
            }
            else
            {
                Var.report[index].RemoveAt(1);
            }
        }
        if ((chkdlstbx.CheckedItems.Count == 2) && (e.CurrentValue == CheckState.Unchecked))
        {
            e.NewValue = CheckState.Unchecked;
        }
        updateReport();
    }

      

It is called by this line:

chkdlstbx.ItemCheck += new ItemCheckEventHandler(checkedListBox_ItemCheck);

      

If someone can help me rewrite my event with an object, that would be awesome. I'm not really sure how else I would go about solving this problem!

+3


source to share


1 answer


This should be enough:



void checkedListBox_ItemCheck(object sender, ItemCheckEventArgs e)
{
    CheckedListBox chkdlstbx = sender as CheckedListBox;
    if (chkdlstbx == null)
    {
        throw new InvalidArgumentException();
    }
    ....
}

      

+1


source







All Articles