Delphi Dünyası Facebook'ta

Kodbank İndir

! CODEBANK 2012 !

İNDİRMEK&DETAYLI BİLGİ ALMAK İÇİN BURAYI TIKLAYINIZ.

Gönderen Konu: ListBox'da Highlight (Renklendirme)  (Okunma sayısı 2211 defa)

0 Üye ve 1 Ziyaretçi konuyu incelemekte.

Çevrimdışı Subhan

  • Delphi XE2 Level 5
  • *****
  • İleti: 818
  • Rep: +1/-0
  • Cinsiyet: Bay
ListBox'da Highlight (Renklendirme)
« : 29 Ağustos 2008 10:38:56 »
Forma TListBox TEdit ekleyin kodu kendinize göre ayarlayın

[Edite yazıl kelimeyi,harfi(ne yazıcaksanız) eyer listbox'un itemin'de varsa onu renklendirir

Kod: [Seç]
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    Edit1: TEdit;
    procedure ListBox1DrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    procedure Edit1Change(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    HighlightText : string ;
    TextLength : integer ;
    Foreground : TColor ;
    Background : TColor ;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
var
   Location : integer ;
   s : string ;
   Text : string;
   ax : integer ;
   TextWidth : integer ;
   OldBrushColor : TColor ;
   OldFontColor : TColor ;
const
   HighlightColor = clRed ;
begin
     with (Control as TListBox) do begin
        Canvas.FillRect(Rect) ;
        Location := Pos(HighlightText, Items[Index]) ;
        if Location > 0 then begin
           TextWidth := Canvas.TextWidth(HighlightText) ;
           s := Items[Index] ;
           ax := 0 ;
           while Location > 0 do begin
              Text := Copy(s, 1, Location - 1) ;
              s := Copy(s, Location + TextLength, Length(s)) ;
              Canvas.TextOut(ax, Rect.Top, Text) ;
              Inc(ax, Canvas.TextWidth(Text)) ;
              OldBrushColor := Canvas.Brush.Color ;
              OldFontColor := Canvas.Font.Color ;
              Canvas.Brush.Color := Background ;
              Canvas.Font.Color := Foreground ;
              Canvas.TextOut(ax, Rect.Top, HighlightText) ;
              Canvas.Brush.Color := OldBrushColor ;
              Canvas.Font.Color := OldFontColor ;
              Inc(ax, TextWidth) ;
              Location := Pos(HighlightText, s) ;
           end ;
           Canvas.TextOut(ax, Rect.Top, s) ;
        end
         else
           Canvas.TextOut(Rect.Left, Rect.Top, Items[Index]) ;
     end ;

end;

procedure TForm1.Edit1Change(Sender: TObject);
begin
HighlightText := Edit1.Text ;
TextLength := Length(Edit1.Text) ;
ListBox1.Refresh ;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Edit1.Clear;
     ListBox1.Items.LoadFromFile('c:\a.txt');//you can change it.
     ListBox1.Style:=lbOwnerDrawFixed;
     HighlightText := Edit1.Text ;
     TextLength := 5 ;
     Background := clRed ;// item renki
     Foreground := clGreen ;// font renki

end;

end.
En Güzel Azeri Programlama Sitesi.

Proqramlasdirma.Com