Bohn
Bohn

Reputation: 26919

knowing if the left button of mouse is released on the same list box

I have two list boxes, one for Avaiable Items and one for Selected items, so the user can move items between these two by either drag-drop or double-click. It is not a perfect code and most of the logic is written in MouseMove event where I can have X and Y of the mouse location also...I am looking for this scenario to Prevent it: user holds left mouse button on the left list box and select an item BUT he releases the mouse button again on the same list box, so I need a way to know if it is still on the same list box then do not do the drag drop...so is there some method can tell me the boundaries of the list box that I can use? or any other better thoughts that you have?

private void lstAvailable_MouseMove(Object eventSender, MouseEventArgs eventArgs)
{
    //*************** FUNCTION DETAILS ***************
    //User moves mouse in the Available list
    //***************** INSTRUCTIONS *****************
    MouseButtons Button = eventArgs.Button;
    int Shift = (int)Control.ModifierKeys / 0x10000;
    float X = (float)VB6.PixelsToTwipsX(eventArgs.X);
    float Y = (float)VB6.PixelsToTwipsY(eventArgs.Y);
    moDualListBox.List1_MouseMove(Button, Shift, X, Y);


    if (eventArgs.Button == MouseButtons.Left )
    {
        if (!mbClickProcessed) // it is a DragDrop
        {
            this.lstAvailable.DoDragDrop(this.lstAvailable.SelectedItems, DragDropEffects.Move);
            mbClickProcessed = true;
        }
        if (mbClickProcessed) // it is a DoubleClick
        {
            MoveClick();
            MoveLostFocus();
            mbClickProcessed = true;
        }
    }
}

Upvotes: 0

Views: 317

Answers (2)

LarsTech
LarsTech

Reputation: 81655

Sample for drag-drop (no error checking):

private ListBox _DraggingListBox = null;

private void listBox1_DragDrop(object sender, DragEventArgs e)
{
  if (_DraggingListBox != listBox1)
    MoveItem(listBox2, listBox1, (int)e.Data.GetData(typeof(int)));
}

private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
  _DraggingListBox = listBox1;
  listBox1.DoDragDrop(listBox1.IndexFromPoint(e.X,e.Y), DragDropEffects.Move);    
}

private void listBox1_DragOver(object sender, DragEventArgs e)
{
  e.Effect = DragDropEffects.Move;
}

private void listBox2_DragDrop(object sender, DragEventArgs e)
{
  if (_DraggingListBox != listBox2)
    MoveItem(listBox1, listBox2, (int)e.Data.GetData(typeof(int)));
}

private void listBox2_DragOver(object sender, DragEventArgs e)
{
  e.Effect = DragDropEffects.Move;
}

private void listBox2_MouseDown(object sender, MouseEventArgs e)
{
  _DraggingListBox = listBox2;
  listBox2.DoDragDrop(listBox2.IndexFromPoint(e.X,e.Y), DragDropEffects.Move);
}

private void MoveItem(ListBox fromLB, ListBox toLB, int index)
{
  toLB.Items.Add(fromLB.Items[index]);
  fromLB.Items.RemoveAt(index);
}

Upvotes: 1

Marco
Marco

Reputation: 57593

If you catch DragDrop event you have th sender of drag-drop operation.
So you can easily discard the operation (or do nothing) if sender is same as destination control...

If you want to know if mouse is leaving a control (and set a variable according to this) you can catch MouseLeave event, while MouseEnter event is handy to know when mouse enters a control from another one.

Upvotes: 0

Related Questions