용일님
지금 계신가 모르겠어여..
잉~~
이거 어디가 틀렸나 봐 주세여..
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
ListBox1: TListBox;
ListBox2: TListBox;
procedure ListBox1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
procedure ListBox1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
procedure ListBox2DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
procedure ListBox2MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure ListBox2DragDrop(Sender, Source: TObject; X, Y: Integer);
private
DragItem : string;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
if Source = ListBox1 then
Accept := True
else
Accept := False;
end;
procedure TForm1.ListBox1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
ItemIndex : Integer;
begin
if Button = mbLeft then
begin
ItemIndex := ListBox1.ItemAtPos(Point(X, Y), True);
if ItemIndex >= 0 then
begin
ListBox1.BeginDrag(False);
DragItem := ListBox1.Items[ItemIndex];
end;
end;
end;
procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
Value : integer;
Index : Integer;
begin
Value := ListBox1.ItemAtPos(Point(x,y), True);
if Value = -1 then
begin
ListBox1.Items.Add(ListBox1.Items[ListBox1.ItemIndex]);
ListBox1.Items.Delete(ListBox1.ItemIndex);
end
else
begin
ListBox1.Items.Insert(Value {+1}, ListBox1.Items[ListBox1.ItemIndex]);
ListBox1.Items.Delete(ListBox1.ItemIndex);
end;
begin
Index := ListBox1.ItemAtPos(Point(X, Y), True);
if Index >= 0 then
ListBox1.Items.Insert(Index, DragItem)
else
ListBox1.Items.Add(DragItem);
ListBox2.Items.Delete(ListBox2.Items.IndexOf(DragItem));
end;
end;
procedure TForm1.ListBox2DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
if Source = ListBox1 then
Accept := True
else
Accept := False;
end;
procedure TForm1.ListBox2MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
ItemIndex1 : Integer;
begin
if Button = mbLeft then
begin
ItemIndex1 := ListBox2.ItemAtPos(Point(X, Y), True);
if ItemIndex1 >= 0 then
begin
ListBox2.BeginDrag(False);
DragItem := ListBox2.Items[ItemIndex1];
end;
end;
end;
procedure TForm1.ListBox2DragDrop(Sender, Source: TObject; X, Y: Integer);
var
Value1 : integer;
Index1 : Integer;
begin
Value1 := ListBox2.ItemAtPos(Point(x,y), True);
if Value1 = -1 then
begin
ListBox2.Items.Add(ListBox2.Items[ListBox2.ItemIndex]);
ListBox2.Items.Delete(ListBox2.ItemIndex);
end
else
begin
ListBox2.Items.Insert(Value1 {+1}, ListBox2.Items[ListBox2.ItemIndex]);
ListBox2.Items.Delete(ListBox2.ItemIndex);
end;
begin
Index1 := ListBox2.ItemAtPos(Point(X, Y), True);
if Index1 >= 0 then
ListBox2.Items.Insert(Index1, DragItem)
else
ListBox2.Items.Add(DragItem);
ListBox1.Items.Delete(ListBox1.Items.IndexOf(DragItem));
end;
end;
end.
잉~~~
저 기달릴께여..
답변좀 부탁드려여
아마두 ListBox1 <-> ListBox1, ListBox2 <-> ListBox2, ListBox1 <-> ListBox2
서로간에 Drag&Drop를 구현하실려고 하시는것 같은데 is, as연산자를 쓰시면 하나의
이벤트만 가지고도 구현하실수 있습니다.
Sender as TListBox는 Sender를 TListBox로 형변환 합니다. 즉 TListBox(Sender)와
같은 뜻이고, Sender is TListBox는 Sender가 TListBox형이면 참을 그렇지 않으면
거짓을 가집니다. ListBox1과 ListBox2에 같은 이벤트를 링크하세요...
// ListBox1, ListBox2의 OnMouseDown 이벤트
procedure TForm1.ListBoxMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
ItemIndex: Integer;
begin
if Button = mbLeft then
begin
ItemIndex := (Sender as TListBox).ItemAtPos(Point(X, Y), True);
if ItemIndex >= 0 then
begin
(Sender as TListBox).BeginDrag(False);
DragItem := (Sender as TListBox).Items[ItemIndex];
end;
end;
end;
// ListBox1, ListBox2의 OnDragOver
procedure TForm1.ListBoxDragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
if Source is TListBox then
Accept := True
else
Accept := False;
end;
// ListBox1, ListBox2의 OnDragDrop
procedure TForm1.ListBoxDragDrop(Sender, Source: TObject; X, Y: Integer);
var
Index: Integer;
begin
Index := (Sender as TListBox).ItemAtPos(Point(X, Y), True);
(Source as TListBox).Items.Delete((Source as TListBox).Items.IndexOf(DragItem));
if Index >= 0 then
(Sender as TListBox).Items.Insert(Index, DragItem)
else
(Sender as TListBox).Items.Add(DragItem);
end;
^^ 항상 즐코하세요...
이정민 wrote:
> 용일님
>
> 지금 계신가 모르겠어여..
>
> 잉~~
>
> 이거 어디가 틀렸나 봐 주세여..
>
> unit Unit1;
>
> interface
>
> uses
> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
> StdCtrls;
>
> type
> TForm1 = class(TForm)
> ListBox1: TListBox;
> ListBox2: TListBox;
> procedure ListBox1DragOver(Sender, Source: TObject; X, Y: Integer;
> State: TDragState; var Accept: Boolean);
> procedure ListBox1MouseDown(Sender: TObject; Button: TMouseButton;
> Shift: TShiftState; X, Y: Integer);
> procedure ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
> procedure ListBox2DragOver(Sender, Source: TObject; X, Y: Integer;
> State: TDragState; var Accept: Boolean);
> procedure ListBox2MouseDown(Sender: TObject; Button: TMouseButton;
> Shift: TShiftState; X, Y: Integer);
> procedure ListBox2DragDrop(Sender, Source: TObject; X, Y: Integer);
> private
> DragItem : string;
> { Private declarations }
> public
> { Public declarations }
> end;
>
> var
> Form1: TForm1;
>
> implementation
>
> {$R *.DFM}
>
> procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X, Y: Integer;
> State: TDragState; var Accept: Boolean);
> begin
> if Source = ListBox1 then
> Accept := True
> else
> Accept := False;
> end;
>
> procedure TForm1.ListBox1MouseDown(Sender: TObject; Button: TMouseButton;
> Shift: TShiftState; X, Y: Integer);
> var
> ItemIndex : Integer;
> begin
> if Button = mbLeft then
> begin
> ItemIndex := ListBox1.ItemAtPos(Point(X, Y), True);
> if ItemIndex >= 0 then
> begin
> ListBox1.BeginDrag(False);
> DragItem := ListBox1.Items[ItemIndex];
> end;
> end;
> end;
>
> procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
> var
> Value : integer;
> Index : Integer;
> begin
> Value := ListBox1.ItemAtPos(Point(x,y), True);
> if Value = -1 then
> begin
> ListBox1.Items.Add(ListBox1.Items[ListBox1.ItemIndex]);
> ListBox1.Items.Delete(ListBox1.ItemIndex);
> end
> else
> begin
> ListBox1.Items.Insert(Value {+1}, ListBox1.Items[ListBox1.ItemIndex]);
> ListBox1.Items.Delete(ListBox1.ItemIndex);
> end;
> begin
> Index := ListBox1.ItemAtPos(Point(X, Y), True);
> if Index >= 0 then
> ListBox1.Items.Insert(Index, DragItem)
> else
> ListBox1.Items.Add(DragItem);
> ListBox2.Items.Delete(ListBox2.Items.IndexOf(DragItem));
> end;
> end;
>
> procedure TForm1.ListBox2DragOver(Sender, Source: TObject; X, Y: Integer;
> State: TDragState; var Accept: Boolean);
> begin
> if Source = ListBox1 then
> Accept := True
> else
> Accept := False;
> end;
>
> procedure TForm1.ListBox2MouseDown(Sender: TObject; Button: TMouseButton;
> Shift: TShiftState; X, Y: Integer);
> var
> ItemIndex1 : Integer;
> begin
> if Button = mbLeft then
> begin
> ItemIndex1 := ListBox2.ItemAtPos(Point(X, Y), True);
> if ItemIndex1 >= 0 then
> begin
> ListBox2.BeginDrag(False);
> DragItem := ListBox2.Items[ItemIndex1];
> end;
> end;
> end;
>
> procedure TForm1.ListBox2DragDrop(Sender, Source: TObject; X, Y: Integer);
> var
> Value1 : integer;
> Index1 : Integer;
> begin
> Value1 := ListBox2.ItemAtPos(Point(x,y), True);
> if Value1 = -1 then
> begin
> ListBox2.Items.Add(ListBox2.Items[ListBox2.ItemIndex]);
> ListBox2.Items.Delete(ListBox2.ItemIndex);
> end
> else
> begin
> ListBox2.Items.Insert(Value1 {+1}, ListBox2.Items[ListBox2.ItemIndex]);
> ListBox2.Items.Delete(ListBox2.ItemIndex);
> end;
> begin
> Index1 := ListBox2.ItemAtPos(Point(X, Y), True);
> if Index1 >= 0 then
> ListBox2.Items.Insert(Index1, DragItem)
> else
> ListBox2.Items.Add(DragItem);
> ListBox1.Items.Delete(ListBox1.Items.IndexOf(DragItem));
> end;
> end;
>
> end.
>
> 잉~~~
>
> 저 기달릴께여..
>
> 답변좀 부탁드려여
>
>