2012-08-01 9 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을 만드십니까? (BTW, 여러 구성 요소에 동일한 이벤트 처리기를 할당 할 수 있습니다.) –

+0

실제 문제는 무엇입니까? –

+0

로그인 버튼을 클릭하면 오류가 발생하고 CPU 오류 또는 무언가에 대해 프로그램이 중단됩니다. 그것은 degug 모드에서 붙어서 나는 프로그램을 다시 설정해야합니다. –

답변

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;와 같이 사용해야합니다.

(나중에 참조 할 사항 : 문제가있을 때 문제 해결에 도움을 요청할 때 문제가 무엇인지 설명하면 도움이됩니다. 오류에 대한 다른 정보가없는 "프로그램 오류"는 의미가 없습니다. "오류"라고 입력하면 바로 다음에 추가 할 주소는 입니다. 정확한 주소 정보가 포함 된 오류 메시지가 포함되어 있습니다. 우리는 앉아있는 곳에서 화면을 볼 수 없으므로