Handling Click for showYesNoButton

I have a window which when tried to close pops up a message, to sure the user wants to click

auto result = juce::NativeMessageBox::showYesNoBox(
                    juce::MessageBoxIconType::WarningIcon,
                    "Quit",
                    "Are you sure you want to Exit?",
                    this,
                    nullptr
                    );

When I try to handle the click using following technique nothing happens.

if (!result)
             JUCEApplication::getInstance()->systemRequestedQuit();

How to handle this type of stuff. I am preety new to JUCE

NativeMessageBox::showYesNoBox returns an int (1 for yes, 0 for no). In your case yes would mean quit. So you should do like this:

if (result == 1)
    JUCEApplication::getInstance()->systemRequestedQuit();

Right now it’s probably working the opposite way of what you want…

Also, where have you put this code? You might wanna consider putting this in an override of systemRequestedQuit , as explained here: :slight_smile:
https://docs.juce.com/develop/classJUCEApplicationBase.html#aa4ae0dcc3467f4927d8365b4814b1396

1 Like

No it doesnot work, there is no effect at all after the button is clicked. I have following code.

void closeButtonPressed() override
        {
            // This is called when the user tries to close this window. Here, we'll just
            // ask the app to quit when this happens, but you can change this to do
            // whatever you need.


            auto result = juce::NativeMessageBox::showYesNoBox(
                    juce::MessageBoxIconType::WarningIcon,
                    "Quit",
                    "Are you sure you want to Exit?",
                    nullptr,
                    nullptr
                    );

            if (result)
             JUCEApplication::getInstance()->systemRequestedQuit();

        }

Ah, I see. I just tried it out. It won’t work this way unless you add JUCE_MODAL_LOOPS_PERMITTED=1 to your preprocessor definitions. Check out this thread for explanation! showModalDialog error

If I were you I’d probably go the async way. Could be like this:

void closeButtonPressed() override
{
    // This is called when the user tries to close this window. Here, we'll just
    // ask the app to quit when this happens, but you can change this to do
    // whatever you need.
    juce::NativeMessageBox::showAsync (juce::MessageBoxOptions()
        .withIconType (juce::MessageBoxIconType::WarningIcon)
        .withTitle ("Quit")
        .withMessage ("Are you sure you want to exit?")
        .withButton ("Yes")
        .withButton ("No"),
        [this] (int result) { if (result == 0) JUCEApplication::getInstance()->systemRequestedQuit(); });
}