Link to home
Start Free TrialLog in
Avatar of NickRackham
NickRackham

asked on

Beginner - Pop up boxes

I'm learning Delphi 3 as part of a college course. My question is I have an exit button on my form and want to have a "Are you sure you want to Quit" dialog box pop up when the button is clicked, and if Yes is chosen the project closes but if no is clicked then you are returned to the original form. How do I do this? Also - on the same theme. I have a Clear button on my form. When the button is clicked the values or text entered in edit boxes 1 to 4 are cleared. How do I get a dialog box to pop up "Are you sure?" and if yes is chosen then the dialog box closes and the edit boxes are cleared or if no is chosen then the dialog box closes and you are returned to the original form?

Many thanks

Nick

PS. Only just started learning Delphi so I'd appreciate "an idiots guide" for an answer. :o)
Avatar of Motaz
Motaz

Exit button:

write this code in Button's OnClick event:

if MessageDlg('Are you sure', mtConfirmation, [mbYes, mbNo], 0) = idYes then Close;

Motaz
Clear button:

Write:

if MessageDlg('Are you sure', mtConfirmation, [mbYes, mbNo], 0) = idYes then
begin
  Edit1.Clear;
  Edit2.Clear;
  Edit3.Clear;
  Edit4.Clear;
end;

Good luck

I also have an Electronic Book for learning Delphi
www.gocities.com/motaz1

By the way in which university are you now and in which year.
Motaz
Avatar of NickRackham

ASKER

Motaz,

Thanks for the quick response. The exit code works fine but the clear button ????

If No ischosen then edit 2 - 4 are cleared but edit 1 isn't. If Yes is chosen then Edit 1 - 4 are cleared. Very strange. Can you help further?

Nick

PS I'm at Suffolk College doing an HNC in Computing - First Year. I'll have a look at your web site later when I've got more time. :o)
ASKER CERTIFIED SOLUTION
Avatar of Motaz
Motaz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Motaz,

Excellent - Many thanks.

No doubt they'll be more questions posted on EE before I've finished :o)

Nick
Motaz, Just one more thing - How do I link the Close button on my form to bring up the same dialog box as when the Exit button is clicked. I've just noticed that you can close the form with out an "Are you sure" dialog box appearing if you close the form with the close  X  button on the top right of the form.

Nick
Motaz, Don't worry, I've found it

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);

begin
  if MessageDlg('Close application ?', mtConfirmation,
    [mbYes, mbNo], 0) = mrYes then
    Action := caFree
  else
    Action := caNone;
end;

Many thanks

Nick

PS - Perhaps I should have looked at the Help first!!
Good luck