StrikeTheCore
Programmer
I have created a TListBox and set both MultiSelect and ExtendedSelect to true. What I want to do is allow for users to either click one item in the list and be able to drag it to another spot or select multiple items and drag that entire block to another position.
However, when MultiSelect is enabled the user can drag the mouse to select multiple items but as soon as the mouse button is released all are deselected and the first item clicked moves to where the button is released (which is the correct behavior when MultiSelect is not enabled).
My code is as follows (and each method is tied to the corresponding event in the TListBox).
void __fastcall TForm1::MsgTypesMouseUp(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
AnsiString str;
int i = 0;
if(dragItem == -1)
return;
if(Button == mbLeft)
{
if(dragItem >= MsgTypes->Items->Count)
dragItem = MsgTypes->Items->Count-1;
MsgTypes->Items->Move(startDragItem, dragItem);
for(int count=(startDragItem<dragItem?startDragItem:dragItem); count<=(startDragItem>dragItem?startDragItem:dragItem);count++)
{
i = 0;
str = MsgTypes->Items->Strings[count];
MsgTypes->Items->Strings[count] = AnsiString().sprintf("%03d",msgEnumValues[count])+str.Delete(1,3);
str.Delete(1,1);
for (i = 0; i < 256; i++)
if (mappings.AnsiCompare(str))
break;
mappings[count] = str;
mappings = NULL;
}
dragItem = -1;
startDragItem = -1;
}
}
void __fastcall TForm1::MsgTypesMouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
TPoint point;
point.x = X;
point.y = Y;
if(Button == mbLeft)
{
dragItem = MsgTypes->ItemAtPos(point, true);
if(dragItem == -1 || dragItem < 2)
{
dragItem = -1;
return;
}
startDragItem = dragItem;
}
}
void __fastcall TForm1::MsgTypesMouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
TPoint point;
if(dragItem == -1)
return;
point.x = X;
point.y = Y;
if(Shift.Contains(ssLeft)){
dragItem = MsgTypes->ItemAtPos(point, false);
if(dragItem > MsgTypes->Items->Count)
return;
if(dragItem < 2)
dragItem = 2;
}
}
The dragItem and startDragItem are both global ints. Also I want to prevent moving the first 2 items, and that is the reason for the addition if statements. Any help with why this is not working is greatly appreciated.
However, when MultiSelect is enabled the user can drag the mouse to select multiple items but as soon as the mouse button is released all are deselected and the first item clicked moves to where the button is released (which is the correct behavior when MultiSelect is not enabled).
My code is as follows (and each method is tied to the corresponding event in the TListBox).
void __fastcall TForm1::MsgTypesMouseUp(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
AnsiString str;
int i = 0;
if(dragItem == -1)
return;
if(Button == mbLeft)
{
if(dragItem >= MsgTypes->Items->Count)
dragItem = MsgTypes->Items->Count-1;
MsgTypes->Items->Move(startDragItem, dragItem);
for(int count=(startDragItem<dragItem?startDragItem:dragItem); count<=(startDragItem>dragItem?startDragItem:dragItem);count++)
{
i = 0;
str = MsgTypes->Items->Strings[count];
MsgTypes->Items->Strings[count] = AnsiString().sprintf("%03d",msgEnumValues[count])+str.Delete(1,3);
str.Delete(1,1);
for (i = 0; i < 256; i++)
if (mappings.AnsiCompare(str))
break;
mappings[count] = str;
mappings = NULL;
}
dragItem = -1;
startDragItem = -1;
}
}
void __fastcall TForm1::MsgTypesMouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
TPoint point;
point.x = X;
point.y = Y;
if(Button == mbLeft)
{
dragItem = MsgTypes->ItemAtPos(point, true);
if(dragItem == -1 || dragItem < 2)
{
dragItem = -1;
return;
}
startDragItem = dragItem;
}
}
void __fastcall TForm1::MsgTypesMouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
TPoint point;
if(dragItem == -1)
return;
point.x = X;
point.y = Y;
if(Shift.Contains(ssLeft)){
dragItem = MsgTypes->ItemAtPos(point, false);
if(dragItem > MsgTypes->Items->Count)
return;
if(dragItem < 2)
dragItem = 2;
}
}
The dragItem and startDragItem are both global ints. Also I want to prevent moving the first 2 items, and that is the reason for the addition if statements. Any help with why this is not working is greatly appreciated.