How to prevent electron window closing when click close and ask confirmation #666

Closed
opened 2026-01-29 16:45:34 +00:00 by claunia · 2 comments
Owner

Originally created by @soundararajans on GitHub (May 13, 2021).

Originally assigned to: @GregorBiswanger on GitHub.

How to prevent electron window closing when click close and ask confirmation

  • Node Version: V 14.17.0
  • Electron.Net Nuget: 11.5.1
  • Target: Win
  • .Net: Net5.0

I tried the below code. But it's not working and window is closing always without asking confirmation.

`
window.OnClose += () =>
{
MessageBoxOptions options = new MessageBoxOptions("Changes you made may not be saved");
options.Type = MessageBoxType.question;
options.Buttons = new string[] { "No", "Yes" };
options.DefaultId = 1;
options.CancelId = 0;

            if (Electron.Dialog.ShowMessageBoxAsync(options).Result.Response == 1)
            {
               
            }
        };

`

Originally created by @soundararajans on GitHub (May 13, 2021). Originally assigned to: @GregorBiswanger on GitHub. <!-- Please search existing issues to avoid creating duplicates. --> How to prevent electron window closing when click close and ask confirmation <!-- Which version of Electron.NET CLI and API are you using? --> <!-- Please always try to use latest version before report. --> * **Node Version**: V 14.17.0 * **Electron.Net Nuget**: 11.5.1 <!-- Which version of .NET Core and Node.js are you using (if applicable)? --> <!-- What target are you building for? --> * **Target**: Win * **.Net**: Net5.0 <!-- Enter your issue details below this comment. --> <!-- If you want, you can donate to increase issue priority (https://donorbox.org/electron-net) --> I tried the below code. But it's not working and window is closing always without asking confirmation. ` window.OnClose += () => { MessageBoxOptions options = new MessageBoxOptions("Changes you made may not be saved"); options.Type = MessageBoxType.question; options.Buttons = new string[] { "No", "Yes" }; options.DefaultId = 1; options.CancelId = 0; if (Electron.Dialog.ShowMessageBoxAsync(options).Result.Response == 1) { } }; `
claunia added the bug label 2026-01-29 16:45:34 +00:00
Author
Owner

@NepPure commented on GitHub (Aug 19, 2021):

The same question

@NepPure commented on GitHub (Aug 19, 2021): The same question
Author
Owner

@GregorBiswanger commented on GitHub (Mar 28, 2023):

🎉🚀 New Electron.NET version 23.6.1 released 🚀🎉

With native Electron 23 and .NET 6 support. Your problem should be fixed here. If you continue to have the problem, please let us know. Please note the correct updating of your API & CLI. Info in the README. Have fun!

@GregorBiswanger commented on GitHub (Mar 28, 2023): 🎉🚀 New Electron.NET version 23.6.1 released 🚀🎉 With native Electron 23 and .NET 6 support. Your problem should be fixed here. If you continue to have the problem, please let us know. Please note the correct updating of your API & CLI. Info in the README. Have fun!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/Electron.NET#666