delphidelphi-10.4-sydneychecklistbox

How to add a Name=Value pair to the RzCheckList component?


In a Delphi Win32 VCL Application, I use the TRzCheckList component that is similar to the standard TCheckListBox but has several additional features.

TRzCheckList is part of the included "Konopka Signature VCL Controls" available for free from the GetIt Package Manager in the Delphi IDE:

enter image description here

I try to add a "Name=Value" pair to the RzCheckList with this code:

ThisItem := RzCheckList1.AddItemToGroup(0, 'MyName');
RzCheckList1.Items.ValueFromIndex[ThisItem] := 'MyValue';

However, instead of getting "MyName" displayed in the RzCheckList (with "MyValue" as a hidden part of the item), I get this at run-time:

enter image description here

Similarly, when using this code:

RzCheckList1.Items.AddPair('MyName', 'MyValue');

...I get this result:

enter image description here

So, how to add a Name=Value pair to the RzCheckList component and have only the Name part displayed?


Solution

  • Use this code to add the pair to a specific Group:

    RzCheckList1.AddItemToGroup(0, 'MyName=MyValue');
    

    Then implement this code in the OnDrawItem event-handler:

    procedure TformMain.RzCheckList1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState);
    var
      Flags: Longint;
      Data: String;
      FCanvas: TCanvas;
      CheckListBox: TCheckListBox;
    begin
      CheckListBox := TCheckListBox(Control);
      FCanvas := CheckListBox.Canvas;
      FCanvas.FillRect(Rect);
      if Index < CheckListBox.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);
        Data := CheckListBox.Items.Names[Index];
    
        DrawText(FCanvas.Handle, Data, Length(Data), Rect, Flags);
      end;
    end;
    

    This gets you the desired result at run-time:

    enter image description here