線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:2605
推到 Plurk!
推到 Facebook!

ListBox的問題?

尚未結案
chrislao
初階會員


發表:86
回覆:69
積分:36
註冊:2002-12-28

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-04-29 22:55:51 IP:202.175.xxx.xxx 未訂閱
ListBox怎樣改變某一個item的字體顏色?
Chance36
版主


發表:31
回覆:1033
積分:792
註冊:2002-12-31

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-04-29 23:20:30 IP:211.20.xxx.xxx 未訂閱
引言: ListBox怎樣改變某一個item的字體顏色?
chrislao 你好
 
  可以在ListBox.OnDrawItem事件中處理    procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
begin
  With TListBox(control).Canvas Do Begin
    Case State Of
      odSelected : Begin // 選擇到的項目
        Canvas.Brush.Color := clRed; // 指定背景顏色
        Canvas.Font.Color := clBlue; // 指定字體顏色
        TextOut(Rect.Left, Rect.Top, (Control as TListBox).Items[Index]);
      End;
      odGrayed:....;  // 各種狀態 可各別改變顏色 
      odDisabled:....;
      odChecked:....;
      odFocused:....;
      .....
      Else Begin // 其他
        TextOut(Rect.Left, Rect.Top, (Control as TListBox).Items[Index]);
      End;
    End;
  End;
end;
yachanga
資深會員


發表:24
回覆:335
積分:296
註冊:2003-09-27

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-04-29 23:38:48 IP:61.230.xxx.xxx 未訂閱
Hi chrisloa: 可參考hager 大大及包大大文章
一個方式, 先將 ListBox1.Style := lbOwnerDrawFixed;
然後:    procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
var c: TCanvas;
begin
  c := ListBox1.Canvas;
  if not (odSelected in State) then begin
    c.Brush.Color := clWhite;
    c.Font.Color := clBlack;
  end else begin
    c.Brush.Color := clBlack;
    c.Font.Color := clRed;
  end;
  c.FillRect(Rect);
  c.TextOut(Rect.Left, Rect.Top, ListBox1.Items[Index]);
end;    ---
http://delphi.ktop.com.tw/topic.php?topic_id=41586 http://delphi.ktop.com.tw/topic.php?topic_id=45838 ~悠遊法國號~
chrislao
初階會員


發表:86
回覆:69
積分:36
註冊:2002-12-28

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-04-30 00:22:21 IP:202.175.xxx.xxx 未訂閱
我的意思是想把adoqAreaStaff 內的人名與listbox內的人名對比,如果lisrbox內的人名存在於adoqAreaStaff 內就改變該人名的頻色,下面的代碼只久改變顏色的部份,請問怎樣可以改變頻色? for i:=0 to adoqAreaStaff.RecordCount-1 do begin for j:=0 to litboxRoster.Items.Count-1 do begin if litboxRoster.Items.Strings[j] = trimleft(trimright(adoqAreaStaff['RTT_PName'])) then items變顏色 end; adoqAreaStaff.Next; end;
Chance36
版主


發表:31
回覆:1033
積分:792
註冊:2002-12-31

發送簡訊給我
#5 引用回覆 回覆 發表時間:2004-04-30 00:53:25 IP:203.204.xxx.xxx 未訂閱
chrislao 你好
  可以利用ListBox.Itesm.Objects來儲存相關顏色的記錄    1. 記錄在每個Item的Objects屬性 1:有對應  2:沒對應到    Var
  i : Integer ;
Begin
  adoqAreaStaff.First;
  While Not adoqAreaStaff.Eof do begin
    i := ListBox1.Items.IndexOf(trim(adoqAreaStaff['RTT_PName'].AsString));
    If i <> -1 Then Begin
      ListBox1.Items.Objects[i] := Pointer(1)  //表示有對應到
    End Else Begin
      ListBox1.Items.Objects[i] := Pointer(2)  //表示沒有對應到
    End;
    adoqAreaStaff.Next;
  end;
End;    2.一樣ListBox.OnDrawItem事件中
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
begin
  With TListBox(Control).Canvas Do Begin
    Case Integer(TListBox(Control).Items.Objects[Index]) Of
      1:Begin
        Font.Color := clRed ;  // 如果為 1 則改為紅色
        Brush.Coloe := $00E4FFCA  ; // 還可以改變底色
    End;
    TextOut(Rect.Left,Rect.Top,TListBox(Control).Items[Index]);
  End;
end;
發表人 - chance36 於 2004/04/30 00:58:53
yachanga
資深會員


發表:24
回覆:335
積分:296
註冊:2003-09-27

發送簡訊給我
#6 引用回覆 回覆 發表時間:2004-04-30 00:59:06 IP:61.230.xxx.xxx 未訂閱
Hi chrislao: 我提供另一種思考,我之前的經驗 1. 把資料庫人名先存在TStringList. 2. 比對ListBox1 與 TStringList 希望對您有幫助 [code] procedure TForm1.FormCreate(Sender: TObject); begin  NameList:=TstringList.Create;  //塞資料  while not adoquery1.eof do    begin    NameList.Add(FieldbyName('Name').asString);    next;   end; end;    procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;   Rect: TRect; State: TOwnerDrawState); var c: TCanvas; begin   c := ListBox1.Canvas;   if not findItem(ListBox1.Items[Index]) then begin c.Brush.Color := clWhite; c.Font.Color := clBlack; end else begin c.Brush.Color := clBlack; c.Font.Color := clRed; end; c.FillRect(Rect); c.TextOut(Rect.Left, Rect.Top, ListBox1.Items[Index]); end; //搜尋人名 function Tform1.findItem(Str:String): boolean; Var i: Integer; begin result:=false; For i:=0 to NameList.Count-1 do begin if NameList.Find(str,i) then begin result:=true; break; end; end; end; ~悠遊法國號~
jest0024
高階會員


發表:11
回覆:310
積分:224
註冊:2002-11-24

發送簡訊給我
#7 引用回覆 回覆 發表時間:2004-04-30 01:30:48 IP:203.67.xxx.xxx 未訂閱
引言: Hi chrislao: 我提供另一種思考,我之前的經驗 1. 把資料庫人名先存在TStringList. 2. 比對ListBox1 與 TStringList 希望對您有幫助
procedure TForm1.FormCreate(Sender: TObject);
begin
 NameList:=TstringList.Create;
 //塞資料
 while not adoquery1.eof do 
  begin
   NameList.Add(FieldbyName('Name').asString);
   next;
  end;
end;    procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
var c: TCanvas;
begin
  c := ListBox1.Canvas;
  if not findItem(ListBox1.Items[Index]) then begin
    c.Brush.Color := clWhite;
    c.Font.Color := clBlack;
  end else begin
    c.Brush.Color := clBlack;
    c.Font.Color := clRed;
  end;
  c.FillRect(Rect);
  c.TextOut(Rect.Left, Rect.Top, ListBox1.Items[Index]);
end;    //搜尋人名
function Tform1.findItem(Str:String): boolean;
Var
 i: Integer;
begin
result:=false;
For i:=0 to NameList.Count-1 do
 begin
  if NameList.Find(str,i) then begin
   result:=true;
   break;
  end;
 end;
end;
//^====這函數怪怪捏!?
StringList.Find 本就是搜尋方法?怎還使用迴圈!?
未排序使用StringList.IndexOf(Value)
發表人 - yachanga 於 2004/04/30 01:01:26
想買台TPC..TTAB-B12D
TATSU
版主


發表:50
回覆:135
積分:62
註冊:2003-01-16

發送簡訊給我
#8 引用回覆 回覆 發表時間:2004-04-30 02:45:33 IP:219.77.xxx.xxx 未訂閱
unit Unit1;    interface    uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;    type
  PDuplicated = ^TDuplicated ;
  TDuplicated = Record
    IsFound : Boolean ;
  end;
  TForm1 = class(TForm)
    ListBox1: TListBox;
    procedure FormCreate(Sender: TObject);
    procedure ListBox1DrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
    DupList : TList ;
  public
    { Public declarations }
  end;    var
  Form1: TForm1;    implementation    {$R *.dfm}    procedure TForm1.FormCreate(Sender: TObject);
var
  i : Integer ;
  aRecord : PDuplicated ;
begin
  DupList := TList.Create ;
  ListBox1.Style := lbOwnerDrawFixed ;
  ListBox1.Items.BeginUpdate ;
  for i := 1 to 26 do
  begin
    New(aRecord) ;
    if (i mod 4 = 0) then
      aRecord.IsFound := True
    else
      aREcord.IsFound := False ;
    ListBox1.Items.Add(Chr(64   i)) ;
    DupList.Add(aRecord) ;
  end;
  ListBox1.Items.EndUpdate ;
end;    procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
var
  DrawCanvas : TCanvas ;
begin
  DrawCanvas := ListBox1.Canvas ;
  if PDuplicated(DupList.Items[Index]).IsFound then
    DrawCanvas.Font.Color := clRed
  else
    if odSelected in State then
      DrawCanvas.Font.Color := clWhite
    else
      DrawCanvas.Font.Color := clBlack ;
  DrawCanvas.TextRect(Rect, Rect.Left   2, Rect.Top   2, ListBox1.Items[Index]);
end;    procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
  i : Integer ;
begin
  if DupList.Count > 0 then
  begin
    for i := 0 to (DupList.count - 1) do
    begin
      Dispose(PDuplicated(DupList.Items[i])) ;
    end;
    DupList.Clear ;
  end;
  DupList.Free ;
end;    end.    
這個只是模擬的版本,我只是附加一個 TList ,讓你秀出文字時,檢查是否已經存在,如何讀取資料,如何比較你自己做吧,你只需要更改那個 DupList 的值應該可以達到你的要求。
系統時間:2024-07-04 8:39:00
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!