老师让我们编个记事本,其中查找算法我写得是
procedure TForm1.N18Click(Sender: TObject); //查找
var
   p:integer;
begin
     if finddialog1.Execute then
     p:=pos(finddialog1.findtext,memo1.Text);        if p=0 then
          showmessage('找不到'+finddialog1.findtext)
        else
           begin
               memo1.setfocus;
               memo1.selstart:=p-1;
               memo1.sellength:=length(finddialog1.findtext);
           end;
end;end.
编译时就没反应,请高手解答。

解决方案 »

  1.   

    “编译时就没反应”——不知所云编译时要是Delphi有反应就表示:
    1. 语法错误 - Delphi编译器发现了语法错误,编译无法完成,无法生成可执行文件;
    2. 警告 - 不存在语法错误,但是编译器认为不合理的地方(比如变量申明了而未使用,编译能正常完成生成可执行文件。难道楼主想让编译器编译时有反应?
      

  2.   

    就是我运行后,我的记事本运行成功,但按编辑的查找键,没反应,不给弹查找对话框。
    ps:我的窗体格式与windows的记事本基本一样
      

  3.   

    Execute执行后马上就返回了。看下面的例子吧。This example requires a TRichEdit, a TButton, and a TFindDialog.
    Clicking the button click will display a Find Dialog to the right of the edit control.  Filling in the "Find what" text and pressing the Find Next button will select the first matching string in the Rich Edit control that follows the previous selection.procedure TForm1.Button1Click(Sender: TObject);begin
      FindDialog1.Position := Point(RichEdit1.Left + RichEdit1.Width, RichEdit1.Top);
      FindDialog1.Execute;
    end;procedure TForm1.FindDialog1Find(Sender: TObject);
    var
      FoundAt: LongInt;
      StartPos, ToEnd: Integer;
    begin
      with RichEdit1 do
      begin
        { begin the search after the current selection if there is one }
        { otherwise, begin at the start of the text }
        if SelLength <> 0 then      StartPos := SelStart + SelLength
        else      StartPos := 0;    { ToEnd is the length from StartPos to the end of the text in the rich edit control }    ToEnd := Length(Text) - StartPos;    FoundAt := FindText(FindDialog1.FindText, StartPos, ToEnd, [stMatchCase]);
        if FoundAt <> -1 then
        begin
          SetFocus;
          SelStart := FoundAt;
          SelLength := Length(FindDialog1.FindText);
        end;
      end;
    end;
      

  4.   

    几个小问题:
    1:pos是区分大小写的
    2:如果有多处子串的话,pos只能返回第一个子串所在的位置
    3:对memo控件来说,当失去焦点的时候,选中的内容是看不出来的
    4:你的代码,当别人取消查找的时候也会报出“找不到”的提示,但这时候是不需要的,这就属于bug了这几个问题解决起来也很简单
    1:可以用lowercase转成小写查找
    2:有个函数叫posex,可以结合pos使用,即第一次用pos,后面用posex继续查找
    3:在memo1.sellength:=length(finddialog1.findtext);后面加一句:memo1.SetFocus;
    4:把执行的代码都写到if finddialog1.Execute then里面去,用begin end括起来,而不要只运行 p:=pos(finddialog1.findtext,memo1.Text);这一句,这是初学者经常容易犯的通病以上只是针对你的问题提出的建议和解决方法,实际上做查找功能不是用这种方法实现的,FindDialog控件本身就可以实现,去看看关于它的资料吧
      

  5.   

    delphi编译时,如成功,就没有反应。不成功,才会有反应!
      

  6.   

    keyz(keyz) 给出的是RichEdit的查找方法,如果用memo则不可以。