如何实现ListBox中内容上下拖拽的效果
例如:
ListBox1中内容为
1-asdfgh
2-qwerty
3-zxcvbn
用鼠标拖动第三行然后放到第二行的上面拖拽完成后
ListBox1中内容为
1-asdfgh
3-zxcvbn
2-qwerty
[解决办法]
看看吧,
//---------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h "
//---------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm "
TForm1 *Form1;
String str;
int ii;
//---------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------
void __fastcall TForm1::ListBox1DragOver(TObject *Sender, TObject *Source,
int X, int Y, TDragState State, bool &Accept)
{
ii = ListBox1-> ItemIndex;
str = ListBox1-> Items-> Strings[ii];
}
//---------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
ListBox1-> Items-> Add( "a ") ;
ListBox1-> Items-> Add( "b ") ;
ListBox1-> Items-> Add( "c ") ;
ListBox1-> Items-> Add( "d ") ;
ListBox1-> Items-> Add( "e ") ;
ListBox1-> Items-> Add( "f ") ;
}
//---------------------------------------
void __fastcall TForm1::ListBox1EndDrag(TObject *Sender, TObject *Target,
int X, int Y)
{
/*
ListBox1-> Items-> Delete(ii);
int index = Y / ListBox1-> ItemHeight;
if(index > ListBox1-> Count)
{
ListBox1-> Items-> Add(str);
}
else
{
ListBox1-> Items-> Insert(index,str);
}
*/
}
//---------------------------------------
void __fastcall TForm1::ListBox1DragDrop(TObject *Sender, TObject *Source,
int X, int Y)//ListBox1中拖动
{
ListBox1-> Items-> Delete(ii);
int index = Y / ListBox1-> ItemHeight;
if(index > ListBox1-> Count)
{
ListBox1-> Items-> Add(str);
}
else
{
ListBox1-> Items-> Insert(index,str);
}
}
//---------------------------------------