Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to move items in ListView?

Status
Not open for further replies.

hstijnen

Programmer
Nov 13, 2002
172
0
0
NL
HI,

How to move items in ListView? E.g. I select an item and want to step it upward or downward.
 
I think that this code should solve your problem. I'm not sure is this what you want but you can always change that sample :)

int SelItem;
//---------------------------------------------------------------------------
void __fastcall TForm1::ListBox1MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
SelItem=ListBox1->ItemIndex;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::UpwardClick(TObject *Sender)
{
if(SelItem>0)
{
ListBox1->Items->Move(SelItem,SelItem-1);
ListBox1->ItemIndex=SelItem-1;
SelItem--;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DownwardClick(TObject *Sender)
{
if(SelItem<ListBox1->Items->Count-1)
{
ListBox1->Items->Move(SelItem,SelItem+1);
ListBox1->ItemIndex=SelItem+1;
SelItem++;
}
}
//---------------------------------------------------------------------------
 
Alas, that works for ListBox, but not for ListView. For the Items property in ListBox is a TStrings that has a method Move, but the Items property of ListView is a TListItems, that has no such method.
 
You could try the following code (I modified dominochmiel's code). I'm not working on a computer with C++Builder right now, so you might have to make some minor adjustments.


int SelItem;

void __fastcall TForm1::ListBox1MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
SelItem=ListBox1->ItemIndex;
}

void __fastcall TForm1::UpwardClick(TObject *Sender)
{
if(ListView->ItemIndex > 0)
{
AnsiString AuxValue = ListView->Items->Strings[ListView->Items->ItemIndex - 1]; // or Values instead of Strings
ListView->Items->Strings[ListView->Items->ItemIndex - 1] = ListView->Items->Strings[ListView->Items->ItemIndex];
ListView->Items->Strings[ListView->Items->ItemIndex] = AuxValue;
// or you could still use a SelItem variable, but I think that the system should select the item clicked with the mouse automaticaly
}
//---------------------------------------------------------------------------
void __fastcall TForm1::DownwardClick(TObject *Sender)
{
if(ListView->ItemIndex < ListView->Count - 1)
{
AnsiString AuxValue = ListView->Items->Strings[ListView->Items->ItemIndex + 1]; // or Values instead of Strings
ListView->Items->Strings[ListView->Items->ItemIndex + 1] = ListView->Items->Strings[ListView->Items->ItemIndex];
ListView->Items->Strings[ListView->Items->ItemIndex] = AuxValue;
}
//---------------------------------------------------------------------------
 
Thank you. It's indeed the manner I've used. I had to discriminate between item->Caption and item->Subitems. Also I had to provide for connected objects (item->Data). They caused undesired effects in the OnSelectItem trigger. So half a day programming for one method in a TList.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top