Prevent resizing controls during design time?

georgemelick

Member
Joined
Apr 23, 2008
Messages
7
Programming Experience
Beginner
How do I prevent resizing of my Controls by the user, via Docking or anchoring or manual resizing during design-time?
and
How can I programmatically manipulate Anchor styles?
 
I don't know if this is the best way, but when I create custom controls I sometimes create code in the resize event that sets the size of the control. This effectively makes the control that size even if you try to change it at design time.

For you to do this with a Button for example, create a class library DLL. I called my test "ButtonResizeLock". Add references to "System.Drawing" and "System.Windows.Forms". Then paste the following code.

VB.NET:
Public Class ButtonResizeLocked
    Inherits System.Windows.Forms.Button

    Private Sub Class1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        Me.Size = New System.Drawing.Size(120, 25)

    End Sub

    Public Sub New()
        Me.Size = New System.Drawing.Size(120, 25)
    End Sub
End Class

Build the DLL and add a WindowApplication to test the results. Your custom control will appear at the top of the toolbox. When you create one on the form the New event sets the size. If you try to change the size by dragging or changing the Size properties, the resize event will override your efforts.
 
Back
Top