软件登录的代码,通常不是写在 OnButtonClick 这种事件中,而是写在窗体 OnCreate/Show 代码中,如果是多个窗体,一般会在工程文件(*.dpr)中,编写代码来完成。
示例代码如下:
工程文件代码:
program KBSuses
Forms,
Windows,
MainF in 'MainF.pas' {frmMain},
mainDM in 'mainDM.pas' {dmMain: TDataModule},
LoginF in 'LoginF.pas' {frmLogin}
{$R *.RES}
var
frmMain: TfrmMain
dmMain: TdmMain
begin
Application.Initialize
Application.CreateForm(TdmMain, dmMain)
Application.CreateForm(TfrmMain, frmMain)
if LoginF.Login() then
Application.Run
end.
登录验证窗体:
unit LoginFinterface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls, Buttons
type
TfrmLogin = class(TForm)
pnl1: TPanel
edUserName: TEdit
lblUserName: TLabel
edPasswd: TEdit
lblPasswd: TLabel
btnOK: TBitBtn
btnCancel: TBitBtn
procedure FormCreate(Sender: TObject)
procedure btnOKClick(Sender: TObject)
private
FTryTimes: Integer
function CheckUserInfo(AUserName, APassword: string): Boolean
public
end
function Login: Boolean //登录函数
implementation
{$R *.DFM}
function Login: Boolean
begin
//动态创建登录窗口
with TfrmLogin.Create(nil) do
begin
//只有返回OK的时候认为登录成功
Result := ShowModal() = mrOk
Free
end
end
function TfrmLogin.CheckUserInfo(AUserName, APassword: string): Boolean
begin
//简单的验证,可以修改为其它的方式
Result := (AUserName = 'lihd' ) and (APassword = 'P@ssw0rd')
end
procedure TfrmLogin.FormCreate(Sender: TObject)
begin
//尝试登录的次数,初始化为0
FTryTimes := 0
end
procedure TfrmLogin.btnOKClick(Sender: TObject)
begin
Inc(FTryTimes) //尝试登录的次数 + 1
if CheckUserInfo(edUserName.Text, edPasswd.Text) then
begin
//如果通过检查,返回OK
Self.ModalResult := mrOk
end else
begin
if (FTryTimes > 2) then
begin
MessageBox(self.Handle, '你已经尝试登录3次,请联系系统管理员。', '错误', MB_OK + MB_ICONERROR)
Self.ModalResult := mrCancel
Exit
end
//通不过,报错
MessageBox(self.Handle, '错误的用户名或密码,请重新输入!', '错误', MB_OK + MB_ICONERROR)
edUserName.SetFocus()
edUserName.SelectAll()
end
end
end.
query1.ParamByName('name').Value :=edit1.Textquery1.SQL.Add('select passwd from clients where name=:name')
把这两句位置换一下吧,改为
query1.SQL.Add('select passwd from clients where name=:name')
query1.ParamByName('name').Value := edit1.Text
或者改为
query1.SQL.Text := Format('select passwd from clients where name = %s', [QuotedStr(Edit1.Text)])
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)