《Delphi 7編程技巧與實例精解》實例3編程
看了題目以後我先按本身的思路實現了,我是在點擊「顯示」按鍵的時候再一次性檢查控件的屬性,來肯定ShowMessage的內容。
書本給出的程序樣例是在每次用戶點擊控件的時候就更新一次存有message內容的變量,我想這樣應該比較浪費資源吧,不過程序很小,估計沒影響。api
姓名、性別、學習成績這些都相對簡單。興趣愛好那裏,創建一個flag變量來肯定是否有選擇。感受寫了好多行把flag設置爲True的代碼,不知道VCL自己有沒有更簡單的函數來提供這個檢驗。函數
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] :='文學'; hobbyList[1] :='音樂'; hobbyList[2] :='棋牌'; hobbyList[3] :='繪畫'; hobbyList[4] :='體育'; hobbyList[5] :='數學'; //set name name := Edit1.Text; //set gender if RadioButton1 .Checked then gender := '男'; if RadioButton2.Checked then gender := '女'; //set grade if RadioGroup1.ItemIndex > -1 then case RadioGroup1.ItemIndex of 0: grade := '優'; 1: grade := '良'; 2: grade := '中'; 3: grade := '差'; 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('請輸入名字') else if gender = '' then showMessage('請輸入性別') else if grade = '' then showMessage('請輸入成績') else if hobbiesFlag = False then showMessage('請選擇愛好') else begin msg := '您的姓名是:'+name+ #13#10 + '您的性別是:'+gender+ #13#10 + '您的學習成績是:'+grade+ #13#10 + '您的愛好是:' + hobby; msg[msg.Length] := #13; msg := msg + #10; ShowMessage(msg); end; end; end.
運行結果:學習