注册 登录
编程论坛 Delphi论坛

delphi 登陆界面后显示主窗体,出现 access violation 错误

a373492883 发布于 2014-11-27 16:57, 4479 次点击
delphi先是登陆窗体,用户名密码正确后显示主窗体,程序如下,目前错误,登陆界面运行后,出现access violation at address错误,求解哪错了。。。哎,憋了我2天了。。那是相当难受
只有本站会员才能查看附件,请 登录

只有本站会员才能查看附件,请 登录

unit Unit20141126_2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, DB, ADODB;

type
  TForm2 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;


implementation

uses Unit20141126_2_2;

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
var
   name,password,sqlstr:string;
begin
        if (length(trim(edit1.Text))>0) or (length(trim(edit2.Text))>0) then
        begin
              name:=trim(edit1.Text); //取得用户输入的用户名
              password:=trim(edit2.Text); //取得用户输入的密码
               sqlstr:='select * from t_user where (username='''+name+''') and(userpass='''+password+''')';
               with Tadoquery.create(nil) do
               try
                     connection:=form1.adoconnection1;
                     close;
                     sql.clear;
                     sql.add(sqlstr);
                     open;
                          if recordcount>0 then
                          begin
                               form2.hide;
                               showmessage('登陆成功');
                               Application.createForm(TForm1,Form1);
                               Form1.ShowModal;

                          end
                          else
                          begin
                               showmessage('用户名密码错误');
                               edit1.SetFocus;
                               edit1.SelectAll;
                          end;

               finally
                       free;
               end;

        end
        else
        begin
               showmessage('请输入用户名密码');
               edit1.SelectAll;
        end;
end;

procedure TForm2.Button2Click(Sender: TObject);
begin
close;
end;

end.
4 回复
#2
volte2014-12-01 15:52
你太牛叉了,代码能写成这样!!!!
#3
a3734928832015-02-25 09:31
好吧我承认我不是变成的材料。。。。
#4
不懂才问2015-02-26 00:59
http://www.

不懂语法的我,也刚开始学习中。。。
#5
AleTiff2015-04-10 15:39
看你代码里有一句: Application.createForm(TForm1,Form1) ,这句是不是意味着,在这之前,你的 Form1 是还没有创建的?

如果你的回答是“Yes”,那么我还看到这句的前面有一句: connection:=form1.adoconnection1 ,Form1 都没创建起来,你就已经在访问它上面的 connect 对象了?

好吧,如果你的回答是“No”,那么你写这句是担心什么呢?

Delphi 中,如果不想一开始就显示 主窗体,可以在 Project 单元的 begin .. end 的某个地方,使用 Application.ShowMainForm = false 来达到目的。这个时候,主窗体其实是已经创建的,只不过被隐藏了,你可以访问这个窗体上的任何全局对象,直到你觉得需要它显示为止。
1