Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
显示多个复选框,delphi_Delphi_Checkbox_Pascal - Fatal编程技术网

显示多个复选框,delphi

显示多个复选框,delphi,delphi,checkbox,pascal,Delphi,Checkbox,Pascal,我有6个复选框,每个复选框上都有edittext。 我只想在备注中显示选中的复选框及其edittext值。 这是我的密码: //jumCheck is total of selected checkbox for I := 0 to jumCheck - 1 do begin if CheckBox1.Checked then begin Memo1.Lines.Append('Gejala: '+CheckBox1.Caption+', Penyakit: '+Edit1.Text)

我有6个复选框,每个复选框上都有edittext。 我只想在备注中显示选中的复选框及其edittext值。 这是我的密码:

//jumCheck is total of selected checkbox
for I := 0 to jumCheck - 1 do
begin
 if CheckBox1.Checked then
 begin
   Memo1.Lines.Append('Gejala: '+CheckBox1.Caption+', Penyakit: '+Edit1.Text);
 end
 else if CheckBox2.Checked then
 begin
   Memo1.Lines.Append('Gejala: '+CheckBox2.Caption+', Penyakit: '+Edit2.Text);
 end;
end;
结果只是我被选中循环的第一个复选框


任何人,请帮助我。

您的代码有一些问题:

  • 使用
    else
    跳过第一个选中复选框后的所有复选框

  • for
    if
    -语句列表组合在一起是没有意义的。如果每个复选框都有一个
    If
    -语句,那么您想用
    重复什么

  • 您的的
    以0开头,但第一个复选框似乎是CheckBox1(通常最好使用更具描述性的名称)

您似乎在寻找查找某个名称或索引的组件的方法
FindComponent

它变成

for I := 1 to jumCheck  do
begin
 if (FindComponent('CheckBox' + IntToStr(i)) as TCheckBox).Checked then
 begin
   Memo1.Lines.Append('Gejala: '+(FindComponent('CheckBox' + IntToStr(i)) as TCheckBox).Caption+', Penyakit: '+(FindComponent('Edit' + IntToStr(i)) as TEdit).Text);
 end
end;

也许,你需要传统的按钮来代替

以下是动态创建的TEDIT和Tcheckbox的代码:

unit Unit1;

interface

    uses
      Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
      System.Classes, Vcl.Graphics,
      Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

    const
      ElementsCount = 6;

    type
      TForm1 = class(TForm)
        Memo1: TMemo;
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
        chba: array [1 .. ElementsCount] of TCheckBox;
        eda: array [1 .. ElementsCount] of TEdit;
        procedure CBClick(Sender: TObject);
      end;

    var
      Form1: TForm1;

    implementation

    {$R *.dfm}

    procedure TForm1.FormCreate(Sender: TObject);
    var
      i: byte;
    begin
      for i := 1 to ElementsCount do
      begin
        chba[i] := TCheckBox.Create(self);
        chba[i].Tag := i; //you can change the code of CBClick
                          //and know out the sender easier by Tag property
        chba[i].Top := (i - 1) * 30 + 1;
        chba[i].Left := 1;
        chba[i].Caption := 'Some caption ' + inttostr(i);
        chba[i].Parent := self;
        chba[i].OnClick:= CBClick;

        eda[i] := TEdit.Create(self);
        eda[i].Top := (i - 1) * 30 + 1;
        eda[i].Left := 100;
        eda[i].Text := '';
        eda[i].Parent := self;
      end;
    end;

    procedure TForm1.CBClick(Sender: TObject);
    var
      i: byte;
    begin
      Memo1.Text := '';
      for i := 1 to ElementsCount do
      begin
        if chba[i].Checked then
        begin
          Memo1.Lines.Append(chba[i].Caption + ' ' + eda[i].Text);
          exit;//??? In this case only the first checked will be processed
               //Probably, you need TRadioButton's instead
        end;
      end;
    end;

    end.

问题到底是什么?这是FindComponent的复制品不太好。最好使用您在代码中填充的数组。@DavidHeffernan当所有复选框都未选中时,他不需要编辑数组。您不明白我的意思。您没有调用
FindComponent
而是编写
CheckBox[i]
Edit[i]
,或者更好的做法是,您有一个包含一对控件(复选框和编辑)的记录,以及这些记录的数组。这样做的目的是不要调用
FindComponent
。我建议不要调用
FindComponent
。我知道我从不这么做,这不是速度的问题。这里的表演不会有问题。这是一个清晰的问题。在这种情况下,我总是选择静态类型的安全性。它很容易实现,并且使代码更干净。我喜欢让编译器查找错误,而不是等到运行时。这可能太晚了。