主题:[讨论]请前辈进来指点一下.有问题没解决
goodluvy
[专家分:0] 发布于 2008-11-15 03:31:00
本人对Pascal语言已初步入门了,现在在学组件。现有一问题想请问一下各前辈:
有1个RadioGroup,Item有4个,分别为1.2.3.4. 又有一GroupBox,内含6个CheckBox.分别为A.B.C.D.E.F. 再有1 Edit编辑框,用于显示结果.
现在要求是:当RadioGroup里选择为3时,6个CheckBox只能选择3个,3个选择完后其它CheckBox为灰色不可选.同理,当RadioGroup选择为2时6个CheckBox只能选择2个,选完后其它变灰不可选,然后通过Edit.Text输出结果。同时希望能实现CheckBox的选择能实时显示在Edit.Text里面。
请教一下思路,或者给个代码参考一下?谢谢了请各位来指点一下这个Delphi的后起之秀啊
回复列表 (共6个回复)
沙发
goodluvy [专家分:0] 发布于 2008-11-15 17:34:00
这么多人看。。。没一个回复一下吗?奇怪了..
板凳
hbxfwjww [专家分:30] 发布于 2008-11-16 06:31:00
看起来很复杂,做起来挺简单的.但问题是:这个论坛好像不能发附件,咋给你呢?
3 楼
hbxfwjww [专家分:30] 发布于 2008-11-16 06:32:00
发源码吧,这个是unit的:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
rg1: TRadioGroup;
rb1: TRadioButton;
rb2: TRadioButton;
rb3: TRadioButton;
rb4: TRadioButton;
grp1: TGroupBox;
chk1: TCheckBox;
chk2: TCheckBox;
chk3: TCheckBox;
chk4: TCheckBox;
chk5: TCheckBox;
chk6: TCheckBox;
edt1: TEdit;
procedure rb2Click(Sender: TObject);
procedure rb3Click(Sender: TObject);
procedure rb4Click(Sender: TObject);
procedure rb1Click(Sender: TObject);
procedure chk1Click(Sender: TObject);
private
{ Private declarations }
procedure DoSel(idx:Integer);
procedure CheckSel(add:Integer);
public
{ Public declarations }
end;
var
Form1: TForm1;
sel:Integer=1;
selcount:Integer=0;
implementation
{$R *.dfm}
procedure tform1.CheckSel(add:Integer);
var
i:Integer;
begin
Inc(selcount,add);
edt1.Text:='maxsel:'+inttostr(sel) + ',selcount:'+inttostr(selcount);
if selcount >=sel then
begin
for i:=0 to ComponentCount-1 do
begin
if Components[i] is TCheckBox then
begin
if not TCheckBox(Components[i]).Checked then
TCheckBox(Components[i]).Enabled:=False;
end;
end;
end
else
begin
for i:=0 to ComponentCount-1 do
begin
if Components[i] is TCheckBox then
begin
if not TCheckBox(Components[i]).Enabled then
TCheckBox(Components[i]).Enabled:=True;
end;
end;
end;
end;
procedure tform1.DoSel(idx:Integer);
var
i:integer;
begin
sel:=idx;
for i:=0 to ComponentCount-1 do
begin
if Components[i] is TCheckBox then
begin
TCheckBox(Components[i]).Enabled:=True;
TCheckBox(Components[i]).Checked:=False;
end;
end;
selcount:=0;
edt1.Text:='maxsel:'+inttostr(sel) + ',selcount:'+inttostr(selcount);
end;
procedure TForm1.rb2Click(Sender: TObject);
begin
dosel(2);
end;
procedure TForm1.rb3Click(Sender: TObject);
begin
dosel(3);
end;
procedure TForm1.rb4Click(Sender: TObject);
begin
dosel(4);
end;
procedure TForm1.rb1Click(Sender: TObject);
begin
dosel(1);
end;
procedure TForm1.chk1Click(Sender: TObject);
begin
if TCheckBox(Sender).Checked then
CheckSel(1)
else
CheckSel(-1);
end;
end.
4 楼
hbxfwjww [专家分:30] 发布于 2008-11-16 06:33:00
这个是Form的:
object Form1: TForm1
Left = 229
Top = 146
Width = 410
Height = 298
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object rg1: TRadioGroup
Left = 32
Top = 40
Width = 161
Height = 177
Caption = 'rg1'
TabOrder = 0
end
object rb1: TRadioButton
Left = 64
Top = 72
Width = 113
Height = 17
Caption = 'rb1'
Checked = True
TabOrder = 1
TabStop = True
OnClick = rb1Click
end
object rb2: TRadioButton
Left = 64
Top = 104
Width = 113
Height = 17
Caption = 'rb2'
TabOrder = 2
OnClick = rb2Click
end
object rb3: TRadioButton
Left = 64
Top = 136
Width = 113
Height = 17
Caption = 'rb3'
TabOrder = 3
OnClick = rb3Click
end
object rb4: TRadioButton
Left = 64
Top = 176
Width = 113
Height = 17
Caption = 'rb4'
TabOrder = 4
OnClick = rb4Click
end
object grp1: TGroupBox
Left = 200
Top = 40
Width = 161
Height = 177
Caption = 'grp1'
TabOrder = 5
object chk1: TCheckBox
Left = 16
Top = 24
Width = 97
Height = 17
Caption = 'chk1'
TabOrder = 0
OnClick = chk1Click
end
object chk2: TCheckBox
Left = 16
Top = 48
Width = 97
Height = 17
Caption = 'chk2'
TabOrder = 1
OnClick = chk1Click
end
object chk3: TCheckBox
Left = 16
Top = 72
Width = 97
Height = 17
Caption = 'chk3'
TabOrder = 2
OnClick = chk1Click
end
object chk4: TCheckBox
Left = 16
Top = 96
Width = 97
Height = 17
Caption = 'chk4'
TabOrder = 3
OnClick = chk1Click
end
object chk5: TCheckBox
Left = 16
Top = 120
Width = 97
Height = 17
Caption = 'chk5'
TabOrder = 4
OnClick = chk1Click
end
object chk6: TCheckBox
Left = 16
Top = 144
Width = 97
Height = 17
Caption = 'chk6'
TabOrder = 5
OnClick = chk1Click
end
end
object edt1: TEdit
Left = 32
Top = 224
Width = 321
Height = 21
TabOrder = 6
Text = 'edt1'
end
end
5 楼
hbxfwjww [专家分:30] 发布于 2008-11-16 06:34:00
运行界面就发不上来了,自己编译出来看吧.
注意那几个CheckBox的OnClick都指向一个过程了.
6 楼
goodluvy [专家分:0] 发布于 2008-11-16 13:43:00
谢谢楼上的,你为新人讲解的态度是我学习下去的动力哟.
我来回复