这个方法怎么很笨,有什么聪明点的方法吗??? ( 积分: 50 )

  • 主题发起人 主题发起人 Jason Law
  • 开始时间 开始时间
J

Jason Law

Unregistered / Unconfirmed
GUEST, unregistred user!
procedure TfrmIncome.CheckBox1Click(Sender: TObject);
begin
if CheckBox1.Checked then begin
Edit1.Enabled :=True;
Edit1.SetFocus;
Edit1.Color :=clWindow;
end else begin
Edit1.Enabled :=False;
Edit1.Text :='0';
Edit1.Color :=Self.Color;
end;
end;

procedure TfrmIncome.CheckBox2Click(Sender: TObject);
begin
if CheckBox2.Checked then begin
Edit2.Enabled :=True;
Edit2.SetFocus;
Edit2.Color :=clWindow;
end else begin
Edit2.Enabled :=False;
Edit2.Text :='0';
Edit2.Color :=Self.Color;
end;
end;

procedure TfrmIncome.CheckBox3Click(Sender: TObject);
begin
if CheckBox3.Checked then begin
Edit3.Enabled :=True;
Edit3.SetFocus;
Edit3.Color :=clWindow;
end else begin
Edit3.Enabled :=False;
Edit3.Text :='0';
Edit3.Color :=Self.Color;
end;
end;

procedure TfrmIncome.CheckBox4Click(Sender: TObject);
begin
if CheckBox4.Checked then begin
Edit4.Enabled :=True;
Edit4.SetFocus;
Edit4.Color :=clWindow;
end else begin
Edit4.Enabled :=False;
Edit4.Text :='0';
Edit4.Color :=Self.Color;
end;
end;

procedure TfrmIncome.CheckBox5Click(Sender: TObject);
begin
if CheckBox5.Checked then begin
Edit5.Enabled :=True;
Edit5.SetFocus;
Edit5.Color :=clWindow;
end else begin
Edit5.Enabled :=False;
Edit5.Text :='0';
Edit5.Color :=Self.Color;
end;
end;

procedure TfrmIncome.CheckBox6Click(Sender: TObject);
begin
if CheckBox6.Checked then begin
Edit6.Enabled :=True;
Edit6.SetFocus;
Edit6.Color :=clWindow;
end else begin
Edit6.Enabled :=False;
Edit6.Text :='0';
Edit6.Color :=Self.Color;
end;
end;
 
你可以自己建个过程,然后使其共用这一个过程,就可以了.
 
procedure TfrmIncome.CheckBoxClick(Sender: TObject);
var
chckbox:TCheckBox;
strHit: string;
begin
chckbox := Sender AS TCheckBox;
if chckbox.Checked then begin
Edit5.Enabled :=True;
Edit5.SetFocus;
Edit5.Color :=clWindow;
end else begin
Edit5.Enabled :=False;
Edit5.Text :='0';
Edit5.Color :=Self.Color;
end;
end;
 
procedure TForm1.CheckBoxClick(Sender: TObject);

procedure SetStatus(CheckBox: TCheckBox; Edit: TEdit);
begin
if CheckBox.Checked then
begin
Edit.Enabled := True;
if Edit.CanFocus then Edit.SetFocus;
Edit.Color := clWindow;
end
else begin
Edit.Enabled := False;
Edit.Text := '0';
Edit.ParentColor := True;
end;
end;

var
Edt: TComponent;
S: string;
begin
if Sender is TCheckBox then
begin
S := TCheckBox(Sender).Name;
S := 'Edit' + S[Length(S)];
Edt := FindComponent(S);
if Edt is TEdit then
SetStatus(Sender as TCheckBox, Edt as TEdit);
end;
end;
如果控件不是很多的话可以用这个,如果很多的话可以给每个TCheckBox增加一个属性指向TEdit,直接设置
 
多人接受答案了。
 
后退
顶部