Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   Delphi Texte in DBGrid formatieren! (https://www.delphipraxis.net/67426-texte-dbgrid-formatieren.html)

Goldesel 14. Apr 2006 09:23


Texte in DBGrid formatieren!
 
Hallo,

ich habe die Eigenschaften einer DBGrid so verändert, dass sie im Prinzip aussieht wie eine Listbox. Das heißt unter "options" so einiges auf "false" gesetzt. Jetzt möchte ich gerne, dass ich in einer Zeile einige Textabschnitte formnatiert bekommen. Mir würde fett und kursiv vollkommen ausreichen. Also z.B.

Hallo wie geht's?

Danke
Jannik

marabu 14. Apr 2006 10:59

Re: Texte in DBGrid formatieren!
 
Hallo Jannik.

Der folgende Code zeigt dir im Prinzip, wie du in einer Zelle verschiedene Styles verwenden kannst. Da du keine Kriterien angegeben hast, habe ich im Beispiel einfach den Zellentext in Wörter zerlegt und schreibe dann Wörter fett, wenn sie mehr als fünf Zeichen lang sind.

Delphi-Quellcode:
function LeadingBlanks(s: String; index: Integer): Integer;
begin
  Result := 0;
  while (index + Result < Length(s)) and (s[index + Result] = ' ') do
    Inc(Result);
end;

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
  s, sPart: String;
  iLeft, iStart, iLength: Integer;
begin
  s := Column.Field.AsString;
  with (Sender as TDBGrid).Canvas do
  begin
    iLeft := Rect.Left + 2;
    iStart := 1;
    while iStart <= Length(s) do
    begin
      if s[iStart] = ' '
        then iLength := LeadingBlanks(s, iStart)
        else iLength := PosEx(' ', s, iStart) - iStart;
      if iLength < 0 then
        iLength := Succ(Length(s) - iStart);
      if iLength > 5
        then Font.Style := Font.Style + [fsBold]
        else Font.Style := Font.Style - [fsBold];
      sPart := Copy(s, iStart, iLength);
      TextOut(iLeft, Rect.Top + 2, sPart);
      iLeft := iLeft + TextWidth(sPart);
      iStart := iStart + iLength;
    end;
  end;
end;
Grüße vom marabu


Alle Zeitangaben in WEZ +1. Es ist jetzt 02:43 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz