DELPHI盒子
!实时搜索: 盒子论坛 | 注册用户 | 修改信息 | 退出
检举帖 | 全文检索 | 关闭广告 | 捐赠
技术论坛
 用户名
 密  码
自动登陆(30天有效)
忘了密码
≡技术区≡
DELPHI技术
lazarus/fpc/Free Pascal
移动应用开发
Web应用开发
数据库专区
报表专区
网络通讯
开源项目
论坛精华贴
≡发布区≡
发布代码
发布控件
文档资料
经典工具
≡事务区≡
网站意见
盒子之家
招聘应聘
信息交换
论坛信息
最新加入: wjy13061029975
今日帖子: 3
在线用户: 3
导航: 论坛 -> DELPHI技术 斑竹:liumazi,sephil  
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2022/5/3 5:18:25
标题:
my homemade: How to use a "CheckBox" in a ComboBox without changing your ComboBox class by Emailx45 浏览:925
加入我的收藏
楼主: 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
作者:
男 emailx45 (emailx45) ▲▲▲▲△ -
普通会员
2022/5/3 5:19:13
1楼: screenshot
此帖子包含附件:
PNG 图像
大小:68.3K
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
信息
登陆以后才能回复
Copyright © 2CCC.Com 盒子论坛 v3.0.1 版权所有 页面执行80.07813毫秒 RSS