回覆列表
  • 1 # 羊毛學院

    給你一份程式碼,用了笨辦法,目的是讓你更容易讀懂程式的流程,程式碼還可以最佳化,就看你自己的了。程式碼已經測試通過了(Windows XP + Delphi7),把程式碼複製到你的IDE裡就可以了,全部程式碼如下:unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Button1: TButton; cb: TComboBox; ck1: TCheckBox; ck2: TCheckBox; ck3: TCheckBox; ck4: TCheckBox; ck5: TCheckBox; ck6: TCheckBox; procedure cbChange(Sender: TObject); procedure Button1Click(Sender: TObject); procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1; f1: TextFile; //這是檔案變數implementation{$R *.dfm}procedure TForm1.cbChange(Sender: TObject);//begin If cb.ItemIndex = 0 Then //如果選擇第一項 ,則複選框1-3選中 begin ck1.Checked := True; ck2.Checked := True; ck3.Checked := True; ck4.Checked := false; ck5.Checked := false; ck6.Checked := false; end; If cb.ItemIndex = 1 Then //如果選擇第二項 ,則複選框4-6選中 begin ck1.Checked := false; ck2.Checked := false; ck3.Checked := false; ck4.Checked := True; ck5.Checked := True; ck6.Checked := True; end;end;procedure TForm1.Button1Click(Sender: TObject);begin AssignFile(f1,"check.txt"); //以可寫方式開啟一個檔案,不存在則建立,存在則覆蓋 Rewrite(f1); //開啟檔案準備讀寫 //下面程式碼儲存checkbox的狀態,選中為1,未選中為0 if ck1.Checked Then Writeln(f1,"1") else Writeln(f1,"0"); if ck2.Checked Then Writeln(f1,"1") else Writeln(f1,"0"); if ck3.Checked Then Writeln(f1,"1") else Writeln(f1,"0"); if ck4.Checked Then Writeln(f1,"1") else Writeln(f1,"0"); if ck5.Checked Then Writeln(f1,"1") else Writeln(f1,"0"); if ck6.Checked Then Writeln(f1,"1") else Writeln(f1,"0"); CloseFile(f1); //關閉檔案end;procedure TForm1.FormCreate(Sender: TObject); var s1: String; //臨時變數beginif FileExists("check.txt") Then //判斷設定檔案是否存在 begin AssignFile(f1,"check.txt"); Reset(f1); //以只讀方式開啟檔案 Readln(f1,s1); //讀取第一行 ck1.Checked := Boolean(StrToInt(s1));//把第一行的內容轉換為布林值後賦值給複選框1,下同 Readln(f1,s1); ck2.Checked := Boolean(StrToInt(s1)); Readln(f1,s1); ck3.Checked := Boolean(StrToInt(s1)); Readln(f1,s1); ck4.Checked := Boolean(StrToInt(s1)); Readln(f1,s1); ck5.Checked := Boolean(StrToInt(s1)); Readln(f1,s1); ck6.Checked := Boolean(StrToInt(s1)); CloseFile(f1); //一定記得關閉檔案 end;end;end.

  • 中秋節和大豐收的關聯?
  • 有匪君子,如切如磋,如琢如磨"是出自《詩經》的哪一篇?