2012-08-01 55 views
0

我首先創建了一個將顯示設置的表單。然後我創建了一個登錄框,它將從一個ini文件中加載一個密碼。我原本以爲加載ini文件時出錯。雖然我已經隔離它,當我加載設置表單。這是所有這些代碼。嘗試更改表單時出現程序錯誤

的設置屏幕的代碼:

unit Unit1; 

interface 

uses 
    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, 
    StdCtrls, inifiles; 

type 

    TForm1 = class(TForm) 
    SaveButton: TButton; 
    AEditA: TEdit; 
    AEditB: TEdit; 
    SEditB: TEdit; 
    PEditB: TEdit; 
    PLabelA: TLabel; 
    SLabelA: TLabel; 
    ALabelA: TLabel; 
    PEditA: TEdit; 
    SEditA: TEdit; 
    ExitButton: TButton; 
    Settings: TLabel; 
    ALabelB: TLabel; 
    SLabelB: TLabel; 
    PLabelB: TLabel; 
    AReserveLabel: TLabel; 
    BReserveLabel: TLabel; 
    Label1: TLabel; 
    Label2: TLabel; 
    Label3: TLabel; 
    Label4: TLabel; 
    Label5: TLabel; 
    Label6: TLabel; 
    Label8: TLabel; 
    Label7: TLabel; 
    procedure SaveButtonClick(Sender: TObject); 
    procedure FormCreate(Sender: TObject); 
    procedure ExitButtonClick(Sender: TObject); 
    procedure AEditAKeyPress(Sender: TObject; var Key: Char); 
    procedure AEditBKeyPress(Sender: TObject; var Key: Char); 
    procedure SEditAKeyPress(Sender: TObject; var Key: Char); 
    procedure SEditBKeyPress(Sender: TObject; var Key: Char); 
    procedure PEditAKeyPress(Sender: TObject; var Key: Char); 
    procedure PEditBKeyPress(Sender: TObject; var Key: Char); 

    private 
    { Private declarations } 
    public 
    { Public declarations } 
    end; 

var 
    Form1: TForm1; 
    IniFile : TIniFile; 
    appINI : TIniFile; 
    APriceA : String; 
    SPriceA : String; 
    PPriceA : String; 
    APriceB : String; 
    SPriceB : String; 
    PPriceB : String; 

implementation 


{$R *.DFM} 

procedure TForm1.SaveButtonClick(Sender: TObject); 
//Save Button 
begin 
    appINI := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini')); 
    APriceA := (AEditA.Text); 
    SPriceA := (SEditA.Text); 
    PPriceA := (PEditA.Text); 
    APriceB := (AEditB.Text); 
    SPriceB := (SEditB.Text); 
    PPriceB := (PEditB.Text); 
    appINI.WriteString('PricesA','Adult',APriceA); 
    appINI.WriteString('PricesA','Student',SPriceA); 
    appINI.WriteString('PricesA','Pensioner',PPriceA); 
    appINI.WriteString('PricesB','Adult',APriceB); 
    appINI.WriteString('PricesB','Student',SPriceB); 
    appINI.WriteString('PricesB','Pensioner',PPriceB); 
    appINI.Free; 
    ShowMessage('Settings Saved Successfully!'); 
end; 

procedure TForm1.FormCreate(Sender: TObject); 
//Displays values as the form is created 
begin 
{ appINI := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini')); 
APriceA := appINI.ReadString('PricesA','Adult',''); 
SPriceA := appINI.ReadString('PricesA','Student',''); 
PPriceA := appINI.ReadString('PricesA','Pensioner',''); 
APriceB := appINI.ReadString('PricesB','Adult',''); 
SPriceB := appINI.ReadString('PricesB','Student',''); 
PPriceB := appINI.ReadString('PricesB','Pensioner',''); 
appINI.Free; 
AEditA.Text := (APriceA); 
SEditA.Text := (SPriceA); 
PEditA.Text := (PPriceA); 
AEditB.Text := (APriceB); 
SEditB.Text := (SPriceB); 
PEditB.Text := (PPriceB);} 
end; 

procedure TForm1.ExitButtonClick(Sender: TObject); 
//Exit Button 
begin 
Close; 
end; 

procedure TForm1.AEditAKeyPress(Sender: TObject; var Key: Char); 
var s:string; 
begin 
    s := ('1234567890.'#8); //Add chars you want to allow 
    if pos(key,s) =0 then begin 
    Key:=#0; 
    showmessage('Only Numbers are allowed. Include cents!'); 
    end; 
end; 

procedure TForm1.AEditBKeyPress(Sender: TObject; var Key: Char); 
var s:string; 
begin 
    s := ('1234567890.'#8); //Add chars you want to allow 
    if pos(key,s) =0 then begin 
    Key:=#0; 
    showmessage('Only Numbers are allowed. Include cents!'); 
    end; 
end; 

procedure TForm1.SEditAKeyPress(Sender: TObject; var Key: Char); 
var s:string; 
begin 
    s := ('1234567890.'#8); //Add chars you want to allow 
    if pos(key,s) =0 then begin 
    Key:=#0; 
    showmessage('Only Numbers are allowed. Include cents!'); 
    end; 
end; 

procedure TForm1.SEditBKeyPress(Sender: TObject; var Key: Char); 
var s:string; 
begin 
    s := ('1234567890.'#8); //Add chars you want to allow 
    if pos(key,s) =0 then begin 
    Key:=#0; 
    showmessage('Only Numbers are allowed. Include cents!'); 
    end; 
end; 

procedure TForm1.PEditAKeyPress(Sender: TObject; var Key: Char); 
var s:string; 
begin 
    s := ('1234567890.'#8); //Add chars you want to allow 
    if pos(key,s) =0 then begin 
    Key:=#0; 
    showmessage('Only Numbers are allowed. Include cents!'); 
    end; 
end; 

procedure TForm1.PEditBKeyPress(Sender: TObject; var Key: Char); 
var s:string; 
begin 
    s := ('1234567890.'#8); //Add chars you want to allow 
    if pos(key,s) =0 then begin 
    Key:=#0; 
    showmessage('Only Numbers are allowed. Include cents!'); 
    end; 
end; 
//End of Settings 
end. 

登錄表單代碼:

unit Unit2; 

interface 

uses 
    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, 
    StdCtrls, Mask, inifiles, Unit1; 

type 
    TForm2 = class(TForm) 
    PassEdit: TMaskEdit; 
    LoginButton: TButton; 
    PassLabel: TLabel; 
    InvisiButton: TButton; 
    procedure PassEditClick(Sender: TObject); 
    procedure LoginButtonClick(Sender: TObject); 
    procedure FormCreate(Sender: TObject); 
    private 
    { Private declarations } 
    public 
    { Public declarations } 
    end; 

var 
    Form2: TForm2; 
    IniFile : TIniFile; 
    appINI : TIniFile; 
    Password : string; 

implementation 



{$R *.DFM} 

procedure TForm2.PassEditClick(Sender: TObject); 
begin 
PassEdit.Text := ''; 
end; 

procedure TForm2.LoginButtonClick(Sender: TObject); 
begin 
//if Password = PassEdit.Text then begin 
Form2.Hide; 
showmessage('test'); 
Form1.Show; 
end; 
//end; 
procedure TForm2.FormCreate(Sender: TObject); 
begin 
appINI := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini')); 
Password := appINI.ReadString('Login','Password',''); 
ShowMessage(Password); 
appINI.Free; 
end; 

end. 

這是項目:

program Project1; 

uses 
    Forms, 
    Unit1 in 'Unit1.pas' {Form1}, 
    Unit2 in 'Unit2.pas' {Form2}; 


{$R *.RES} 

begin 
    Application.Initialize; 
    //Application.CreateForm(TForm1, Form1); 
    Application.CreateForm(TForm2, Form2); 
    Application.Run; 
end. 
+0

你在哪裏創建Form1中? (順便說一下,您可以將相同的事件處理程序分配給多個組件。) – 2012-08-01 00:44:13

+0

實際問題是什麼? – 2012-08-01 00:44:43

+0

當我點擊登錄按鈕時,它會拋出錯誤,有關CPU錯誤或某事和程序崩潰的東西。然後卡在degug模式下,我不得不重置程序。 – 2012-08-01 00:56:10

回答

4

你註釋掉代碼行.dpr file that creates Form1`:

//Application.CreateForm(TForm1, Form1); 

但你在訪問該Unit1形式自存:

procedure TForm2.LoginButtonClick(Sender: TObject); 
begin 
//if Password = PassEdit.Text then begin 
Form2.Hide; 
showmessage('test'); 
Form1.Show;  // <-- Accessing uncreated form here 
end; 

取消註釋所以它被創建在項目文件中的行。請注意,與Application.CreateForm創建第一形式成爲應用程序的主要形式,而當窗體關閉應用程序終止。

您還可以在你的代碼中的另一個重大缺陷。你不應該從它自己的方法之一中引用的形式本身的名字,就像你從TForm2.LoginButtonClick內要做的:

Form2.Hide; 

這意味着,如果你重新命名形式,它不會編譯,如果你創建了多個TForm2,你的代碼將訪問錯誤的代碼或者訪問非創建的表單(比如你現在遇到的問題)時會導致訪問衝突。您應該直接使用表單的方法,如Hide;' from the method, or use Self.Hide;`來引用當前正在運行該方法的實例。 (以備參考:當你遇到問題時,如果你在解決問題的時候解釋了什麼是問題,那麼它會有所幫助,「程序錯誤」沒有關於錯誤的其他信息本身是毫無意義的。鍵入「錯誤」,你應該添加非常接下來的事情就是你有,包括包括任何地址信息的確切錯誤消息確切錯誤。我們不能看到你的屏幕來自我們坐在哪裏,所以只有我們有你提供我們的幫助你去的信息。)

相關問題