Book Example 3 Radio Button and Check Button

Keywords: Programming Delphi Windows

Example 3 of Delphi 7 Programming Skills and Case Elaboration

After reading the title, I first realized it according to my own ideas. I checked the properties of the control once again when I clicked the "display" button to determine the content of ShowMessage.
The sample program given in the book is to update the variables with message content every time the user clicks on the control. I think this should be a waste of resources, but the program is very small, and it is estimated that it will not affect.

Names, gender and academic achievements are relatively simple. Where the hobbies are, create a flag variable to determine whether there is a choice. I feel like I've written a lot of lines of code to set flag to True, and I don't know if VCL itself has any simpler functions to provide this check.

 

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Button1: TButton;
    Edit1: TEdit;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    RadioGroup1: TRadioGroup;
    GroupBox1: TGroupBox;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    CheckBox3: TCheckBox;
    CheckBox4: TCheckBox;
    CheckBox5: TCheckBox;
    CheckBox6: TCheckBox;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

var
  name,gender,grade,hobbies: String;
  hobbyList: Array of String;


procedure TForm1.Button1Click(Sender: TObject);
var
  msg,hobby: String;
  hobbiesFlag: Boolean;

begin
  //clear
  name := '';
  gender := '';
  grade := '';
  hobbies := '';
  hobbiesFlag := False;

  setLength(hobbyList,6);
  hobbyList[0] :='literature';
  hobbyList[1] :='Music';
  hobbyList[2] :='Chess and card';
  hobbyList[3] :='painting';
  hobbyList[4] :='Sports';
  hobbyList[5] :='Mathematics';

  //set name
  name := Edit1.Text;

  //set gender
  if RadioButton1 .Checked then
    gender := 'male';
  if RadioButton2.Checked then
    gender := 'female';

  //set grade
  if RadioGroup1.ItemIndex > -1 then
    case RadioGroup1.ItemIndex of
      0: grade := 'excellent';
      1: grade := 'good';
      2: grade := 'in';
      3: grade := 'difference';
    end;

  //set hobbies
  if CheckBox1.Checked then
  begin
    hobbiesFlag := True;
    hobby := hobby + hobbyList[0] + ',';
  end;
  if CheckBox2.Checked then
  begin
    hobbiesFlag := True;
    hobby := hobby + hobbyList[1] + ',';
  end;
  if CheckBox3.Checked then
  begin
    hobbiesFlag := True;
    hobby := hobby + hobbyList[2] + ',';
  end;
  if CheckBox4.Checked then
  begin
    hobbiesFlag := True;
    hobby := hobby + hobbyList[3] + ',';
  end;
  if CheckBox5.Checked then
  begin
    hobbiesFlag := True;
    hobby := hobby + hobbyList[4] + ',';
  end;
  if CheckBox6.Checked then
  begin
    hobbiesFlag := True;
    hobby := hobby + hobbyList[5] + ',';
  end;

  //show message
  if name = '' then
    showMessage('Please enter your name.')
  else if gender = '' then
    showMessage('Please input gender')
  else if grade = '' then
    showMessage('Please enter your score.')
  else if hobbiesFlag = False then
    showMessage('Please choose your hobbies.')
  else
  begin
    msg := 'Your name is:'+name+ #13#10 +
           'Your gender is:'+gender+ #13#10 +
           'Your academic achievement is:'+grade+ #13#10 +
           'Your hobbies are:' + hobby;
    msg[msg.Length] := #13;
    msg := msg + #10;
    ShowMessage(msg);

end;
end;
end.

Operation results:

  

Posted by Desertwar on Sun, 06 Oct 2019 11:42:11 -0700