線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:3059
推到 Plurk!
推到 Facebook!

多個 Pagecontrol 頁面元件值儲存及還原

答題得分者是:jow
ANDY8C
資深會員


發表:114
回覆:582
積分:299
註冊:2006-10-29

發送簡訊給我
#1 引用回覆 回覆 發表時間:2012-12-05 10:35:16 IP:210.66.xxx.xxx 未訂閱
請問一下
現在 FORM 上有多個 Pagecontrol A, Pagecontrol B, Pagecontrol C
每個 Pagecontrol A/B/C 下,又有 多個 Pagecontrol
例如 : Pagecontrol A 下有 Pagecontrol AA1,Pagecontrol AA2....等
不管第幾層的 Pagecontrol , 都有 TABSHEET
其中有放多個 EDIT , CHECKBOX,COMBOBOX....等元件 (上百個)
現在 設計二個按鍵, 儲存 及 載入
當 USER 在各個 TABSHEET 切換並輸入資料後
最後可用 儲存 鍵, 將全部結果存起來
也可用 載入 鍵,將全部的結果重新讀進來,並顯示出來,用人工按鍵操作都正常
問題 : 想在 程式啟動時,要自動將上次的儲存結果讀進來
所以在 CREATE FORM 時, 模擬了 按下 載入 鍵的功能
(這時FORM 還沒有顯示在螢幕上)
雖然可以動作,但是某些 TABSHEET 的元件值是錯的
此時程式不離開,再按一次 載入 鍵,正確的值就可以顯示出來
( 此時 FORM 就在螢幕上)
爬文後,好像有人提及 FORM 的開啟,關鍵就在 FORM 是否已經開啟完成 ?
我這樣的寫法,不知有何錯誤? 需要改進 ??
網友您們要將畫面上很多個不同頁面的元件值儲存, 下次可以復原, 您們是如何做 ?
謝謝您
------
---------------------------------------
偶爾才來 KTOP ,交流條碼問題,在 FB [條碼標籤達人] 社團留言,感恩.
老大仔
尊榮會員


發表:78
回覆:837
積分:1088
註冊:2006-07-06

發送簡訊給我
#2 引用回覆 回覆 發表時間:2012-12-05 12:52:40 IP:210.61.xxx.xxx 未訂閱
請問~
假如改設在onShow呢?
又或者~
在Form開完後
弄個等待視窗來做載入呢?

我個人在猜...
值之所以會有錯誤
"可能"是元件還沒被建立完成?
就如同您提到的一樣

小弟只是個小小新手
如發言有誤
請包涵~^^
ANDY8C
資深會員


發表:114
回覆:582
積分:299
註冊:2006-10-29

發送簡訊給我
#3 引用回覆 回覆 發表時間:2012-12-05 18:53:22 IP:60.245.xxx.xxx 未訂閱

===================引 用 老大仔 文 章===================
請問~
假如改設在onShow呢? 不行, 非 ACTIVE (FOCUS) 的 TABSHEET ,裡面的元件值都不是 "載入" 的值
又或者~
在Form開完後
弄個等待視窗來做載入呢? 這想法不錯,只是網友不知有其它更好的方法....

我個人在猜...
值之所以會有錯誤
"可能"是元件還沒被建立完成?
就如同您提到的一樣

小弟只是個小小新手
如發言有誤
請包涵~^^



------
---------------------------------------
偶爾才來 KTOP ,交流條碼問題,在 FB [條碼標籤達人] 社團留言,感恩.
jow
尊榮會員


發表:66
回覆:751
積分:1253
註冊:2002-03-13

發送簡訊給我
#4 引用回覆 回覆 發表時間:2012-12-05 22:55:00 IP:123.193.xxx.xxx 未訂閱

[code delphi]
unit fMain;

interface

uses
Windows,
TypInfo,
SysUtils,
Classes,
Controls,
Forms,
StdCtrls,
ComCtrls;

type
//參考來源: http://www.swissdelphicenter.ch/torry/showcode.php?id=2436
TUserConfig = class(TObject)
private
function IsProperty(Obj: TObject; sProp: string): Boolean;
function SetProperty(Obj: TObject; sProp: string; vValue: Variant): Boolean;
function HasAncestor(Child: TComponent; _name: string): Boolean;
public
function SaveToFile(Component: TComponent; fn: string): Boolean;
function LoadFromFile(Component: TComponent; fn: string): Boolean;
end;

TfrmMain = class(TForm)
PageControlA: TPageControl;
TabSheet1: TTabSheet;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
TabSheet2: TTabSheet;
CheckBox1: TCheckBox;
ListBox1: TListBox;
ComboBox1: TComboBox;
Button1: TButton;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
X: TUserConfig;
end;

var
frmMain: TfrmMain;

implementation

{$R *.dfm}

function TUserConfig.IsProperty(Obj: TObject; sProp: string): Boolean;
var
i,n: Integer;
plList: TPropList;
begin
Result := False;

i := GetPropList(
PTypeInfo(Obj.ClassInfo),
[Low(TTypeKind)..High(TTypeKind)],
@plList);

n := 0;
while n begin
Result := plList[n].Name=sProp;
if Result then Break else Inc(n);

end;

end;

function TUserConfig.SetProperty(Obj: TObject; sProp: string; vValue: Variant): Boolean;
begin
Result := IsProperty(Obj,sProp);
if Result then SetPropValue(Obj,sProp,vValue);
end;

function TUserConfig.HasAncestor(Child: TComponent; _name: string): Boolean;
var
C: TComponent;
begin
Result := False;

C := Child;
while C.HasParent do
begin
C := C.GetParentComponent;
if C.Name<>_name then Continue;
Result := True;
Break;
end;


end;

function TUserConfig.SaveToFile(Component: TComponent; fn: string): Boolean;
var
I: Integer;
S: string[255];
C,O: TComponent;
F: TFileStream;
begin
C := Component;
while C.HasParent do
begin
C := C.GetParentComponent;
end;

F := TFileStream.Create(fn,fmCreate);
try
for I := 0 to C.ComponentCount-1 do
begin
O := C.Components[I];
if not (O is TControl) then Continue;
if not HasAncestor(O,Component.Name) then Continue;
S := O.Name;
F.Write(S,Length(S) 1);
F.WriteComponent(O);
end;
Result := True;
finally
FreeAndNil(F);
end;

end;

function TUserConfig.LoadFromFile(Component: TComponent; fn: string): Boolean;
var
n: Integer;
C,O: TComponent;
F: TFileStream;
sName: string[255];
iName: Integer;
begin
Result := False;
if not FileExists(fn) then EXIT;

C := Component;
while C.HasParent do
begin
C := C.GetParentComponent;
end;

F := TFileStream.Create(fn,fmOpenRead);
try
F.Position := 0;
while F.Position begin
F.Read(sName[0], 1);
iName := Byte(sName[0]);
F.Read(sName[1],iName);
for n := 0 to C.ComponentCount-1 do
begin
O := C.Components[n];
if O.Name<>sName then Continue;
SetProperty(O,'Checked', False);
F.ReadComponent(O);
Break;
end;
end;
finally
FreeAndNil(F);
end;

end;

procedure TfrmMain.FormCreate(Sender: TObject);
begin
X := TUserConfig.Create;
X.LoadFromFile(PageControlA,'ABC.DAT');
end;

procedure TfrmMain.FormClose(Sender: TObject; var Action: TCloseAction);
begin
X.SaveToFile(PageControlA,'ABC.DAT');
FreeAndNil(X);
end;

procedure TfrmMain.Button1Click(Sender: TObject);
begin
ListBox1.Items.Add(IntToStr(Random(1000)));
ComboBox1.Items.Add(IntToStr(Random(1000)));
end;

procedure TfrmMain.Button2Click(Sender: TObject);
begin

if ListBox1.Items.Count>0 then
ListBox1.Items.Delete(Random(ListBox1.Items.Count));

if ComboBox1.Items.Count>0 then
ComboBox1.Items.Delete(Random(ComboBox1.Items.Count));

end;

end.
[/code]

ANDY8C
資深會員


發表:114
回覆:582
積分:299
註冊:2006-10-29

發送簡訊給我
#5 引用回覆 回覆 發表時間:2012-12-06 12:05:27 IP:210.66.xxx.xxx 未訂閱
 感謝 jow 兄的範例, 這種寫法算是 "高度技巧",不容易看懂
不過在單層的 PageControl 可以(範例 1),多層 PageContril 上(範例 2,) 還需修改

另外範例 3. 是我目前的做法,很 "土" 及 直覺的寫法
很奇怪,用此範例3,在 FormCreate 時,他是可以還原全部的值,且 edit 都有更新

我出問題的程式,欄位超過百個,有些欄位無法更新正確的值(用 FormCreate 自動載入)
但用 Button 載入,也可以更新正確值.

附上 範例 1,2,3 ;
1 -> 是 jow 兄的 ,
2-> 是多個 PageControl
3-> 一般寫法(我自己寫的)
請到 "會員作品區 " 下載
http://delphi.ktop.com.tw/board.php?cid=31&fid=79&tid=104879

------
---------------------------------------
偶爾才來 KTOP ,交流條碼問題,在 FB [條碼標籤達人] 社團留言,感恩.
編輯記錄
ANDY8C 重新編輯於 2012-12-05 21:14:31, 註解 無‧
ANDY8C
資深會員


發表:114
回覆:582
積分:299
註冊:2006-10-29

發送簡訊給我
#6 引用回覆 回覆 發表時間:2012-12-06 12:18:19 IP:210.66.xxx.xxx 未訂閱
// 範例 3
unit fMain3;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils,
System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
Vcl.StdCtrls, Vcl.ComCtrls,
TypInfo, Vcl.ExtCtrls ;
type
TfrmMain = class(TForm)
PageControlA: TPageControl;
TabSheet1: TTabSheet;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
TabSheet2: TTabSheet;
CheckBox1: TCheckBox;
ComboBox1: TComboBox;
Button1: TButton;
Button2: TButton;
Panel1: TPanel;
PageControlA1: TPageControl;
TabSheet3: TTabSheet;
TabSheet4: TTabSheet;
Edit4: TEdit;
Edit5: TEdit;
RadioGroup1: TRadioGroup;
Edit6: TEdit;
Panel2: TPanel;
Edit7: TEdit;
CheckBox2: TCheckBox;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure CheckBox2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmMain: TfrmMain;
implementation
{$R *.dfm}
procedure TfrmMain.FormCreate(Sender: TObject);
begin
try // 無法自動載入
// 第二層的 PAGECONTROL 會產生錯誤
// X := TUserConfig.Create;
// X.LoadFromFile(PageControlA,'ABC2.DAT');
Button2Click(Sender);
except
// 略過
end;
end;
procedure TfrmMain.Button1Click(Sender: TObject);
// 儲存
var
F : TextFile;
nCC ,i , nCCItem: integer;
sTmp,sVal,sComponentName ,s: string;
bTF : boolean;
sComponentType : TComponent;
sEdit_Save ,sCheckBox_Save ,sComboBox_Save ,
sRadioGroup_Save ,sMemo_Save : string;
sAll ,Save_str ,Save_str1 ,Save_str2 ,sRnd6: string ;
xsEdit,xsCheckBox , xsComboBox, xsRadioGroup : TStringList;
begin
xsEdit := TStringList.Create;
xsCheckBox := TStringList.Create;
xsComboBox := TStringList.Create;
xsRadioGroup := TStringList.Create;
sAll := '';
nCC := self.ComponentCount;
nCCItem := 0;
// 整理 各物件 的資料在 TstringList 中
//-------Tcheckbox 欄位 ------------------------------------------
for i:=0 to nCC - 1 do
begin
sTmp := '';
sVal := '';
sComponentType := self.Components[i];
sComponentName := self.Components[i].Name;// 大寫
// ------ ----------------------
try
if sComponentType is TCheckBox then
begin
nCCItem := nCCItem 1;
bTF := Tcheckbox(FindComponent( sComponentName )).Checked;
if bTF then
xsCheckBox.Add( UpperCase(sComponentName) '=T' )
else
xsCheckBox.Add( UpperCase(sComponentName) '=F' );
end; // if sComponentType is TCheckBox
except
// 錯誤訊息
end;
end; // for i:=0 to nCC - 1 do
//--------TEdit 欄位-----------------------------------------
nCCItem := 0;
for i:=0 to nCC - 1 do
begin
sTmp := '';
sVal := '';
sComponentType := self.Components[i];
sComponentName := self.Components[i].Name; // 大寫
// ------ ----------------------
try
if sComponentType is TEdit then
begin
nCCItem := nCCItem 1;
sTmp := TEdit(FindComponent( sComponentName )).Text;
xsEdit.Add( UpperCase(sComponentName) '=' sTmp );
end; // if sComponentType is
except
// 錯誤訊息
end;
end; // for i:=0 to nCC - 1 do
//--------TCOMBOBOX 欄位-----------------------------------------
nCCItem := 0;
for i:=0 to nCC - 1 do
begin
sTmp := '';
sVal := '';
sComponentType := self.Components[i];
sComponentName := self.Components[i].Name; // 大寫
// ------ ----------------------
try
if sComponentType is TCOMBOBOX then
begin
nCCItem := nCCItem 1;
sVal := inttostr(TCOMBOBOX(FindComponent( sComponentName )).ItemIndex);
xsComboBox.Add( UpperCase(sComponentName) '=' sVal );
end; // if sComponentType is
except
// 錯誤訊息
end;
end; // for i:=0 to nCC - 1 do
//--------TRadioGROUP 欄位-----------------------------------------
nCCItem := 0;
for i:=0 to nCC - 1 do
begin
sTmp := '';
sVal := '';
sComponentType := self.Components[i];
sComponentName := self.Components[i].Name; // 大寫
// ------ ----------------------
try
if sComponentType is TRadioGROUP then
begin
nCCItem := nCCItem 1;
sVal := inttostr(TRadioGROUP(FindComponent( sComponentName )).ItemIndex);
xsRadioGroup.Add( UpperCase(sComponentName) '=' sVal );
end; // if sComponentType is
except
// 錯誤訊息
end;
end; // for i:=0 to nCC - 1 do
// ------------------------------------------------
// 排序
xsEdit.Sort;
xsCheckBox.Sort;
xsComboBox.Sort;
xsRadioGroup.Sort;
// xsMemo.Sort;
// 個別壓縮 或 加密處理
sEdit_Save := xsEdit.CommaText; // AES( xsEdit.Text );
sCheckBox_Save := xsCheckBox.CommaText; // AES( xsCheckBox.Text );
sComboBox_Save := xsComboBox.CommaText; // AES( xsComboBox.Text );
sRadioGroup_Save := xsRadioGroup.CommaText; // AES( xsRadioGroup.Text );
// 開啟文字檔
// 將 個別壓縮 或 加密處理後的資料,以文字檔型式存檔
try
AssignFile( F , 'ABCD.TXT' );
except
// 檔案開啟錯誤 訊息
exit;
end;
Save_str1 := '日期:' DATETIMETOSTR( NOW ) #13#10
'----------------------------' #13#10 ;
Save_str2 :=
sEdit_Save #13#10 // 以下,都已經個別壓縮 或 加密處理
sCheckBox_Save #13#10
sComboBox_Save #13#10
sRadioGroup_Save #13#10 ;
Save_str := Save_str1 Save_str2 ;
try
try
ReWrite( F ); // 開啟文字檔
Write( F , Save_str ); // 以文字檔格式存入
except
showmessage('文字檔案儲存錯誤 !!');
exit;
end;
finally
showmessage(' 儲存完成 !!');
CloseFile( F );
end;
xsEdit.Free;
xsCheckBox.Free;
xsComboBox.Free;
xsRadioGroup.Free;
end;
// 載入
procedure TfrmMain.Button2Click(Sender: TObject);
var
F : TextFile;
k, nCC ,i , nCCItem : integer;
sTmp,sVal,sComponentName , sPath : string;
bTF : boolean;
sComponentType : TComponent;
xsEdit,xsCheckBox , xsComboBox, xsRadioGroup : TStringList;
begin
xsEdit := TStringList.Create;
xsCheckBox := TStringList.Create;
xsComboBox := TStringList.Create;
xsRadioGroup := TStringList.Create;
nCC := self.ComponentCount;
nCCItem := 0;
//---- 文字檔 存在嗎 ? -------------------------------------
if not(Fileexists( 'ABCD.TXT' )) then
begin
showmessage(' ABCD.TXT 不存在 !! ');
exit;
end;
//---- 載入文字檔 到 TstringList 中 ---------------
try
AssignFile( F , 'ABCD.TXT' );
Reset(F);
except
showmessage(' ABCD.TXT 檔案存取錯誤 !!');
exit;
end;
// 逐筆讀進來
try // aaaa
try // bbbb
ReadLN( F , sTmp ); // 讀取第 01 行
ReadLN( F , sTmp ); // 讀取第 02 行
ReadLN( F , sTmp ); // 讀取文字檔第 3 行
xsEdit.CommaText := sTmp ;
ReadLN( F , sTmp ); // 讀取文字檔第 4 行
xsCheckBox.CommaText := sTmp ;
ReadLN( F , sTmp ); // 讀取文字檔第 5 行
xsComboBox.CommaText := sTmp ;
ReadLN( F , sTmp ); // 讀取文字檔第 6 行
xsRadioGroup.CommaText := sTmp ;
except
showmessage(' 文字檔讀取錯誤 !!');
exit;
end; // bbbb
finally
CloseFile( F );
end; // aaaa
// --------------------------------------------------------
// 指定值給各個輸入的物件
//-------Tcheckbox 欄位 ----------------------------
for i:=0 to nCC - 1 do
begin
sTmp := '';
sVal := '';
sComponentType := self.Components[i];
sComponentName := self.Components[i].Name;// 大寫
// ------ ----------------------
try
if sComponentType is TCheckBox then
begin
nCCItem := nCCItem 1;
bTF := False;
if xsCheckBox.Values[ sComponentName ] ='T' then
bTF := True;
// 反向指定值 給螢幕的元件
Tcheckbox(FindComponent(sComponentName)).Checked:=bTF;
end; // if sComponentType is TCheckBox
except
// 錯誤訊息
end;
end; // for i:=0 to nCC - 1 do
//--------TCOMBOBOX 欄位--------------------------------
nCCItem := 0;
for i:=0 to nCC - 1 do
begin
sTmp := '';
sVal := '';
sComponentType := self.Components[i];
sComponentName := self.Components[i].Name; // 大寫
// ------ ----------------------
try
if sComponentType is TCOMBOBOX then
begin
nCCItem := nCCItem 1;
sVal := xsComboBox.Values[ sComponentName ];
// 反向指定值 給螢幕的元件
TCOMBOBOX(FindComponent(sComponentName)).ItemIndex:=
strtoint( sVal );
end; // if sComponentType is
except
// 錯誤訊息
end;
end; // for i:=0 to nCC - 1 do
//--------TRadioGROUP 欄位----------------------------
nCCItem := 0;
for i:=0 to nCC - 1 do
begin
sTmp := '';
sVal := '';
sComponentType := self.Components[i];
sComponentName := self.Components[i].Name; // 大寫
// ------ ----------------------
try
if sComponentType is TRadioGROUP then
begin
nCCItem := nCCItem 1;
sVal := xsRadioGROUP.Values[ sComponentName ];
// 反向指定值 給螢幕的元件
TRadioGroup(FindComponent(sComponentName)).ItemIndex:=
strtoint( sVal );
end; // if sComponentType is
except
// 錯誤訊息
end;
end; // for i:=0 to nCC - 1 do
//--------TEdit 欄位-------------------------------------
nCCItem := 0;
for i:=0 to nCC - 1 do
begin
sTmp := '';
sVal := '';
sComponentType := self.Components[i];
sComponentName := self.Components[i].Name; // 大寫
// ------ ----------------------
try
if sComponentType is TEdit then
begin
nCCItem := nCCItem 1;
// 指定 值 給螢幕的元件
TEdit(FindComponent( sComponentName )).Text:=
xsEdit.Values[ sComponentName ];
end; // if sComponentType is
except
// 錯誤訊息
end;
end; // for i:=0 to nCC - 1 do
// ------------------------------------------------
xsEdit.Free;
xsCheckBox.Free;
xsComboBox.Free;
xsRadioGroup.Free;
end;
procedure TfrmMain.CheckBox2Click(Sender: TObject);
begin
edit7.Visible := CheckBox2.Checked;
end;
end.
------
---------------------------------------
偶爾才來 KTOP ,交流條碼問題,在 FB [條碼標籤達人] 社團留言,感恩.
jow
尊榮會員


發表:66
回覆:751
積分:1253
註冊:2002-03-13

發送簡訊給我
#7 引用回覆 回覆 發表時間:2012-12-06 22:54:05 IP:123.193.xxx.xxx 未訂閱

[code delphi]
unit fMain;
interface

uses
Windows,
SysUtils,
Classes,
Controls,
Forms,
StdCtrls,
ExtCtrls,
ComCtrls;

type
TUserConfig = class(TObject)
private
F_Owner: TForm;
function ControlType(O: TComponent): Integer;
public
constructor Create(AOwner: TForm);
procedure SaveToFile2(fn: string);
procedure LoadFromFile2(fn: string);
end;

TfrmMain = class(TForm)
PageControlA: TPageControl;
PageControl1: TPageControl;
PageControl2: TPageControl;
PageControl3: TPageControl;
PageControl4: TPageControl;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
TabSheet3: TTabSheet;
TabSheet4: TTabSheet;
TabSheet5: TTabSheet;
TabSheet6: TTabSheet;
Edit1: TEdit;
Edit2: TEdit;
Edit4: TEdit;
ListBox1: TListBox;
ListBox2: TListBox;
ComboBox1: TComboBox;
ComboBox2: TComboBox;
CheckBox1: TCheckBox;
RadioGroup1: TRadioGroup;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
X: TUserConfig;
end;

var
frmMain: TfrmMain;

implementation

{$R *.dfm}

constructor TUserConfig.Create(AOwner: TForm);
begin
inherited Create;
F_Owner := AOwner;
end;

function TUserConfig.ControlType(O: TComponent): Integer;
begin
Result := -1;
if O is TEdit then Result := 0
else if O is TListBox then Result := 1
else if O is TComboBox then Result := 2
else if O is TCheckBox then Result := 3
else if O is TRadioGroup then Result := 4
else if O is TPageControl then Result := 5;
end;

procedure TUserConfig.LoadFromFile2(fn: string);
var
L,M: TStringList;
n,I,K: Integer;
O: TComponent;
S: string;
begin

if not FileExists(fn) then EXIT;

L := TStringList.Create;
M := TStringList.Create;
try
M.Delimiter := ';';
L.LoadFromFile(fn);
for I := 0 to F_Owner.ComponentCount-1 do
begin
O := F_Owner.Components[I];
n := ControlType(O);
if n=-1 then Continue;

M.DelimitedText := L.Values[UpperCase(O.Name)];
for K := 0 to M.Count-1 do
begin
S := Trim(M[K]);
case n of
0:
case K of
0: TEdit(O).Text := S;
end;
1:
case K of
0: TListBox(O).Items.CommaText := S;
1: TListBox(O).ItemIndex := StrToIntDef(S,-1);
end;
2:
case K of
0: TComboBox(O).Items.CommaText := S;
1: TComboBox(O).ItemIndex := StrToIntDef(S,-1);
end;
3:
case K of
0: TCheckBox(O).Checked := S='1';
end;
4:
case K of
0: TRadioGroup(O).Items.CommaText := S;
1: TRadioGroup(O).ItemIndex := StrToIntDef(S,-1);
end;
5:
case K of
0: TPageControl(O).ActivePageIndex := StrToIntDef(S,-1);
end;
end;
end;
end;
finally
FreeAndNil(M);
FreeAndNil(L);
end;

end;

procedure TUserConfig.SaveToFile2(fn: string);
var
n,I: Integer;
L,M: TStringList;
O: TComponent;
begin
L := TStringList.Create;
M := TStringList.Create;
try
M.Delimiter := ';';
for I := 0 to F_Owner.ComponentCount-1 do
begin
O := F_Owner.Components[I];
n := ControlType(O);
if n = -1 then Continue;

M.Clear;
case n of
0: with TEdit(O) do M.Add(Text);
1:
with TListBox(O) do
begin
M.Add(Items.CommaText);
M.Add(IntToStr(ItemIndex));
end;
2:
with TComboBox(O) do
begin
M.Add(Items.CommaText);
M.Add(IntToStr(ItemIndex));
end;
3: with TCheckBox(O) do M.Add(IntToStr(Ord(Checked)));
4:
with TRadioGroup(O) do
begin
M.Add(Items.CommaText);
M.Add(IntToStr(ItemIndex));
end;
5: with TPageControl(O) do M.Add(IntToStr(ActivePageIndex));
end;

if M.DelimitedText<>'' then
begin
L.Values[UpperCase(O.Name)] := M.DelimitedText;
end;


end;

if L.Count=0 then DeleteFile(fn) else L.SaveToFile(fn);

finally
FreeAndNil(M);
FreeAndNil(L);
end;

end;

{ TfrmMain }

procedure TfrmMain.FormCreate(Sender: TObject);
begin
X := TUserConfig.Create(Self);
X.LoadFromFile2('WXYZ.TXT');
end;

procedure TfrmMain.FormClose(Sender: TObject; var Action: TCloseAction);
begin
X.SaveToFile2('WXYZ.TXT');
FreeAndNil(X);
end;


end.
[/code]

ANDY8C
資深會員


發表:114
回覆:582
積分:299
註冊:2006-10-29

發送簡訊給我
#8 引用回覆 回覆 發表時間:2012-12-06 23:41:54 IP:210.66.xxx.xxx 未訂閱
 JOW 太強了,感恩 !!

前輩您好人做到底....可否上傳到作品區,方便下載...

謝謝您
------
---------------------------------------
偶爾才來 KTOP ,交流條碼問題,在 FB [條碼標籤達人] 社團留言,感恩.
taishyang
站務副站長


發表:377
回覆:5490
積分:4563
註冊:2002-10-08

發送簡訊給我
#9 引用回覆 回覆 發表時間:2012-12-07 09:27:21 IP:114.32.xxx.xxx 訂閱
高高手jow前輩已將範例放到下面連結供下載 ^^
http://delphi.ktop.com.tw/board.php?cid=31&fid=79&tid=104880

===================引 用 ANDY8C 文 章===================
JOW 太強了,感恩 !!

前輩您好人做到底....可否上傳到作品區,方便下載...

謝謝您
編輯記錄
taishyang 重新編輯於 2012-12-06 18:28:15, 註解 無‧
ANDY8C
資深會員


發表:114
回覆:582
積分:299
註冊:2006-10-29

發送簡訊給我
#10 引用回覆 回覆 發表時間:2012-12-09 14:53:31 IP:140.135.xxx.xxx 未訂閱

剛用 jow 的範例測試,加入 UpDown 的功能

發現一個 EDIT 的小問題
若 EDIT 的 增/減是透過 UpDown 來進行
在程式執行時後(第一個畫面),螢幕的此 EDIT 值都顯示
UpDown MIN 值(不會顯示上次的存檔值)

若用按鍵 "重新載入" ,才會看到上次EDIT 存檔值
這好像不是上次我說的 Focus 問題,
不知是 EDIT 或 UPDOWN 的那個 Properties 要設定
還是有其它方式可以改善.....
已附上 測試的程式於 "作品區"

TEST_18.ZIP



謝謝您

------
---------------------------------------
偶爾才來 KTOP ,交流條碼問題,在 FB [條碼標籤達人] 社團留言,感恩.
jow
尊榮會員


發表:66
回覆:751
積分:1253
註冊:2002-03-13

發送簡訊給我
#11 引用回覆 回覆 發表時間:2012-12-10 02:43:17 IP:123.193.xxx.xxx 未訂閱

[code delphi]
function TUserConfig.ControlType(O: TComponent): Integer;
begin
Result := -1;
if O is TEdit then Result := 0
else if O is TListBox then Result := 1
else if O is TComboBox then Result := 2
else if O is TCheckBox then Result := 3
else if O is TRadioGroup then Result := 4
else if O is TPageControl then Result := 5
else if O is TUpDown then Result := 6;
end;

procedure TUserConfig.LoadFromFile2(fn: string);
var
L,M: TStringList;
n,I,K: Integer;
O,P: TComponent;
S: string;
begin
{省略...}

case n of
{省略...}
6:
begin
case K of
0: TUpDown(O).Max := StrToIntDef(S,100);
1: TUpDown(O).Min := StrToIntDef(S,0);
2: TUpDown(O).Position := StrToIntDef(S,0);
end;
P := TUpDown(O).Associate;
case ControlType(P) of
0: TEdit(P).Text := IntToStr(TUpDown(O).Position);
end;
end;
{省略...}

end;
end;

procedure TUserConfig.SaveToFile2(fn: string);
var
n,I: Integer;
L,M: TStringList;
O: TComponent;
begin
{省略...}

case n of
{省略...}
6:
with TUpDown(O) do
begin
M.Add(IntToStr(Max));
M.Add(IntToStr(Min));
M.Add(IntToStr(Position));
end;
end;
{省略...}

end;

[/code]


編輯記錄
jow 重新編輯於 2012-12-09 11:49:05, 註解 無‧
jow 重新編輯於 2012-12-09 11:57:10, 註解 縮排有問題>.<, 試試全形空白 ... ‧
jow 重新編輯於 2012-12-09 11:59:31, 註解 無‧
ANDY8C
資深會員


發表:114
回覆:582
積分:299
註冊:2006-10-29

發送簡訊給我
#12 引用回覆 回覆 發表時間:2012-12-10 08:04:42 IP:210.66.xxx.xxx 未訂閱
 jow 前輩,剛測試一下, ok 了
簡潔的程式, 要功力深厚的人才能完成.

感恩

謝謝您

------
---------------------------------------
偶爾才來 KTOP ,交流條碼問題,在 FB [條碼標籤達人] 社團留言,感恩.
系統時間:2024-05-02 6:02:34
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!