Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# ListView with CheckBoxes, automatic checkbox checked when multi select rows

I'm using a ListView control with multirow and fullrow select on. When I'm selecting multiple rows at once, some of my rows magically become checked. This happens when dragging the mouse over and also when selecting one, and shift clicking another.

See image describing issue here: alt text

What in the grapefruit is going on? Anyone?

like image 642
Nick Avatar asked Jan 06 '10 23:01

Nick


1 Answers

Unfortunately there are bugs in the ListView class, this is one of them. The following code is a fix that worked for me.

Edit: Sorry, this doesn't work quite right, although it does prevent the error that you show in your question. This prevents selecting multiple items and then checking them by clicking the check box.

void SetupListView()
{
    listView.ItemCheck += new ItemCheckEventHandler(listView_ItemCheck);
    listView.MouseDown += new MouseEventHandler(listView_MouseDown);
    listView.MouseUp += new MouseEventHandler(listView_MouseUp);
    listView.MouseLeave += new EventHandler(listView_MouseLeave);
}

bool mouseDown = false;
void listView_MouseLeave(object sender, EventArgs e)
{
    mouseDown = false;
}

void listView_MouseUp(object sender, MouseEventArgs e)
{
    mouseDown = false;
}

void listView_MouseDown(object sender, MouseEventArgs e)
{
    mouseDown = true;
}

void listView_ItemCheck(object sender, ItemCheckEventArgs e)
{
    if(mouseDown)
    {
        e.NewValue = e.CurrentValue;
    }
}
like image 56
Matt Nelson Avatar answered Sep 24 '22 08:09

Matt Nelson