4.选择类组件
4.1 CheckBox
复选框(CheckBox)组件具有未选中状态和选中状态,还有一种不确定状态,表示既未选中,也未非选中。主要属性如下:
属性 | 说明 |
---|---|
Checked | 表示CheckBox是否被选中 |
State | 表示CheckBox的状态,取值: cbChecked - 选中 cbUnchecked - 未选中 cbGrayed - 不确定 |
AllowGrayed | 表示是否为三态选项,取值为True时是三态选项,取值为False时,只有未选中和选中两种状态 |
示例:使用CheckBox完成程序员使用的计算机语言的选择,界面如下图:
程序代码如下:
procedure TForm1.Button1Click(Sender: TObject); var SelectStr: String; begin // 选择按钮单击事件 Memo1.Lines.Add('姓名:' + Edit1.Text); Memo1.Lines.Add('性别:' + Edit2.Text); Memo1.Lines.Add('职业:' + Edit3.Text); Memo1.Lines.Add('您喜欢的计算机语言如下:'); SelectStr := ''; if CheckBox1.Checked then SelectStr := SelectStr + CheckBox1.Caption + '、'; if CheckBox2.Checked then SelectStr := SelectStr + CheckBox2.Caption + '、'; if CheckBox3.Checked then SelectStr := SelectStr + CheckBox3.Caption + '、'; if CheckBox4.Checked then SelectStr := SelectStr + CheckBox4.Caption + '、'; if CheckBox5.Checked then SelectStr := SelectStr + CheckBox5.Caption + '、'; if CheckBox6.Checked then SelectStr := SelectStr + CheckBox6.Caption + '、'; Memo1.Lines.Add(copy(SelectStr, 0, length(SelectStr) - 1)); end; procedure TForm1.Button2Click(Sender: TObject); begin // 下一个按钮单击事件 Edit1.Text := ''; Edit2.Text := ''; Edit3.Text := ''; CheckBox1.Checked := False; CheckBox2.Checked := False; CheckBox3.Checked := False; CheckBox4.Checked := False; CheckBox5.Checked := False; CheckBox6.Checked := False; Memo1.Lines.Clear; end;
4.2 RadioButton
单选按钮(RadioButton)一般总是成组工作,主要属性:
属性 | 说明 |
---|---|
Checked | 表示RadioButton是否被选中,True表示被选中,False表示未选中 |
示例:使用RadioButton组件控制Label的字体,界面如下图:
程序代码如下:
procedure TForm1.RadioButton1Click(Sender: TObject); begin // 宋体 Label1.Font.Name := '宋体'; end; procedure TForm1.RadioButton2Click(Sender: TObject); begin // 楷体 Label1.Font.Name := '楷体_GB2312'; end; procedure TForm1.RadioButton3Click(Sender: TObject); begin // 黑体 Label1.Font.Name := '黑体'; end; procedure TForm1.RadioButton4Click(Sender: TObject); begin // 微软雅黑 Label1.Font.Name := '微软雅黑'; end; procedure TForm1.RadioButton5Click(Sender: TObject); begin // 仿宋体 Label1.Font.Name := '仿宋'; end;
4.3 RadioGroup
单选按钮组(RadioGroup)将一个GroupBox和一组RadioButton组合在一起,可以使用统一的索引号ItemIndex来使用。主要属性如下:
属性 | 说明 |
---|---|
Columns | 用于设置单选按钮组中按钮的数量,取值范围1~16,默认值为1 |
Items | 用于设置各个单选按钮的标题 |
ItemIndex | 单选按钮组中被选中的按钮的索引号,从0开始,默认值为-1,表示组中没有被选中的按钮 |
示例:使用RadioButton组件控制Label的字体,界面如下图:
属性设置如下:
对象 | 属性 | 属性值 | 说明 |
---|---|---|---|
RadioGroup1 | ItemIndex | 0 | 默认字体“宋体”被选中 |
Columns | 5 | 列数 | |
Items | 宋体 黑体 楷体 微软雅黑 仿宋体 | 选项标题 | |
Label1 | Caption | 您好!Delphi欢迎您! | 标题 |
Font.Name | 宋体 | 默认字体 |
程序代码如下:
procedure TForm1.RadioGroup1Click(Sender: TObject); begin case RadioGroup1.ItemIndex of 0: Label1.Font.Name := '宋体'; 1: Label1.Font.Name := '黑体'; 2: Label1.Font.Name := '楷体_GB2312'; 3: Label1.Font.Name := '微软雅黑'; 4: Label1.Font.Name := '仿宋'; end; end;