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

Open
opened 2026-01-29 16:45:29 +00:00 by claunia · 0 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:29 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/Electron.NET#664