How to get that the main form is going to be minimized

Hi,

Is there any way (like an event) to get that user is pressing MinimizeBox

I totally want to know how to get that the user wants to minimize the main from.

In this case i want to send my applicatin to notification tray when user minimizes application's main from.

With thanks.




Answer this question

How to get that the main form is going to be minimized

  • Ggreg

    Hi, and thanx, But i want to be notified when my form is going to be minimized. I hove no problem with tray icon.

  • Rick Penner

    Try using the SizeChanged event, as shown in the above code...


  • Dave Ehrlich

    Yes, it's ok, thank you.

  • Amir.S

    Here's an example in VB.NET:

    Public Class Form1
    Dim WithEvents ntfy As New NotifyIcon

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ntfy.Icon = My.Resources.Icon1
    End Sub

    Private Sub Form1_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged
    Me.ShowInTaskbar = Me.WindowState <> FormWindowState.Minimized
    'Me.Visible = Me.WindowState <> FormWindowState.Minimized
    ntfy.Visible = Me.WindowState = FormWindowState.Minimized
    End Sub

    Private Sub ntfy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ntfy.Click
    Me.WindowState = FormWindowState.Normal
    'Form1_SizeChanged(sender, e)
    End Sub
    End Class

    The way it is coded works pretty well, except that the form is still visible in the Alt+Tab sequence. To hide it from Alt+Tab, remove the comment quotes for the code. The problem then is that the form doesn't get the focus when the tray icon is clicked. To fix that, you'd need to call SetForegroundWindow; please yell if you need to code for that...


  • How to get that the main form is going to be minimized