procedure TForm1.FormDestroy(Sender: TObject);
begin
ListeItems.Free;
end;
procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
var i : Integer;
begin
(Sender as TControl).begindrag(false,3);
ListeItems.Clear;
for i := 0 to ListBox1.Count - 1 do
if ListBox1.Selected[i] then ListeItems.Add(IntToStr(i));
end;
procedure TForm1.ListBox2DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
Accept := true;
end;
procedure TForm1.ListBox2DragDrop(Sender, Source: TObject; X, Y: Integer);
var i : Integer;
begin
for i := 0 to ListeItems.Count - 1 do
ListBox2.Items.Add(ListBox1.Items[StrToInt(ListeItems[i])]);
for i := ListeItems.Count - 1 downto 0 do
ListBox1.Items.Delete(StrToInt(ListeItems[i]));
end;
On peut faire un Drag'n Drop des éléments sélectionnés de ListBox1 vers ListBox2.
A toi d'arranger ça à ta sauce ;)
japee
Messages postés1727Date d'inscriptionvendredi 27 décembre 2002StatutModérateurDernière intervention 6 novembre 20218 29 déc. 2007 à 23:54
Salut à tous,
Il n'est pas obligatoire de passer par un TStrings.
Dans l'exemple qui suit, on peut indifféremment glisser-déposer depuis ListBox1 vers ListBox2 et vice-versa.
Pour celà, ListBox2 est reliée à ListBox1 par l'inspecteur d'objets pour les 2 events qui suivent :
procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
Accept := Source is TListBox;
end;
procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
i: Integer;
begin
// ordre inversé
{
for i := TListBox(Source).Items.Count - 1 downto 0 do
begin
if TListBox(Source).Selected[i] then
begin
TListBox(Sender).Items.Add(TListBox(Source).Items[i]);
TListBox(Source).Items.Delete(i);
end;
end;
}
// ou si on veut respecter l'ordre des items :
for i := 0 to TListBox(Source).Items.Count - 1 do
if TListBox(Source).Selected[i] then
TListBox(Sender).Items.Add(TListBox(Source).Items[i]);
for i := TListBox(Source).Items.Count - 1 downto 0 do
if TListBox(Source).Selected[i] then
TListBox(Source).Items.Delete(i);
end;
japee
Messages postés1727Date d'inscriptionvendredi 27 décembre 2002StatutModérateurDernière intervention 6 novembre 20218 30 déc. 2007 à 01:23
Ok...
procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
i: Integer;
CursorPos: TPoint;
Index: Integer;
begin
CursorPos := Point(X, Y); // position du curseur
Index := ListBox1.ItemAtPos(CursorPos, True);
for i := TListBox(Source).Items.Count - 1 downto 0 do
begin
if TListBox(Source).Selected[i] then
begin
TListBox(Sender).Items.Insert(Index, TListBox(Source).Items[i]);
TListBox(Source).Items.Delete(i);
end;
end;
end;
japee
Messages postés1727Date d'inscriptionvendredi 27 décembre 2002StatutModérateurDernière intervention 6 novembre 20218 30 déc. 2007 à 01:55
Petite correction, sinon ça plante quand la ListBox destinataire est vide :
procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
i: Integer;
CursorPos: TPoint;
Index: Integer;
begin
CursorPos := Point(X, Y);
Index := ListBox1.ItemAtPos(CursorPos, True);
for i := TListBox(Source).Items.Count - 1 downto 0 do
begin
if TListBox(Source).Selected[i] then
begin
if Index < TListBox(Sender).Items.Count then
TListBox(Sender).Items.Insert(Index, TListBox(Source).Items[i])
else
TListBox(Sender).Items.Add(TListBox(Source).Items[i]);
TListBox(Source).Items.Delete(i);
end;
end;
end;
japee
Messages postés1727Date d'inscriptionvendredi 27 décembre 2002StatutModérateurDernière intervention 6 novembre 20218 30 déc. 2007 à 15:11
J'ai fait une erreur grossière dans mon code précédent, il fallait faire :
procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
i: Integer;
CursorPos: TPoint;
Index: Integer;
begin
CursorPos := Point(X, Y);
Index := TListBox(Sender).ItemAtPos(CursorPos, True);
for i := TListBox(Source).Items.Count - 1 downto 0 do
begin
if TListBox(Source).Selected[i] then
begin
if Index < -1 then
TListBox(Sender).Items.Insert(Index, TListBox(Source).Items[i])
else
TListBox(Sender).Items.Add(TListBox(Source).Items[i]);
TListBox(Source).Items.Delete(i);
end;
end;
end;
Mais quoi qu'il en soit, j'ai continué à travailler ce code quand même un peu bancal.
Je passe finalement par une StringList pour garder l'ordre dans lequel ont été trouvés les items de la LisBox source... (+ 1 pour Cari )
Je vais poster la source que je suis en train de finaliser, elle pourra servir à d'autres.
cs_morinxav2
Messages postés67Date d'inscriptionmercredi 30 juin 2004StatutMembreDernière intervention 6 octobre 20114 30 déc. 2007 à 18:34
Bonsoir,
Je suis au moins heureux de 3 choses;
-Vous m'avez trouvez la solution, et que vous le croyez ou non j'avais dèja passer du temps dessus. J'avais pensé au items.move et j'avais pas vue le items.insert.
-D'avoir déclenché tant d'angouement sur la resolution de mon problème.
-D'avoir declenché l'envie a Japee de faire une demo, pour que tout le monde en profite.
Je remercie et m'excuse auprès de Japee et Caribensila de s'être couché, je pense plus tard que moi (2H du mat pour ma part), mais le lendemain j'ai mes trois gamains qui galopent (8 ans, 3ans, 3mois).
et encore merci à qui m'a permis de debuter la programmation en 2004.
Deux dernières petites questions pour la route;
- Qu'elle est l'avenir du pascal et de delphi?
- Qu'elle langage est le plus utilisé en ce moment?
- Que faut- il pour programmer pour Linux? (Je ne passerais pas sous Vista, Ras le bol de Bill Gates!!!!)
A+