此问题关键之处应为找行号这一步。
给你一段代码,其中行号,列号是常量,你可以自己变为其它形式。
procedure TForm1.Button2Click(Sender: TObject);
const
RowNum=2;
ColNum=3;
var fp,i,j:integer;
begin
fp:=0;
i:=0;
j:=1;
while j<rownumdo
begin
fp:=RichEdit1.FindText(#13#10,i,Length(richedit1.Text),[stMatchCase]);
if fp=-1 then
begin
ShowMessage('notfound');
exit;
end;
i:=fp+3;
inc(j);
end;
Form1.Caption:=IntToStr(fp);
RichEdit1.SelStart:=fp+2;
RichEdit1.SelLength:=ColNum;
with RichEdit1.SelAttributesdo
begin
Name:='宋体';
Size:=24;
Color:=clRed;
end;
RichEdit1.SetFocus;
end;
上午发的code因为一个小于号...重贴!
procedure TForm1.Button1Click(Sender: TObject);
const
RowNum=2;
ColNum=3;
var fp,i,j:integer;
begin
fp:=0;
i:=0;
j:=1;
while j<Rownumdo
begin
fp:=RichEdit1.FindText(#13#10,i,length(RichEdit1.Text),[]);
if fp=-1 then
ShowMessage('Row NOT Found!');
i:=fp+3;
inc(j);
end;
RichEdit1.SelStart:=fp+2;
RichEdit1.SelLength:=ColNum;
with RichEdit1.SelAttributesdo
begin
Color:=clRed;
Size:=24;
Name:='宋体';
end;
end;
写了一个函数帮你完成这个任务。
function FindAndSet(RE: TRichEdit;
Const C: String;
Inde: Integer): Boolean;
var
I, J, K: Integer;
begin
Result := False;
With REdo
begin
I := -1;
K := 1;
J := Length(Text);
while (I < J) and (K <= Inde)do
begin
I := FindText(C, I+1, (J-I), []);
If I = -1 then
Exit
else
Inc(K);
end;
If K <> Inde+1 then
Exit;
Result := True;
SelStart := I;
SelLength := Length(C);
with SelAttributesdo
begin
Color := clRed;
Size := 24;
Name := '宋体';
end;
end;
end;
使用时调用:
If FindAndSet(RichEdit1, 's', 5) then
ShowMessage('YES!')
else
ShowMessage('NO!');
其中5是出现的次数,
's'是找的串,不论位置,
RichEdit1就不用说了吧?:)
100分快拿来!