• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 628
  • Last Modified:

mousedown cancels checkonclick

i've got a checkedlistbox with a mousedown event which starts a dragNdrop procedure. one can move entries up and down in the list. unfortunatly this event cancels the checkonclick property of the list. even if i write a mouseclick event for checking the boxes it doesn't work, the event just ins't triggered. what am i doing wrong?
private void lbx_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
    if (((CheckedListBox)sender).Items.Count == 0)
        return;
 
    int index = ((CheckedListBox)sender).IndexFromPoint(e.X, e.Y);
 
    //set to last entry
    if (index == -1)
        return;
 
    Sp sp = (Sp)((CheckedListBox)sender).Items[index];
    DragDropEffects dde1 = DoDragDrop(sp, DragDropEffects.Move);
}
 
private void lbx_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
    e.Effect = DragDropEffects.Move;            
}
 
private void lbx_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
    Sp sp = (Sp) e.Data.GetData(typeof (Sp));
    int indexPos = ((CheckedListBox) sender).IndexFromPoint(((CheckedListBox) sender).PointToClient(new Point(e.X, e.Y)));
 
    ((CheckedListBox) sender).Items.Remove(sp);
 
    if (indexPos > -1)
        ((CheckedListBox) sender).Items.Insert(indexPos, sp);
    else
        ((CheckedListBox) sender).Items.Add(sp);
}
 
private void lbx_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
{
    int index = ((CheckedListBox)sender).IndexFromPoint(e.X, e.Y);
    ((CheckedListBox)sender).SetItemChecked(index, !((CheckedListBox)sender).GetItemChecked(index));
}

Open in new window

0
1eEurope
Asked:
1eEurope
  • 2
  • 2
2 Solutions
 
käµfm³d 👽Commented:
it most likely has something to do with not being over the checkbox when the mouse button is released. can't you just manually check the box within your mousedown event?

checkbox.checked = true;
0
 
1eEuropeAuthor Commented:
i can't do it in the mousedown event because if you just want to move it, it shouldn't be ckecked automaticly. i can uncheck them without any problems regardles if i'm above the checkbox or not.
0
 
käµfm³d 👽Commented:
Are you checking for a threshold of movement in your mousedown event? That should give the functionality you seek. In the following, I made a panel that had thumbnail pictures on it. I wanted to be able to drag and drop them in order to rearrange them, but the mousedown was conflicting with some other condition (I can't recall what). I made two class level variables to store the X & Y positions of the mouse from the mousedown event. Then, in the mousemove event, i check if the current mouse position has moved more than five pixels, in either direction, from the position set in mousedown. If it has, then I do the drag-drop.

I believe this is similar to your situation. The checkbox will be checked as long as mousehasn't moved more than Z pixels (whatever you set Z to).
private void thumb_MouseDown(object sender, MouseEventArgs e)
{
    this.clickedIndex = ((Thumbnail)sender).Index;
 
    if (e.Button == MouseButtons.Left)
    {
        this.mouseX = e.X;
        this.mouseY = e.Y;
    }
}
 
private void thumb_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        if (Math.Abs(e.X - this.mouseX) > 5 || Math.Abs(e.Y - this.mouseY) > 5)
        {
            Thumbnail thumb = (Thumbnail)sender;
            thumb.DoDragDrop(thumb.Image, DragDropEffects.Move);
        }
    }
}

Open in new window

0
 
1eEuropeAuthor Commented:
i solved it similar to your sugestion. i saved the index of the checkbox in mousedown and in mousemove i start the drag event if a mousebutton is down.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now