How do I prevent a form from being resized by the user?
vb.netWinformsvb.net Problem Overview
I have a form that needs to be maximized in VB.net. I don't want the user to be able to change its size or move it around. How can I do this?
vb.net Solutions
Solution 1 - vb.net
Set the highlighted properties. Set MaximimSize and MinimizeSize properties the same size
Solution 2 - vb.net
To prevent users from resizing, set the FormBoderStyle to Fixed3D or FixedDialog from properties window or from code
frmYour.BorderStyle = System.WinForms.FormBorderStyle.Fixed3D
And set the WindowState property to Maximized, set the MaximizeBox and MinimizeBox properties to false.
To prevent the user from moving around, override WndProc
Protected Overrides Sub WndProc(ByRef m As Message)
Const WM_NCLBUTTONDOWN As Integer = 161
Const WM_SYSCOMMAND As Integer = 274
Const HTCAPTION As Integer = 2
Const SC_MOVE As Integer = 61456
If (m.Msg = WM_SYSCOMMAND) And (m.WParam.ToInt32() = SC_MOVE) Then
Return
End If
If (m.Msg = WM_NCLBUTTONDOWN) And (m.WParam.ToInt32() = HTCAPTION) Then
Return
End If
MyBase.WndProc(m)
End Sub
Solution 3 - vb.net
//Set fixed border
yourForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D
//Set the state of your form to maximized
yourForm.WindowState = FormWindowState.Maximized
//Disable the minimize box and the maximize box
yourForm.MinimizeBox = False
yourForm.MaximizeBox = False
Solution 4 - vb.net
Set the window start style as maximized. Then, hide the minimize and maximize buttons.
Solution 5 - vb.net
You can remove the UI to control this with:
frmYour.MinimizeBox = False
frmYour.MaximizeBox = False
Solution 6 - vb.net
Add some code to the Form Load event:
me.maximumsize = new size(Width, Height)
me.minimumsize = me.maximumsize
me.maximizebox = false
me.minimizebox = false
Example: For a Form height and width of 50 pixels each:
me.maximumsize = new size(50, 50)
me.minimumsize = me.maximumsize
me.maximizebox = false
me.minimizebox = false
Note that setting maximumsize
and minimumsize
to the same size as shown here prevents resizing the Form.
Solution 7 - vb.net
Solution 8 - vb.net
If you want to prevent resize by dragging sizegrips and by the maximize button and by maximize by doubleclick on the header text, than insert the following code in the load event of the form:
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle ' Prevent size grips
Me.MaximumSize = Me.Size ' Prevent maximize (also by doubleclick of header text)
Of course all choices of a formborderstyle beginning with Fixed will do.
Solution 9 - vb.net
Set the min and max size of form to same numbers. Do not show min and max buttons.
Solution 10 - vb.net
Just change these settings in the Solution Explorer.
MaximizeBox = False
MinimizeBox = False
The other things such as ControlBox, Locked, and FormBorderStyle are extra.
Solution 11 - vb.net
There is an option in vb.net that lets you do all this.
The user wont be able to re-size the form or move it around, although there are other ways, this I think is the best.