felixRo
felixRo

Reputation: 187

CheckListBox item style

I add items on startup (filenames from a given folder's name) and I'm using this procedure to make things done when clicking items:

procedure TForm1.CheckListBoxClickCheck(Sender: TObject);

how to make the checked item to change its color or style? In other words I click an item and I what to be bold after check.

Upvotes: 1

Views: 1368

Answers (2)

IceCold
IceCold

Reputation: 21154

@Felix
Kobik's answer is the best. I have ABSOLUTELY nothing to comment against it.

But if you are inexperienced with Delphi to create "complex" custom-drawing code like that, there is also a hack-like alternative: compose your own CheckListBox: put multiple checkboxes in a TPanel or TScrollBox and align them to top. This way you have access to each checkbox properties (font face, size, style, etc).
Many new controls can be created this way, from composite controls.

Remember this is just an alternative/dirty hack. Yes. It will work with Delphi styles (vsf).

Upvotes: 0

kobik
kobik

Reputation: 21242

You need to set the Style property to lbOwnerDrawFixed and draw the items yourself in the OnDrawItem event.

e.g.

procedure TForm1.CheckListBox1DrawItem(Control: TWinControl;
  Index: Integer; Rect: TRect; State: TOwnerDrawState);
var
  Flags: Longint;
begin
  with TCheckListBox(Control) do
  begin
    if Checked[Index] then
    begin
      Canvas.Font.Style := Canvas.Font.Style + [fsBold];
      Canvas.Font.Color := clRed;
    end;
    Canvas.FillRect(Rect);
    if Index < Items.Count then
    begin
      Flags := DrawTextBiDiModeFlags(DT_SINGLELINE or DT_VCENTER or DT_NOPREFIX);
      if not UseRightToLeftAlignment then
        Inc(Rect.Left, 2)
      else
        Dec(Rect.Right, 2);
      DrawText(Canvas.Handle, PChar(Items[Index]), Length(Items[Index]), Rect,
        Flags);
    end;
  end;
end;

procedure TForm1.CheckListBox1ClickCheck(Sender: TObject);
begin
  TCheckListBox(Sender).Invalidate;
end;

Note the Invalidate in the OnClickCheck is also needed since that the control is not invalidated when an item is checked/unchecked (at-least not in my current Delphi version).

Upvotes: 3

Related Questions