vts123
vts123

Reputation: 1776

Disable Alt+F4 in UserControl

i have some user control and i want to disable Alt + F4 oportunity for the end user. When my User Control shows, there is opportunity to close it with Alt + F4, then program goes to base class in the method:

protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
    //Content = null; // Remove child from logical parent - for reusing purposes
    this.RemoveLogicalChild(Content); //this works faster
    base.OnClosing(e);
    { GC.Collect(); };
}

What i must do here or somewhere else, to do disable my user control closing on Alt + F4?

Upvotes: 5

Views: 9395

Answers (5)

greedydita
greedydita

Reputation: 136

On your custom control add the following handlers:

PreviewKeyDown="Window_PreviewKeyDown"
PreviewKeyUp="Window_PreviewKeyUp"

The implementation for the handlers:

bool AltDown = false;
private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.SystemKey == Key.LeftAlt || e.SystemKey == Key.RightAlt)
    {
        AltDown = true;
    }
    else if (e.SystemKey == Key.F4 && AltDown)
    {
        e.Handled = true;
    }            
}
private void Window_PreviewKeyUp(object sender, KeyEventArgs e)
{
    if (e.SystemKey == Key.LeftAlt || e.SystemKey == Key.RightAlt)
    {
        AltDown = false;
    }
}

Upvotes: 5

yozcaliskan
yozcaliskan

Reputation: 31

Another decent method is :


protected override void OnPreviewKeyDown(System.Windows.Input.KeyEventArgs e)
    {

        if (e.KeyboardDevice.Modifiers == ModifierKeys.Alt && e.SystemKey == Key.F4)
        {
            e.Handled = true;
        }
    }

Upvotes: 1

amin
amin

Reputation: 1

The simplest way to block Alt+F4 is to block only F4. So, when F4 doesn't need to be handled, blocking Alt is unnecessary.

Hope that's useful.

Upvotes: 0

Jignesh
Jignesh

Reputation: 136

you can override the OnPreviewkeyDown method and trap ALT and F4 keys.

protected override void OnPreviewKeyDown(System.Windows.Input.KeyEventArgs e)
        {
            if ((Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt)) && Keyboard.IsKeyDown(Key.F4))
                e.Handled = true;
        }

Upvotes: 5

Wonko the Sane
Wonko the Sane

Reputation: 10813

To be sure, I would really question this as a Best Practice. However, if you really want to do this, then you need to prevent the window containing the UserControl from closing.

The easiest way to do this is to set a DependencyProperty on your UserControl that is simply a Boolean that flags whether the container can be closed. You would only set this to true when you want it to actually close (you probably already have a button or something that you are using now to close the control).

public Boolean AllowClose
{
    get { return (Boolean)GetValue(AllowCloseProperty); }
    set { SetValue(AllowCloseProperty, value); }
}

 public static readonly DependencyProperty AllowCloseProperty =
    DependencyProperty.Register("AllowClose", typeof(Boolean), 
    typeof(MyUserControl), new UIPropertyMetadata(false));

Then, in the windows Closing event, you would check for that property to be set to true. If it is not, then you would set e.Cancel = true;

Using your example:

   protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
   {
    if (! myUserControl.AllowClose)
    {
        MessageBox.Show("Even though most Windows allow Alt-F4 to close, I'm not letting you!");
        e.Cancel = true;
    }
    else
    {
        //Content = null; // Remove child from parent - for reuse
        this.RemoveLogicalChild(Content); //this works faster
        base.OnClosing(e);
        { GC.Collect(); };
    }
}

Upvotes: 7

Related Questions