Search code examples
vb.netwinformslagwindow-resize

Resize Bordless Form is Glitchy


My code to resize my form from the right hand side works, but is really glitchy or has a lot of lag. All my images blur and my drawn rectangles flicker while I'm resizing. Everything is normal however once mouseup.

I put the formborderstyle from borderless to sizable and the form resizes normally.

Hoping someone might be able to point out what is wrong with my code.

Dim myresize As Boolean = False
Dim cursorx As Integer
Dim cursory As Integer

Private Sub TableLayoutPanel1_MouseDown(sender As Object, e As MouseEventArgs) Handles TableLayoutPanel1.MouseDown

    If e.Location.X > Me.Width - 7 And e.Location.Y > 11 And e.Location.Y < Me.Height - 10 Then
        myresize = True
        cursorx = Windows.Forms.Cursor.Position.X - Me.Width
        cursory = Windows.Forms.Cursor.Position.Y - Me.Height
    End If

End Sub

Private Sub TableLayoutPanel1_MouseMove(sender As Object, e As MouseEventArgs) Handles TableLayoutPanel1.MouseMove

    If myresize = True Then
        Me.Width = Windows.Forms.Cursor.Position.X - cursorx
    End If

End Sub

Private Sub TableLayoutPanel1_MouseUp(sender As Object, e As MouseEventArgs) Handles TableLayoutPanel1.MouseUp

    myresize = False

End Sub

Solution

  • For the flickering, the form does not pass the DoubleBuffered property to its child controls, such as your TableLayoutPanel. Instead try adding a new class to your project that will doubleBuffer the tableLayoutPanel.

    Public Class DoubleBufferedTableLayoutPanel
        Inherits TableLayoutPanel
        Public Sub New()
            Me.DoubleBuffered = True
        End Sub
    End Class
    

    Build your project and the new version of the TableLayoutPanel will be in your toolbox as DoubleBufferedTableLayoutPanel. From there just use it as you would the TableLayoutPanel in your code.