Ahmad Alshaib
Ahmad Alshaib

Reputation: 88

How to lock a windows form in c# to be always be maximized?

Im working on a windows form program and I want to make the main form always maximized ,I've tried setting the WindowState to Maximized and FormBorderStyle to FixedDialog/FixedSingle and it works but the maximize button is still there so I tried setting the MaximizeBox to false but then the form is full screen and it totally covers the taskbar which is the problem ,I don't want it to be over the taskbar. If anyone knows a solution or ever an alternative solution to the problem please feel free to help me out.

Upvotes: 0

Views: 1074

Answers (1)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112342

Keep FormBorderStyle = Sizable. Set MaximizeBox = false and MinimizeBox = false. As code behind use

public partial class frmFixedMaximized : Form
{
    private bool _changing;

    public frmFixedMaximized()
    {
        InitializeComponent();
        WindowState = FormWindowState.Maximized;
    }

    private void frmFixedMaximized_Shown(object sender, EventArgs e)
    {
        // Make resizing impossible.
        MinimumSize = Size;
        MaximumSize = Size;
    }

    private void frmFixedMaximized_LocationChanged(object sender, EventArgs e)
    {
        if (!_changing) {
            _changing = true;
            try {
                // Restore maximized state.
                WindowState = FormWindowState.Minimized;
                WindowState = FormWindowState.Maximized;
            } finally {
                _changing = false;
            }
        }
    }
}

The reason for this code is that a user can still drag a window by holding its title bar. The _changing variable prevents the LocationChanged event handler to trigger itself in an endless loop.

Upvotes: 4

Related Questions