Reputation: 908
I am trying to use Close (Cross) Button as Minimize button. It is exactly like what we see in Skype.
I know we can simply disable the Close button by setting ControlBox property to false or we can disable by creating params. But I want that the Close button should be visible, enabled and working, just the work should be to minimize!
Please help!
Thanks in advance!
Upvotes: 0
Views: 5514
Reputation: 941505
That's what the FormClosing event helps you do. It is very important that you use it responsibly and don't prevent the user from shutting down his machine. Or get nagged that your program is not playing along, the more typical outcome these days. You have to pay attention to the reason the window is getting closed. Thus:
Protected Overrides Sub OnFormClosing(e As FormClosingEventArgs)
If e.CloseReason = CloseReason.UserClosing Then
Me.WindowState = FormWindowState.Minimized
e.Cancel = True
End If
MyBase.OnFormClosing(e)
End Sub
Upvotes: 7
Reputation: 35270
If this is WinForms, you can handle the FormClosing
event to accomplish this and set e.Cancel
to true so the form does not actually get closed after you minimize it:
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Me.WindowState = FormWindowState.Minimized
e.Cancel = True
End Sub
And similarly with WPF using the Closing
event:
Private Sub Window_Closing(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
Me.WindowState = Windows.WindowState.Minimized
e.Cancel = True
End Sub
Upvotes: 5