How do I create a button that doesn't close a dialog?

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
bossydog9
Junior Coder
Posts: 29
Joined: Wed Jan 08, 2020 12:46 pm

How do I create a button that doesn't close a dialog?

Post by bossydog9 » Sun Feb 02, 2020 3:13 am

I want a button in a dialog that performs a subroutine and nothing else. I want the dialog to stay open. I have added an AddDialogHandler statement that performs the subroutine, but the dialog closes. How do I stop it?
Last edited by bossydog9 on Sun Feb 02, 2020 5:19 pm, edited 1 time in total.

User avatar
JRL
Automation Wizard
Posts: 3392
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: How do I create a button that doesn't close a dialog

Post by JRL » Sun Feb 02, 2020 6:38 am

Edit: Fixed the Dialog caption so the script will run.

Code: Select all

Dialog>Dialog1
object Dialog1: TForm
  Caption = 'Button Won''t Close'
  ClientHeight = 269
  ClientWidth = 429
  object MSButton1: tMSButton
    Left = 170
    Top = 170
    Width = 75
    Height = 25
    Caption = 'Press Me'
  end
end
EndDialog>Dialog1

AddDialogHandler>Dialog1,MSButton1,OnClick,DoIt
AddDialogHandler>Dialog1,,OnClose,Quit

Show>Dialog1,

SRT>DoIt
  MDL>Would have been nice to have seen your script but did you not add a comma to the Show>Dialog line
END>DoIt

SRT>Quit
  Exit
END>Quit

bossydog9
Junior Coder
Posts: 29
Joined: Wed Jan 08, 2020 12:46 pm

Re: How do I create a button that doesn't close a dialog?

Post by bossydog9 » Mon Feb 03, 2020 12:13 pm

Your script doesn't load because of the single apostrophe in the caption property (Won't should have 2 apostrophes - Won''t).

It turns out that in order for a button to perform a subroutine and not close the form, its ModalResult must be zero.
Note that if a button's ModalResult value is zero (the default) it will not close the dialog. To respond to button clicks for buttons that should not close the dialog, leave ModalResult for those buttons to zero and create an event handler with the AddDialogHandler command.

User avatar
JRL
Automation Wizard
Posts: 3392
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: How do I create a button that doesn't close a dialog?

Post by JRL » Mon Feb 03, 2020 5:31 pm

Fixed the dialog. Sorry for any confusion.

You are correct that the modalresult property must be set to zero. Its been so long since I've used the pre-version 12 dialog format that I'd forgotten about that. It is important to note that you will never see the modal result until the dialog closes. If you are using dialog event handlers, I can only think of one reason to ever set a button's modalresult property (the default is zero), that would be to have the dialog close when the button is picked.

If you do want to use modalresult and have the dialog stay open, just add another Show>Dialog at an appropriate location in the script. Or maybe add a loop.

Code: Select all

Dialog>Dialog1
object Dialog1: TForm
  Caption = 'Button Won''t Close'
  ClientHeight = 269
  ClientWidth = 429
  object MSButton1: tMSButton
    Left = 170
    Top = 170
    Width = 75
    Height = 25
    Caption = 'Press Me'
    ModalResult = 99
  end
end
EndDialog>Dialog1

AddDialogHandler>Dialog1,MSButton1,OnClick,DoIt
//AddDialogHandler>Dialog1,,OnClose,Quit

Let>Count=0

Label>RestartDialog1
If>Buttonresult={"Buttonresult"}
  Show>Dialog1,Buttonresult
Else
  MDL>Buttonresult = %Buttonresult%%crlf%Count = %Count%
  If>Buttonresult=2
    Exit
  EndIf
  Show>Dialog1,Buttonresult
EndIf
Goto>RestartDialog1

SRT>DoIt
  //Do Button Stuff
  Add>Count,1
END>DoIt

/*
SRT>Quit
  Exit
END>Quit
*/

nodochau
Pro Scripter
Posts: 111
Joined: Wed Jan 16, 2019 12:59 pm

Re: How do I create a button that doesn't close a dialog?

Post by nodochau » Tue Feb 04, 2020 11:30 am

bossydog9 wrote:
Sun Feb 02, 2020 3:13 am
I want a button in a dialog that performs a subroutine and nothing else. I want the dialog to stay open. I have added an AddDialogHandler statement that performs the subroutine, but the dialog closes. How do I stop it?
If you have a button in your dialog then just set its ModalResult = 0.

bossydog9
Junior Coder
Posts: 29
Joined: Wed Jan 08, 2020 12:46 pm

Re: How do I create a button that doesn't close a dialog?

Post by bossydog9 » Tue Feb 04, 2020 12:15 pm

Thanks, all. :)

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts