源码如下:
procedure TForm2.Button1Click(Sender: TObject);
var i:integer;
begin
i:=0;
if (edit1.Text='') or (edit2.Text='') then
showmessage('用户名或密码不能为空')
else
begin
while i<=adotable1.RecordCount do
begin
if (adotable1.FieldByName('id').AsString=edit1.Text) and (adotable1.FieldByName('password').AsString=edit2.Text) then
begin
edit2.Text:='';
CurrentForm:=TForm1.Create(nil);
CurrentForm.ShowModal;
exit;
end
else
adotable1.Next;
i:=i+1;
end;
if i>adotable1.RecordCount then
showmessage('用户名或密码错误');
form1.Close;
end;
end;1 用正确的用户名和密码登陆后出现:List Index out of bounds(1)
是怎么回事?在程序中如何修改?
2 用正确的用户名和密码登陆后管理界面form1可操作,登陆框(Form2),无法关闭也无法操作,为什么呢?可否改改?
谢谢了
procedure TForm2.Button1Click(Sender: TObject);
var i:integer;
begin
i:=0;
if (edit1.Text='') or (edit2.Text='') then
showmessage('用户名或密码不能为空')
else
begin
while i<=adotable1.RecordCount do
begin
if (adotable1.FieldByName('id').AsString=edit1.Text) and (adotable1.FieldByName('password').AsString=edit2.Text) then
begin
edit2.Text:='';
CurrentForm:=TForm1.Create(nil);
CurrentForm.ShowModal;
exit;
end
else
adotable1.Next;
i:=i+1;
end;
if i>adotable1.RecordCount then
showmessage('用户名或密码错误');
form1.Close;
end;
end;1 用正确的用户名和密码登陆后出现:List Index out of bounds(1)
是怎么回事?在程序中如何修改?
2 用正确的用户名和密码登陆后管理界面form1可操作,登陆框(Form2),无法关闭也无法操作,为什么呢?可否改改?
谢谢了
改成if i>adotable1.RecordCount+1 then
看看这个提示在我这里出现都是因为循环太小或太大。
form1.close改成form2.close;
当i=adotable1.RecordCount 时,也就是查询到最后一条时,并且if条件不满足,那么就得执行else语句,即adotable1.Next;最后一条还NEXT肯定要出错了。
解决方法:
将你的for改为while not adotable1.eof do就可以了,而且还省一个变量i,还不用人工控制i加1。2.对这种登录可以这样:
将你的主窗体设为默认窗体,然后在改窗体的onshow中写f_login.showmodal();即可。
这样在主窗体显示之前登录先出来,登录通过才显示主窗体。
begin
if (adotable1.FieldByName('id').AsString=edit1.Text) and (adotable1.FieldByName('password').AsString=edit2.Text) then
begin
edit2.Text:='';
CurrentForm:=TForm1.Create(nil);
CurrentForm.ShowModal;
exit;
end
else
adotable1.Next;
i:=i+1;
end;
改成:
adotable1.first;
while i<adotable1.RecordCount do
begin
if (adotable1.FieldByName('id').AsString=edit1.Text) and (adotable1.FieldByName('password').AsString=edit2.Text) then
begin
edit2.Text:='';
CurrentForm:=TForm1.Create(nil);
CurrentForm.ShowModal;
exit;
end
else
adotable1.Next;
i:=i+1;
end;
而且更奇怪的是,这个问题因机器而异,如何解决呢?