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:
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:
Similarly, when using this code:
RzCheckList1.Items.AddPair('MyName', 'MyValue');
...I get this result:
So, how to add a Name=Value pair to the RzCheckList component and have only the Name part displayed?
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: