老师让我们编个记事本,其中查找算法我写得是
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.
编译时就没反应,请高手解答。
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.
编译时就没反应,请高手解答。
解决方案 »
- delphi7中Ttable控件在哪里?我怎么找不到
- DELPHI被病毒感染!!!!!!!!!!!
- DELPHI Dll调用问题。Rundll32.exe *.dll,CameraSubArea
- 散分专贴四
- DCOM无法启动 的问题。
- =?GB2312?Q?=C4=E3=BA=C3?= 的解码方法.
- 一点个人意见:期待Delphi所应具有的以下几个基本特征(欢迎讨论,都有分啊)
- 在quickrep中, 如果一个记录能自动换行,怎么动态调整相应的QRShape的长度啊?
- Socketconnection连接Sql server的问题
- ODBC为什么总是调用失败??????是高手的请进。。。。。。
- 在psapi中的GetModuleFileNameEx函数在Win98下无法正常运行,而WinXP正常。
- 关于edit和dblookupcombobox控件的2个小问题
1. 语法错误 - Delphi编译器发现了语法错误,编译无法完成,无法生成可执行文件;
2. 警告 - 不存在语法错误,但是编译器认为不合理的地方(比如变量申明了而未使用,编译能正常完成生成可执行文件。难道楼主想让编译器编译时有反应?
ps:我的窗体格式与windows的记事本基本一样
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;
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控件本身就可以实现,去看看关于它的资料吧