Autor Beitrag
Kinimod8
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 24

Win XP Home
D3 Prof
BeitragVerfasst: Mo 28.07.03 08:27 
Hallo,

weiß jemand wie ich einen Text der in TRichEdit geschrieben ist (ohne Formatierungen) nach bestimmten Wörtern durchsuchen und die gefundenen dann durch andere ersetzen kann?
Tino
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Veteran
Beiträge: 9839
Erhaltene Danke: 45

Windows 8.1
Delphi XE4
BeitragVerfasst: Mo 28.07.03 08:53 
Hallo!

Schau dir mal die Suche in: Delphi-Forum, Delphi-Library FINDTEXT-Methode vom TRichEdit an. In der Delphi-Hilfe befindet sich auch ein Beispiel:

Delphi-Hilfe hat folgendes geschrieben:
Für dieses Beispiel werden drei Objekte benötigt: TRichEdit, TButton und TFindDialog.
Nach einem Klick auf die Schaltfläche wird rechts vom Eingabefeld ein Dialogfeld Suchen angezeigt. Wenn nun ein Suchtext eingegeben und die Suche gestartet wird, erhält der erste passende String nach der aktuellen Cursorposition im RTF-Eingabefeld eine Markierung.
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
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
    { Suchbeginn nach der aktuellen Markierung (falls vorhanden), }
    { andernfalls am Textanfang starten }

    if SelLength <> 0 then
      StartPos := SelStart + SelLength;
    else

      StartPos := 0;

    { ToEnd ist die Länge von StartPos bis zum Ende des Textes im RTF-Eingabefeld }

    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;

Gruß
Tino
Cyrus
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 56



BeitragVerfasst: Di 29.07.03 13:56 
eine weitere Möglichkeit

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
function Search_And_Replace(RichEdit: TRichEdit; 
  SearchText, ReplaceText: string): Boolean; 
var 
  startpos, Position, endpos: integer; 
begin 
  startpos := 0; 
  with RichEdit do 
  begin 
    endpos := Length(RichEdit.Text); 
    Lines.BeginUpdate; 
    while FindText(SearchText, startpos, endpos, [stMatchCase])<>-1 do 
    begin 
      endpos   := Length(RichEdit.Text) - startpos; 
      Position := FindText(SearchText, startpos, endpos, [stMatchCase]); 
      Inc(startpos, Length(SearchText)); 
      SetFocus; 
      SelStart  := Position; 
      SelLength := Length(SearchText); 
      richedit.clearselection; 
      SelText := ReplaceText; 
    end; 
    Lines.EndUpdate; 
  end; 
end; 

procedure TForm1.Button1Click(Sender: TObject); 
begin 
  Search_And_Replace(Richedit1, 'OldText', 'NewText'); 
end;


Greetz Cyrus

Moderiert von user profile iconUGrohne: Code- durch Delphi-Tags ersetzt

_________________
Wer glaub er ist, hört auf zu werden!
Delphi Rulez!!!