-3

I have setup my login procedure with an available form (Form4) and a MainForm. On the available form (form4) I have:

var
  Form4: TForm4;
  procedure Login;
implementation
.....

The 'Login' procedure goes:

procedure Login;
begin
  with TForm4.Create(nil) do
  try
    Application.MainForm.Hide;
    if ShowModal = mrOK then
      Application.MainForm.Show
    else
      Application.Terminate;
  finally
    Free;
  end;
end;

Then on the same form I have a button to log in :

procedure TForm4.AdvGlowButton1Click(Sender: TObject);  //the buton's property is ModalResult=mrOK
begin
DataModule2.LOGIN_QUERY.Active:=false;
DataModule2.LOGIN_QUERY.SQL.Clear;
DataModule2.LOGIN_QUERY.SQL.Add('select user,passsword from users where user='+QuotedStr(cxlookupcombobox1.text)+' and password='+QuotedStr(cxTextEdit1.Text));
DataModule2.LOGIN_QUERY.Open;
if DataModule2.LOGIN_QUERY.FieldByName('password').AsString<>''
then   ModalResult := mrOK  else
ModalResult := mrNone;
end;

The project source goes like this :

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := False;
  Application.CreateForm(TDataModule2, DataModule2);
  Application.CreateForm(TMainForm, MainForm);
  Application.CreateForm(TForm7, Form7);
  Application.CreateForm(TARCHIVE, ARHCIVE);
  Application.CreateForm(TForm10, Form10);
  Application.CreateForm(TForm1, Form1);
  Application.CreateForm(TForm6, Form6);
  Application.CreateForm(TForm5, Form5);
  Application.CreateForm(TForm9, Form9);
  Application.CreateForm(TForm12, Form12);
  Application.CreateForm(TForm12, Form12);
  Application.CreateForm(TAboutBox, AboutBox);
  Login;
  Application.Run;
end.

Yet, every now and then when clicking the Login button on Form4 the application terminates without no reason. Why is this happening ? Should

Application.MainFormOnTaskbar := False;

be set to true perhaps?

Edit:

I edited the project file and the form4 on create event :

procedure TForm4.FormCreate(Sender: TObject);
begin
AdvGlowButton1.ModalResult := mrOK;
end;

and changed the project source :

{$R *.res}
 var
  MainForm: TMainForm;

begin
  Application.Initialize;
  Application.CreateForm(TMainForm, MainForm);
  Application.MainFormOnTaskbar := False;
  Application.CreateForm(TDataModule2, DataModule2);
  Application.CreateForm(TForm7, Form7);
  Application.CreateForm(TARCHIVE, ARCHIVE);
  Application.CreateForm(TForm10, Form10);
  Application.CreateForm(TForm1, Form1);
  Application.CreateForm(TForm6, Form6);
  Application.CreateForm(TForm5, Form5);
  Application.CreateForm(TForm9, Form9);
  Application.CreateForm(TForm12, Form12);
  Application.CreateForm(TForm12, Form12);
  Application.CreateForm(TAboutBox, AboutBox);
  Login;
  Application.Run;
end.

and I dont seem to be getting application closing....(it still does,ughh...)

edit2:

Tried this way. I set my AdvGlowButton1 to ModalResult=mrNone and the Form style to fsdialog:

procedure TForm4.AdvGlowButton1Click(Sender: TObject);  //the buton's property is ModalResult=mrOK
begin
DataModule2.LOGIN_QUERY.Active:=false;
DataModule2.LOGIN_QUERY.SQL.Clear;
DataModule2.LOGIN_QUERY.SQL.Add('select user,passsword from users where user='+QuotedStr(cxlookupcombobox1.text)+' and password='+QuotedStr(cxTextEdit1.Text));
DataModule2.LOGIN_QUERY.Open;
if DataModule2.LOGIN_QUERY.FieldByName('password').AsString<>''
then   ModalResult := mrOK  else
dxStatusBar1.Panels[1].Text :='Wrong password !';
end;

this works most of the times and yet it sometimes closes when I start the application and hit the AdvGlowButton1 button (login button) . Another thing I figured out is missing, how do you prompt for the closure of the application on this login form as it expects only modal results?

user763539
  • 3,509
  • 6
  • 44
  • 103
  • 2
    I'd hazard a guess that ShowModal is not mrOk. – Sertac Akyuz Feb 11 '15 at 04:55
  • Pretty hard to see past that `Application.Terminate`. I also wonder why you don't give your forms names. And why you create them all at startup. I guess the ide did that for you. But it is a bad guide. It thinks you are a VB programmer from the late 20th century. – David Heffernan Feb 11 '15 at 07:23
  • 2
    BTW After this is all fixed up, your next assignment (before you do anything else in your application) is to get rid of the plain text passwords in your database, and replace them with (SHA1) hashes. This is internet/database security rule #1 and **you can't start doing that soon enough**. – Jan Doggen Feb 11 '15 at 08:52
  • Buttons property is set : ModalResult=mrOK – user763539 Feb 11 '15 at 12:28
  • Totally agree on password hashing, but don't forget the blatant SQL injection too! – whosrdaddy Feb 11 '15 at 12:42
  • I dont think sql injection works with parameters..... – user763539 Feb 11 '15 at 12:44
  • It will also terminate, for example, if the user clicks the red "X" or pressed Alt+F4 (since that will exit the form with - I think - mrCancel being returned through ShowModal). – HeartWare Feb 11 '15 at 14:09
  • @user763539: that's the point, you are not using parameters?? – whosrdaddy Feb 11 '15 at 14:11

2 Answers2

2

Like @SertacAkyuz is hinting, one option would be that ShowModal does not return mrOK. Check the ModalResult value for this button and/or the event handler for the OnClick of this button to see if mrOK is the ModalResult that is returned in these cases...

If you click a button, then the OnClick event is fired and if the ModalResult of that button is set to anything, the Form's ModalResult will be set to that value. When an event (like an OnClick event) ends, the form checks its ModalResult value, and if it is set to anything other that 0 (zero), then the form closes and the value is returned as the result of the ShowModal function.

So from what information you have given, this seems like a likely scenario - the Form's ModalResult is set to some value that isn't mrOK. The form then closes, and your IF ShowModal test then terminates the application (since it didn't return mrOK).

HeartWare
  • 7,464
  • 2
  • 26
  • 30
  • So I should try setting on create of the form with : ModalResult := mrOK;? – user763539 Feb 11 '15 at 12:24
  • Only if you want it to immediately close with ShowModal returning mrOK. – HeartWare Feb 11 '15 at 14:06
  • so how do I fix this ? – user763539 Feb 11 '15 at 22:15
  • First of all, you figure out what exactly is the return value of the ShowModal function (save it in a local variable and show it either in the Debug Output or using ShowMessage before Application.Terminate). Then you figure out how it comes to return that specific value. – HeartWare Feb 12 '15 at 06:14
  • It seems to me that hitting the button changes the forms modal result. So theres no point in declaring it a modal form oncreate since the button always changes that. – user763539 Feb 12 '15 at 17:11
  • If hitting (clicking) the button changes the form's ModalResult, then it is because the button's ModalResult value is set to something other than mrNone ( = zero). Read 2nd paragraph of the answer... If you don't want a click on a button to ALWAYS end (close) a modal form, then don't set the button's ModalResult property. – HeartWare Apr 04 '15 at 09:00
1

Based on your code entering wrong password would also result in termination of your application becouse in that case the modal result returned would be mrNone and you only expecting mrOK to continue with your application.

So I recomend next changes:

First remove the modal result propery of your button. button modal result propery is mostly used only as a way to forward the information as of clicking on which specific button lead to closure of modal form.

Then change your buttons event code so that it only sets form modal result if the code is correct else it should show a message that the entered passowrd is incorect. Something like so:

procedure TForm4.AdvGlowButton1Click(Sender: TObject);  //the buton's property is ModalResult=mrOK
begin
  DataModule2.LOGIN_QUERY.Active:=false;
  DataModule2.LOGIN_QUERY.SQL.Clear;
  DataModule2.LOGIN_QUERY.SQL.Add('select user,passsword from users where user='+QuotedStr(cxlookupcombobox1.text)+' and password='+QuotedStr(cxTextEdit1.Text));
  DataModule2.LOGIN_QUERY.Open;
  if DataModule2.LOGIN_QUERY.FieldByName('password').AsString<>'' then
    //Close the modal form with returning of mrOK as modal result
    ModalResult := mrOK  
  else
  begin
    //No modal result should be set here or it would lead to closure of login form
    MessageDlg('Entered password is incorect!',mtError, mbOKCancel, 0);
  end;
end;

This is direct solution for your specific problem. But I would recomend you seriously reconsider your design of the whole login system. Why?

In you current design you create all the forms at the start of your application. Now while you do start with your main form hidden this still doesen't mean that your user can't access it.

Using a special software user could find a handle to your main form window and show it without going through your login process at all.

So the correct approach to avoid this would be to create the login form first and then only on sucsessfull login create the rest of your forms. But that would mean that your login form would actually become the main form of your application so you should take great care of not closing it as it would lead to the closure of your whole application.

You can check the example of how to implement such approach in my answer to another question here:

Delphi Change main form while application is running

Community
  • 1
  • 1
SilverWarior
  • 7,372
  • 2
  • 16
  • 22
  • How can a wrong passport terminate the application if the button returns mrNone? – user763539 Feb 11 '15 at 12:37
  • My forms style (form4) is set to bsdialog. Is not a dialog form mrOK by default ? – user763539 Feb 11 '15 at 12:50
  • @user763539 That is becouse in your Login method you are checking the Modal result in a way that if it is mrOK you show the main form, but in all other cases (which also includes mrNone) statment you call Application.Terminate. – SilverWarior Feb 11 '15 at 13:49
  • I have tried your suggestion from above and still the application sometimes terminates when I hit the login button. – user763539 Feb 11 '15 at 22:04
  • Can you please edit the question to include the latest code so that I see what changes you have made so far and spot posible problem. – SilverWarior Feb 11 '15 at 23:04
  • I have made my LoginButton ModalResult =mrNone and added the message you suggested (removed ModalResult := mrNone;).Other code rests the same... Still the same result.... – user763539 Feb 11 '15 at 23:10
  • any news on this ? Anybody? – user763539 Mar 22 '15 at 12:53
  • if I close the Login Form (without entering anything), Eureka Log gives me "LOGIN_QUERY: Field 'USER' not found." exception ??? – user763539 Mar 22 '15 at 13:09
  • Can you please update the question with your latest code so that I can see what have you done so far and try tracking the problem. – SilverWarior Mar 22 '15 at 13:24