Disable Alt + F4 in UserControl

I have a user control and I want to disable the Alt + F4 feature for the end user. When my user control shows, it is possible to close it with Alt + F4 , then the program goes to the 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 should I do here or somewhere else to disable my user control on Alt + F4 ?

+4
source share
4 answers

Of course, I would question this as a best practice. However, if you really want to do this, you need not to close the window containing the UserControl .

The easiest way to do this is to set DependencyProperty to UserControl , which is just a Boolean that indicates 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 must set this property to true . If this is not the case, you should 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(); }; } } 
+6
source

In your user control, add the following handlers:
PreviewKeyDown = "Window_PreviewKeyDown" PreviewKeyUp = "Window_PreviewKeyUp"

Implementation for 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; } } 
+5
source

you can override the OnPreviewkeyDown method and intercept the 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; } 
+3
source

The easiest way to block Alt + F4 is to block only F4 . Therefore, when F4 does not need to be processed, an Alt lock is not needed.

Hope this is helpful.

0
source

Source: https://habr.com/ru/post/1316624/


All Articles