Recent

Author Topic: [Solved]change color combobox item in runtime ?  (Read 2239 times)

majid.ebru

  • Hero Member
  • *****
  • Posts: 530
[Solved]change color combobox item in runtime ?
« on: August 07, 2023, 12:02:42 pm »
Hi
please help and guide me

i have a combobox and write this code and that worked correctly:
Code: Pascal  [Select][+][-]
  1. procedure TmElements.c205DrawItem(Control: TWinControl;Index: Integer;
  2.   ARect: TRect;State: TOwnerDrawState);
  3. var
  4.   sText: string;
  5. begin
  6.   with (Control as TComboBox).Canvas do begin
  7.     case Index of
  8.       0 :Font.Color := clDefault;
  9.     else begin
  10.         Brush.Color:= StringToColor(stgElement.cells[2,index]);
  11.         if(Index = 11)then
  12.           Font.Color := StringToColor('0');
  13.       end;
  14.     end;
  15.     FillRect(ARect);
  16.     TextRect(ARect, 5, ARect.Top, (Control as TComboBox).Items[Index]);
  17.   end;
  18. end;

now
in run time i create a new combobox , how can I change DrawItem of new combobox
i use this code but didn't work?
Code: Pascal  [Select][+][-]
  1.  
  2.   .
  3.   .
  4.   .
  5.  tmpComBo := TComboBox.Create(mElements);
  6. with(tmpComBo)do begin
  7.   .
  8.   .
  9.   .
  10.   OnDrawItem :=  c205.OnDrawItem;
  11.   .
  12.   .
  13.   .
  14.  
« Last Edit: August 07, 2023, 05:57:41 pm by majid.ebru »

wp

  • Hero Member
  • *****
  • Posts: 13484
Re: change color combobox item in runtime ?
« Reply #1 on: August 07, 2023, 12:26:37 pm »
Code: Pascal  [Select][+][-]
  1. with tmpCmbo do begin
  2.   ...
  3.   OnDrawItem := @c205DrawItem;  // in mode objfpc; or without '@' in mode Delphi
  4.   ...
  5. end;

majid.ebru

  • Hero Member
  • *****
  • Posts: 530
Re: change color combobox item in runtime ?
« Reply #2 on: August 07, 2023, 01:18:49 pm »
Code: Pascal  [Select][+][-]
  1. with tmpCmbo do begin
  2.   ...
  3.   OnDrawItem := @c205DrawItem;  // in mode objfpc; or without '@' in mode Delphi
  4.   ...
  5. end;
Unfortunately, this code did not work

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: change color combobox item in runtime ?
« Reply #3 on: August 07, 2023, 01:27:13 pm »
Unfortunately, this code did not work
What does that actually mean ? It does not get called at all ?, it draws things wrong ? your car wouldn't start when running your code ? It starts raining each and every time you run your code ;)

Since you created the combobox dynamically, have you set the ownerdraw style ? see also https://wiki.lazarus.freepascal.org/TComboBox#Owner_drawn_ComboBox
Today is tomorrow's yesterday.

Joanna

  • Hero Member
  • *****
  • Posts: 1440
Re: change color combobox item in runtime ?
« Reply #4 on: August 07, 2023, 04:59:05 pm »
Another thing that is sometimes needed for owner drawn combobox is an onmeasure item event..

majid.ebru

  • Hero Member
  • *****
  • Posts: 530
Re: change color combobox item in runtime ?
« Reply #5 on: August 07, 2023, 05:57:04 pm »
Hi
I found answer.
I should set style property.
Code: Pascal  [Select][+][-]
  1. Style := csOwnerDrawFixed ; // or csOwnerDrawVariable
  2.  

Thanks everybody

 

TinyPortal © 2005-2018