MEMO查找的问题。。。

hljiayou 2009-06-29 11:33:53

unit UntMemoFind;

interface

uses
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.
//运行之后发现只能找到文本中第一次出现的要查找的内容,再不能往下找了
...全文
81 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
hljiayou 2009-06-30
  • 打赏
  • 举报
回复
非常感谢楼上这位大侠!!!!只是有些地方还要我自己去研究,对我这个初学者来说是个挑战啊,不过还是要感谢你的!!!!
hljiayou 2009-06-29
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 xjwy 的回复:]
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.
[/Quote]

break的定义

break过程在delphi代码中起控制作用,如:退出一个for、while或者某些重复语句段,而continue在这些重复语句段中是执行下一个循环。

break的使用必须包含在一个for、while或者某些重复语句段,否则编译器报错。

以上翻译的对吗??这个常识应该知道,谢谢这位大侠~!
hljiayou 2009-06-29
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 happinessdepig 的回复:]
用变量记住起始位置,循环查找时接着查找。
[/Quote]


好像有些明白,但是这三行是什么意思呢??


Skipchars:=Skipchars+Length(Memo1.Lines[J]);
SkipChars:=SkipChars+(I*2); //发现不要这行也可执行
SkipChars:=SkipChars+PosReturn-1; //从这儿以上都不清楚
hljiayou 2009-06-29
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hongqi162 的回复:]
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 //从…
[/Quote]

去掉也只能找到一个而已,而且还不是从第一行开始找,他想找哪就找哪。。。
bdmh 2009-06-29
  • 打赏
  • 举报
回复
Break:将退出循环体,去掉
xjwy 2009-06-29
  • 打赏
  • 举报
回复
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.
happinessdepig 2009-06-29
  • 打赏
  • 举报
回复
用变量记住起始位置,循环查找时接着查找。

hongqi162 2009-06-29
  • 打赏
  • 举报
回复
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;
dinoalex 2009-06-29
  • 打赏
  • 举报
回复
[Code=Delphi(Pascal)]
unit Unit1;

interface

uses
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.
[/Code]

你的是按行来找的,在一行里,有相同的话,只select上第一个,比如找s,在第三行有..ss..这样的话,只select上第一个s

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧