Home > Net >  How to emphasize some specific items from cxDBchecklistbox
How to emphasize some specific items from cxDBchecklistbox

Time:01-09

I have dataset with 2 different sources - some are for goverment and others are for organization only. I need to distinct them somehow, like making some items bold or different color.

I tried to use DrawItem event, but couldn't figure it out.

For adding items I used:

  while not (cdDataset1.Eof) do
   begin
     if ((cdDataset1.fieldbyName('displayName').value<> '')  and (cdDataset1.fieldbyName('TyypId').value=1280781)) then
     begin
     cxDBCheckListBox1.Items.Add.Text:= cdDataset1.fieldbyName('displayName').value;
     end;
     cdDataset1.Next;
   end;

   cdDataset1.First;

   while not (cdDataset1.Eof) do
   begin
   if ((cdDataset1.fieldbyName('displayName').value<> '')  and (cdDataset1.fieldbyName('TyypId').value=1243501)) then
     begin
     cxDBCheckListBox1.Items.Add.Text:= cdDataset1.fieldbyName('displayName').value;
     end;
     cdDataset1.Next;
   end;

This part works well. But can I use field TyypID for distinction on cxdbchecklistbox ? It should look like this(checkboxs intead of bullets ofcourse):

  • Important option1
  • Impotant option2
  • extra info option1

CodePudding user response:

I found the solution. First add tags . OnDrawItem creating canvas is needed:

//adding items to checklistbox where needed. 
    cxDBCheckListBox1.Items.Clear;
    while not (cdDataset1.Eof) do
    begin
       if ((cdDataset1.fieldbyName('displayName').value<> '') and 
           (cdDataset1.fieldbyName('TyypId').value=28078)) then
            begin
              with cxDBCheckListBox1.Items.Add do
                begin
                   Text:= cdDataset1.fieldbyName('displayName').value;
                   Tag := 1280781;
                end;
             end;
             cdDataset1.Next;
    end;

//Then on drawitem:

procedure TfmSample.cxDBCheckListBox1DrawItem(
  Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState);
 var
  ACanvas: TcxCanvas;
  AText: string;
  ATextRect: TRect;
  AGlyphWidth: Integer;
  AListBox: TcxDBCheckListBox;
  ACanvasFont: TFont;
  AItemEnabled: Boolean;
  AItemTag: Integer;
begin
  AListBox := (Control as TcxDBCheckListBox);
  ACanvas := AListBox.InnerCheckListBox.Canvas;
  ACanvasFont := ACanvas.Font;
  AItemTag := AListBox.Items[Index].Tag;
  AItemEnabled := AListBox.Items[Index].Enabled;

  case AItemTag of
     1243501:
      begin
        ACanvasFont.Color := clBlue;
      end;
    1280781 :
      begin
        ACanvasFont.Style := [fsBold];
        ACanvasFont.Color := clBlack;
      end;
  end;
  ACanvas.Brush.Color := clWhite;
   ACanvas.FillRect(Rect);
  AText := AListBox.Items[Index].Text;
  ATextRect := Rect;
  ATextRect.Left := 20;
  ACanvas.DrawTexT(AText, ATextRect, 0);
end;
  •  Tags:  
  • Related