以下是我最近刚刚编的小程序(是照书抄的),但老run不了,出现错误:
编的程序是使用鼠标拖曳的形式将几个认证的课程从一个listbox移至另一个listbox.
请高人指点,在下乃初学者,遇到此难题老是心烦意乱,在此静侯.
unit drag;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, ExtCtrls;type
  TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    ListBox1: TListBox;
    Label3: TLabel;
    ListBox2: TListBox;
    Bevel1: TBevel;
    BitBtn1: TBitBtn;
    procedure listbox2dragover(sender,source:tobject;x,y:integer;state
    :tdragstate;var accept:boolean);
    procedure listbox2dragdrop(sender,source:tobject;x,y:integer);
    procedure bitbtnclick(sender:tobject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;
  index:integer;
implementation{$R *.dfm}
procedure listbox2dragover(sender,source:tobject;x,y:integer;state
    :tdragstate;var accept:boolean);
begin
accept:=(source is tlistbox);
end;
procedure tform1.listbox2dragdrop(sender,source:tobject;x,y:integer)
begin
  index:=tlistbox(source).ItemIndex;
  if index<>1 then begin
     tlistbox(sender).Items.add(tlistbox(source).Items[index]);
     tlistbox(source).Items.delete(index);
   end;
  end;
  procedure tform1.bitbtn1click(sender:tobject);
  begin
    showmessage(format('您已通过%d科认证课程',[listbox2.items.count]);
  end;
end.

解决方案 »

  1.   

    是不是这里
    if index<>1 then begin
    要改成 if Index <> -1 then begin
    ?
      

  2.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, Buttons, StdCtrls, ExtCtrls;type
      TForm1 = class(TForm)
        ListBox1: TListBox;
        ListBox2: TListBox;
        SpeedButton1: TSpeedButton;
        SpeedButton2: TSpeedButton;
        SpeedButton3: TSpeedButton;
        SpeedButton4: TSpeedButton;
        procedure FormCreate(Sender: TObject);
        procedure ListBox1DragOver(Sender, Source: TObject; X, Y: Integer;
          State: TDragState; var Accept: Boolean);
        procedure ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
        procedure ListBox2DragDrop(Sender, Source: TObject; X, Y: Integer);
        procedure ListBox2DragOver(Sender, Source: TObject; X, Y: Integer;
          State: TDragState; var Accept: Boolean);
        procedure ListBox1MouseDown(Sender: TObject; Button: TMouseButton;
          Shift: TShiftState; X, Y: Integer);
        procedure ListBox2MouseDown(Sender: TObject; Button: TMouseButton;
          Shift: TShiftState; X, Y: Integer);
      private
        DragItem:Integer;
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
    var
      I:integer;
    begin
      for I:=0 to 9 do
      ListBox1.Items.Add(Inttostr(I));
    end;procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    var
      IdX:Integer;
    begin
       if Source=ListBox2 then
         Accept:=True
       else if Source=ListBox1 then
       begin
          IdX:=ListBox1.ItemAtPos(Point(X,Y),True);
          if Idx<>DragItem then
            ACcept:=False;
       end;
    end;procedure TForm1.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);begin
       if Source=ListBox2 then
        begin
           with ListBox1 do
           begin
              ListBox1.Items.AddObject(ListBox2.Items[DragItem],ListBox2.Items.Objects[DragItem]);
              ListBox2.Items.Delete(DragItem);
           end;
        end;
    end;procedure TForm1.ListBox2DragDrop(Sender, Source: TObject; X, Y: Integer);
    begin
       if Source=ListBox1 then
       begin
          ListBox2.Items.
                     AddObject(ListBox1.Items[DragItem],ListBox1.Items.Objects[DragItem]);      ListBox1.Items.Delete(DragItem);
       end;
    end;procedure TForm1.ListBox2DragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    var
      IdX:integer;
    begin
       if Source=ListBox1 then
         Accept:=True
       else if Source=ListBox2 then
       begin
          IdX:=ListBox2.ItemAtPos(Point(X,Y),True);
          if Idx<>DragItem then
            ACcept:=False;
       end;
    end;procedure TForm1.ListBox1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    begin
       if Button=mbLeft then
        begin
         with ListBox1 do
         begin
            DragItem:=ItemAtPos(Point(X,Y),True);
            if DragItem>=0 then
              ListBox1.BeginDrag(False);
         end;
        end;
    {Call BeginDrag to start a drag operation. BeginDrag is called in application code
    only when the value of the control DragMode is dmManual. If DragMode is dmAutomatic,
    BeginDrag is called automatically. If the Immediate parameter is true, the mouse pointer
    changes to the value of the DragCursor property and dragging begins immediately.
    If Immediate is false, the mouse pointer doesn't change to the value of the DragCursor property
    and dragging doesn't begin until the user moves the mouse pointer the number of pixels specified
    by the Threshold parameter. If the caller passes a Threshold value less than 0 (such as the default
    value for this parameter), BeginDrag uses the DragThreshold property of the global Mouse variable.
    Setting Immediate to false allows the control to accept mouse clicks without beginning a
    drag-and-drop or drag-and-dock operation.
    }
    end;procedure TForm1.ListBox2MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    begin
        if Button=mbLeft then
         begin
          with ListBox1 do
          begin
             DragItem:=ItemAtPos(Point(X,Y),True);
             if DragItem>=0 then
               ListBox2.BeginDrag(False);
          end;
         end;
    end;end.