unit UntMemoFind;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;type
  TForm1 = class(TForm)
    Memo1: TMemo;
    FindDialog1: TFindDialog;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FindDialog1Find(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
  Finddialog1.Execute;
end;procedure TForm1.FindDialog1Find(Sender: TObject);
var
  I,J,PosReturn,SkipChars:Integer;//定义四个整型变量
begin
  for I:=0 to Memo1.Lines.Count do //I用来在MEMO文本中循环查找
  begin
    PosReturn:=Pos(FindDialog1.FindText,Memo1.Lines[I]); //查找的文本
    if PosReturn <>0 then //found!
    begin
      Skipchars:=0;  //  initialization Skipchars
      for J:=0 to I-1 do //从这儿以下都不清楚
        Skipchars:=Skipchars+Length(Memo1.Lines[J]);
      SkipChars:=SkipChars+(I*2);   //发现不要这行也可执行
      SkipChars:=SkipChars+PosReturn-1; //从这儿以上都不清楚
      Memo1.SetFocus;//设置光标
      Memo1.SelStart:=SkipChars; //选择起始位置
      Memo1.SelLength:=Length(FindDialog1.FindText);//选择长度
      Break; //干什么用的?
    end;
  end;
end;
end.
//运行之后发现只能找到文本中第一次出现的要查找的内容,再不能往下找了

解决方案 »

  1.   

    procedure TForm1.FindDialog1Find(Sender: TObject);
    var
      I,J,PosReturn,SkipChars:Integer;//定义四个整型变量
    begin
      for I:=0 to Memo1.Lines.Count do //I用来在MEMO文本中循环查找
      begin
        PosReturn:=Pos(FindDialog1.FindText,Memo1.Lines[I]); //查找的文本
        if PosReturn <>0 then //found!
        begin
          Skipchars:=0;  //  initialization Skipchars
          for J:=0 to I-1 do //从这儿以下都不清楚
            Skipchars:=Skipchars+Length(Memo1.Lines[J]);
          SkipChars:=SkipChars+(I*2);  //发现不要这行也可执行
          SkipChars:=SkipChars+PosReturn-1; //从这儿以上都不清楚
          Memo1.SetFocus;//设置光标
          Memo1.SelStart:=SkipChars; //选择起始位置
          Memo1.SelLength:=Length(FindDialog1.FindText);//选择长度
          Break; //干什么用的? 找到内容就退出了,不继续查找,要是去掉可以继续查找
        end;
      end;
    end; 
      

  2.   

    break
    The Break procedure causes the flow of control in Delphi code to exit a for, while, or repeat statement and continue at the next statement following the loop statement.A call to Break must be contained in a for, while, or repeat statement, or the compiler reports an error.
      

  3.   

    好像有些明白,但是这三行是什么意思呢??
     Skipchars:=Skipchars+Length(Memo1.Lines[J]); 
     SkipChars:=SkipChars+(I*2);  //发现不要这行也可执行 
     SkipChars:=SkipChars+PosReturn-1; //从这儿以上都不清楚 
      

  4.   


    break的定义break过程在delphi代码中起控制作用,如:退出一个for、while或者某些重复语句段,而continue在这些重复语句段中是执行下一个循环。break的使用必须包含在一个for、while或者某些重复语句段,否则编译器报错。以上翻译的对吗??这个常识应该知道,谢谢这位大侠~!
      

  5.   


    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        FindDialog1: TFindDialog;
        Button1: TButton;
        Memo1: TMemo;
        procedure Button1Click(Sender: TObject);
        procedure FindDialog1Find(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      f: integer;
      I,J,PosReturn,SkipChars:Integer;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    begin
      f:=0;
      Finddialog1.Execute;
    end;procedure TForm1.FindDialog1Find(Sender: TObject);begin
      for I:=0 to Memo1.Lines.Count -1 do
      begin
        PosReturn:=Pos(FindDialog1.FindText,Memo1.Lines[I]);
        if PosReturn <> 0 then
        begin
          Skipchars:=0;
          for J:=0 to I-1 do
            Skipchars:=Skipchars+Length(Memo1.Lines[J]);
          if skipchars >= f then
          begin
            SkipChars:=SkipChars+(I*2);
            SkipChars:=SkipChars+PosReturn-1;
            Memo1.SetFocus;
            Memo1.SelStart:=SkipChars;
            Memo1.SelLength:=Length(FindDialog1.FindText);
            f:= skipchars;
            Break;
          end;
        end;
      end;
    end;end.
    你的是按行来找的,在一行里,有相同的话,只select上第一个,比如找s,在第三行有..ss..这样的话,只select上第一个s