我有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;
结果只是我选择循环的第一个复选框。
任何人,请帮助我。
答案 0 :(得分:0)
可能你需要TRadioButtons ......
以下是动态创建的TEdits和TCheckBoxes的代码:
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.
答案 1 :(得分:-1)
您的代码存在一些问题:
在第一个选中的复选框后,使用else
会跳过所有复选框
组合for
和if
- 语句列表没有意义。如果您为每个复选框都有if
- 语句,那么您希望在for
结束时进行迭代?
您的for
以0开头,但第一个复选框似乎是CheckBox1(通常最好使用更具描述性的名称)
您似乎要寻找的方法是FindComponent
找到某个名称或索引的组件。
E.g。它变成了
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;