书上的一个例题函数是这样的
function TDragForm.AddNotDup (List: TCustomListBox; Text: string): Boolean;
begin
   // 判断项目是否存在
  Result := List.Items.IndexOf (Text) < 0;
  if Result then
    List.Items.Add (Text);
end;从函数中能看出来这个result应该是返回的值但是在我照着写的时候确报错
 function tdragform.addnotdup(list:TCustomListBox;text:string):Boolean;
 begin
   Result:=list.
 end;写到这里报错
[Pascal Error] Unit1.pas(1): Unable to invoke Code Completion due to errors in source code我想问书上的例题这个Result是从哪里出来的

解决方案 »

  1.   

    完整的程序如下
    unit DragF;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      Grids, StdCtrls, CheckLst, ExtCtrls;type
      TDragForm = class(TForm)
        ListBox1: TListBox;
        CheckListBox1: TCheckListBox;
        Label1: TLabel;
        Edit1: TEdit;
        Button1: TButton;
        procedure ListDragOver(Sender, Source: TObject; X, Y: Integer;
          State: TDragState; var Accept: Boolean);
        procedure ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
        procedure CheckListBox1DragDrop(Sender, Source: TObject; X,
          Y: Integer);
        procedure Edit1MouseDown(Sender: TObject; Button: TMouseButton;
          Shift: TShiftState; X, Y: Integer);
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        function AddNotDup (List: TCustomListBox; Text: string): Boolean;
      end;var
      DragForm: TDragForm;implementation{$R *.DFM}procedure TDragForm.ListDragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    begin
      Accept := True;
      if (Source = Edit1) and
        ((Sender as TCustomListBox).Items.IndexOf (Edit1.Text) >= 0) then
        Accept := False;
    end;procedure TDragForm.ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
    var
      I: Integer;
    begin
      if Source = Edit1 then
        // 复制Edit控件中的文本
        ListBox1.Items.Add (Edit1.Text)
      else if Source = CheckListBox1 then
      begin
        // 复制所有选中的项目
        // 删除选中的项目
        for I := CheckListBox1.Items.Count - 1 downto 0 do
          if CheckListBox1.Checked [I] then
          begin
            if AddNotDup (ListBox1, CheckListBox1.Items [I]) then
              CheckListBox1.Items.Delete (I);
          end;
      end;
    end;procedure TDragForm.CheckListBox1DragDrop(Sender, Source: TObject; X,
      Y: Integer);
    var
      nItem: Integer;
    begin
      if Source = Edit1 then
        // 复制Edit控件中的文本
        CheckListBox1.Items.Add (Edit1.Text)
      else if Source = ListBox1 then
      begin
        // 复制ListBox中被选中的项
        nItem := ListBox1.ItemIndex;
        if AddNotDup (CheckListBox1, ListBox1.Items [nItem]) then
          // 删除被选中的项目
          ListBox1.Items.Delete (nItem);
      end;
    end;procedure TDragForm.Edit1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    begin
      Edit1.BeginDrag (False, 10);
    end;function TDragForm.AddNotDup (List: TCustomListBox; Text: string): Boolean;
    begin
       // 判断项目是否存在
      Result := List.Items.IndexOf (Text) < 0;
      if Result then
        List.Items.Add (Text);
    end;procedure TDragForm.Button1Click(Sender: TObject);
    //添加项目
    begin
      if not(edit1.Text='') then
      ListBox1.items.Add(edit1.text);
    end;end.
      

  2.   

    但是在我照着写的时候确报错
     function tdragform.addnotdup(list:TCustomListBox;text:string):Boolean;
     begin
      Result:=list.   //这里没写完
     end;写到这里报错
    [Pascal Error] Unit1.pas(1): Unable to invoke Code Completion due to errors in source code
      

  3.   

    你的那句报错是又由于Result是Boolean,但是你却写了Result:=list.,要写完才可以。
    函数的返回值是由函数定义决定的:function tdragform.addnotdup(list:TCustomListBox;text:string):Boolean;
      

  4.   

    Result 是返回值 就是函数得出的结果
    Result的类型要和函数定义的类型一致
    function tdragform.addnotdup(list:TCustomListBox;text:string):Boolean;
    应该是Boolean类型
      

  5.   

    把 Result 当个特殊的变量用就好了唯一要记得的是 他是函数的 返回值 或 叫处理结果
    注意初始化
      

  6.   

    function tdragform.addnotdup(list:TCustomListBox;text:string):Boolean;
     begin
      Result:=list.//
     end;写到这里报错注意红色的地方了么?