Search code examples
vb.netformsautosize

border less form gets automatically resized during runtime in vb .net


I've created a small borderless form using vb .net. The form contains three square buttons. The size of the form is (93, 31). All good during the design of the form, but when I run the program the size of the form gets increased to somewhat like (98,34). I even switched between true and false for the autosize property of the form to check if it was the cause of the problem but that dint help.
How do I stop the form from resizing?

EDIT:
I made the form borderless by setting the form's FormBorderStyle property to None

Here's the code

Public Class OSD_Dialog

Dim drag As Boolean
Dim mousex As Integer
Dim mousey As Integer

' The folllowing three subs are helpfull in making the form dragable

  Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
    drag = True
    mousex = Windows.Forms.Cursor.Position.X - Me.Left
    mousey = Windows.Forms.Cursor.Position.Y - Me.Top
End Sub

Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
    If drag Then
        Me.Top = Windows.Forms.Cursor.Position.Y - mousey
        Me.Left = Windows.Forms.Cursor.Position.X - mousex
    End If
End Sub

Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
    drag = False
End Sub

'The following sub is helpful in creating an outline on the border of the form

 Protected Overrides Sub OnPaintBackground(ByVal e As PaintEventArgs)
    MyBase.OnPaintBackground(e)

    Dim rect As New Rectangle(0, 0, Me.ClientSize.Width - 1, Me.ClientSize.Height - 1)

    e.Graphics.DrawRectangle(Pens.White, rect)
End Sub

Private Sub OSD_Dialog_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.TopMost = True
    Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True)

    Me.BackColor = Color.Red
    TransparencyKey = BackColor

End Sub

Solution

  • Add this to your Form_Load event:

    Me.Size = New Size(93, 31)
    

    Also ensure you have set AutoScaleMode to 'None' in design time