My "homemade": How to use a "CheckBox" in a ComboBox without changing your ComboBox class ... (or similar List of Items, like ListBox)
Scenary: -- FMX project -- no changes on ComboBox class! -- "1" CheckBox for each item on ComboBox
implementation
{$R *.fmx}
var MyCurrentCheckBoxesOnComboBox: TCheckBox = nil;
procedure TForm1.MyCheckBoxOnChange(Sender: TObject); begin Memo1.Lines.Clear; // for var z: integer := 0 to (ComboBox1.ChildrenCount - 1) do // for tests... begin if (ComboBox1.Children[z] is TCheckBox) then if TCheckBox(ComboBox1.Children[z]).IsChecked then Memo1.Lines.Add(TCheckBox(ComboBox1.Children[z]).Name + ' = Checked') else Memo1.Lines.Add(TCheckBox(ComboBox1.Children[z]).Name + ' = NOT Checked'); end; end;
procedure TForm1.BtnCreatingMyCheckBoxClick(Sender: TObject); var MyCheckBox: TCheckBox; begin BtnCreatingMyCheckBox.Enabled := false; // ComboBox1.Items.Add('MyComboBoxItem0'); ComboBox1.Items.Add('MyComboBoxItem1'); ComboBox1.Items.Add('MyComboBoxItem2'); ComboBox1.Items.Add('MyComboBoxItem3'); // // // for each ComboBox item, you should create a "CheckBox". for var i: integer := 0 to (ComboBox1.Items.Count - 1) do begin MyCheckBox := TCheckBox.Create(ComboBox1); MyCheckBox.Name := Format('MyCheckBox_CmbBox%d', [i]); MyCheckBox.Visible := false; MyCheckBox.Text := ''; // not text please! MyCheckBox.Parent := ComboBox1; // = ComboBox1.AddObject(MyCheckBox); // positioning your checkboxes on "Parent" MyCheckBox.Position.X := 0; // ComboBox1.Position.X + 10; MyCheckBox.Position.Y := (ComboBox1.Height / 2) - (MyCheckBox.Height / 2); // MyCheckBox.OnChange := MyCheckBoxOnChange; end; // ComboBox1.ItemIndex := 0; end;
procedure TForm1.ComboBox1Change(Sender: TObject); begin if (ComboBox1.ItemIndex > -1) then begin // // you can create a list with "only CheckBox" to avoid verify all children in ComboBox1 // for var i: integer := 0 to (ComboBox1.ChildrenCount - 1) do begin if (ComboBox1.Children[i] is TCheckBox) then if (ComboBox1.Children[i].Name = Format('MyCheckBox_CmbBox%d', [ComboBox1.ItemIndex])) then begin if not(MyCurrentCheckBoxesOnComboBox = nil) then MyCurrentCheckBoxesOnComboBox.Visible := false; // MyCurrentCheckBoxesOnComboBox := TCheckBox(ComboBox1.Children[i]); MyCurrentCheckBoxesOnComboBox.Visible := true; // // to "check the choiced"... // pay attention when the list is created... then the first will be selected by default!!! // Maybe it's better wait to "user choice" what "checkbox" will be selected manually!!! // // MyCurrentCheckBoxesOnComboBox.IsChecked := true; // exit; // get out here!!! end; end; end; end;
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3