Search code examples
vb.nettaskbarmaximize-window

Maximize form but do not cover the taskbar


When I click the button to maximize my form, it covers the entire screen including the taskbar. I managed to find a solution and it works, I used my code in form load event but I cannot return the form into normal state.

Private Sub frmDashboard_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.Top = Screen.PrimaryScreen.WorkingArea.Top
    Me.Left = Screen.PrimaryScreen.WorkingArea.Left
    Me.Height = Screen.PrimaryScreen.WorkingArea.Height
    Me.Width = Screen.PrimaryScreen.WorkingArea.Width        
End Sub

Private Sub btnMaximizeMin_Click(sender As Object, e As EventArgs) Handles btnMaxMin.Click
    If Me.WindowState = FormWindowState.Normal Then
        'maximize but dont cover taskbar
        Me.Top = Screen.PrimaryScreen.WorkingArea.Top
        Me.Left = Screen.PrimaryScreen.WorkingArea.Left
        Me.Height = Screen.PrimaryScreen.WorkingArea.Height
        Me.Width = Screen.PrimaryScreen.WorkingArea.Width
    Else
        Me.WindowState = FormWindowState.Normal
    End If
End Sub

Solution

  • The problem is that you aren't maximising the form. You specifically DON'T want to maximise the form because that covers the Windows Task Bar. You can't set the WindowState "back" to Normal because it's already in that state, because it never leaves that state. It's up to you to remember the state for yourself and also the previous bounds, e.g.

    Private isMaximised As Boolean = False
    Private normalBounds As Rectangle
    
    Private Sub MaximiseOrRestore()
        isMaximised = Not isMaximised
    
        If isMaximised Then
            normalBounds = Bounds
            Bounds = Screen.PrimaryScreen.WorkingArea
        Else
            Bounds = normalBounds
        End If
    End Sub