单选按钮(RadioButton)和复选按框(CheckBox)都是很常用的组件,尤其是在对话框中。单选按钮(RadioButton)是一种具有排他性的选择组件,用户每一次只能选择其中的一个(在同一个容器组件中,如同一Form上或RadioBox中)。而复选框(CheckBox)则不同,可以同时选择多个。
两种组件都有一个Checked属性,用于标识和检测是否被选中。我们使用这两个组件,给上一节(Memo组件介绍)的例程(简单编辑器)添加一些新的功能,使它可以选择字体颜色和字体的样式。
程序的界面如下图所示:
程序如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
SaveDialog1: TSaveDialog;
OpenDialog1: TOpenDialog;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Button6: TButton;
Button7: TButton;
RadioButton1: TRadioButton;
RadioButton2: TRadioButton;
RadioButton3: TRadioButton;
CheckBox1: TCheckBox;
CheckBox2: TCheckBox;
CheckBox3: TCheckBox;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
procedure RadioButton1Click(Sender: TObject);
procedure RadioButton2Click(Sender: TObject);
procedure RadioButton3Click(Sender: TObject);
procedure CheckBox1Click(Sender: TObject);
procedure CheckBox2Click(Sender: TObject);
procedure CheckBox3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
memo1.Lines.Add('在末端添加文本');
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
memo1.Lines.Text:='这里是Memo组件示例';
memo1.Font.Color:=clRed;
//设定文本的初始颜色为红色,因为RadioButton1是默认选中的
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if Memo1.SelLength>0 then
Memo1.CutToClipboard;
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
if savedialog1.Execute then
memo1.Lines.SaveToFile(savedialog1.FileName);
end;
procedure TForm1.Button6Click(Sender: TObject);
begin
if Opendialog1.Execute then
memo1.Lines.LoadFromFile(opendialog1.FileName);
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
if Memo1.SelLength>0 then
Memo1.CopyToClipboard;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
if Memo1.SelLength=0 then
Memo1.PasteFromClipboard;
end;
procedure TForm1.Button7Click(Sender: TObject);
begin
close;
end;
procedure TForm1.RadioButton1Click(Sender: TObject);
begin
if radiobutton1.Checked=true then //判断是否被选中
Memo1.Font.color:=clRed; //改变颜色
end;
procedure TForm1.RadioButton2Click(Sender: TObject);
begin
if radiobutton2.Checked=true then
Memo1.Font.Color:=clBlue;
end;
procedure TForm1.RadioButton3Click(Sender: TObject);
begin
if radiobutton3.Checked=true then
Memo1.Font.Color:=clGreen;
end;
procedure TForm1.CheckBox1Click(Sender: TObject);
begin
if checkbox1.Checked=true then
Memo1.Font.Style:=Memo1.Font.Style+[fsBold]
else
Memo1.Font.Style:=Memo1.Font.Style-[fsBold];
end;
procedure TForm1.CheckBox2Click(Sender: TObject);
begin
if checkbox2.Checked=true then
Memo1.Font.Style:=Memo1.Font.Style+[fsItalic]
else
Memo1.Font.Style:=Memo1.Font.Style-[fsItalic];
end;
procedure TForm1.CheckBox3Click(Sender: TObject);
begin
if checkbox3.Checked=true then
Memo1.Font.Style:=Memo1.Font.Style+[fsUnderline]
else
Memo1.Font.Style:=Memo1.Font.Style-[fsUnderline];
end;
end.