Nyalotha
Nyalotha

Reputation: 419

How to disable ALT+F4 on vb wpf Window

I have a custom window that displays on errors or shows messages, depending on the context. How can I disable ALT+F4 to prevent a user from closing it, without using events.

The window needs to be closed only with a button that resides on it. I do not want users to be able to close it using ALT+F4. The main application can be closed using ALT+F4 and that is ok, but this Window should not be closed under any circumstances with ALT+F4.

Currently I have removed the window from being displayed in the taskbar.

Upvotes: 1

Views: 1619

Answers (1)

Naresh Ravlani
Naresh Ravlani

Reputation: 1620

Use this XAML to catch Alt + F4.

    <Window.InputBindings>
        <KeyBinding Modifiers="Alt" Key="F4" Command="{Binding Path=ToDelegateCommandThatExecuteNothing}" />
    </Window.InputBindings>

Create a RelayCommand in ViewModel like this :

public RelayCommand ToDelegateCommandThatExecuteNothing { get; set; }

Add method against ToDelegateCommandThatExecuteNothing and leave it blank or add whatever action you want to perform on Alt + F4:

ToDelegateCommandThatExecuteNothing = new RelayCommand(o => DoNothing());

public void DoNothing()
    {

    }

My code sample is in C#, you can check it for VB.Net

Upvotes: 2

Related Questions