Thema: Delphi Suche im Richedit

Einzelnen Beitrag anzeigen

Benutzerbild von d3g
d3g

Registriert seit: 21. Jun 2002
602 Beiträge
 
#4
  Alt 6. Aug 2002, 18:14
Hi theomega,

das dürfte aber nach dem selben Prinzip funktionieren. Ich habe eine Funktionion geschrieben (TSearchTypes entspricht den TSearchTypes des RichEdit).

Code:
procedure BoldText(RichEdit: TRichEdit; Pattern: String; st: TSearchTypes);
type
  TStrArray = array of String;

  function ParseString(s: String; c: Char; Delimiters: Integer): TStrArray;
  var
    i, Count: Integer;
  begin
    // get count of delimiters and format array
    SetLength(Result, Delimiters + 1);
    // put string parts into array
    Count := 0;
    for i := 1 to Length(s) do
      if (s[i] = c) then begin
        Result[Count] := Copy(s, 1, i - 1);
        Delete(s, 1, i);
        Inc(Count);
      end;
    Result[Count] := s;
  end;

var
  StrArray: TStrArray;
  i, StartPos, EndPos, Delimiters: Integer;
begin
  Delimiters := 0;
  for i := 1 to Length(Pattern) do
    if (Pattern[i] = '*') then
      Inc(Delimiters);
  if (Delimiters < 1) then
    Exit;

  SetLength(StrArray, Delimiters + 1);
  StrArray := ParseString(Pattern , '*', Delimiters);
  EndPos := 0;

  while (True) do begin
    // find first part
    StartPos := RichEdit.FindText(StrArray[0], EndPos, Length(RichEdit.Text), st);
    if (StartPos = -1) then
      Exit;
    EndPos := StartPos;

    // found other parts
    for i := 1 to Delimiters do begin
      EndPos := RichEdit.FindText(StrArray[i], EndPos, Length(RichEdit.Text), st);
      if (EndPos = 1) then
        Exit;
    end;

    // set bold
    with RichEdit do begin
      SelStart := StartPos;
      SelLength := EndPos + Length(StrArray[Delimiters]) - StartPos;
      SelAttributes.Style := SelAttributes.Style + [fsBold];
      SelLength := 0;
    end;
  end;
end;
MfG,
d3g
-- Crucifixion?
-- Yes.
-- Good. Out of the door, line on the left, one cross each.
  Mit Zitat antworten Zitat