Click here to Skip to main content
15,895,011 members

form flickers when on move event

qbndl8 asked:

Open original thread
I wrote the following procedure to move and dock a borderless window:

Public Class frmNavigation

    Inherits Form

    'Declarations to allow form movement on mouse down
    Private IsFormBeingDragged As Boolean = False
    Private MouseDownX As Integer
    Private MouseDownY As Integer
    Dim Xs As Integer
    Dim Ys As Integer
    Dim DockScale As Integer

Private Sub frmNavigation_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseDown

        'This procedure allows the user to move the form when the 
        'mouse button is down. The form does not have borders, so it
        'needs to be coded to move.

        If e.Button = MouseButtons.Left Then
            IsFormBeingDragged = True
            MouseDownX = e.X
            MouseDownY = e.Y

        End If
    End Sub

    Private Sub frmNavigation_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseUp

        'This procedure allows the user to move the form when the 
        'mouse button is up. The form does not have borders, so it
        'needs to be coded to move.

        If e.Button = MouseButtons.Left Then
            IsFormBeingDragged = False

        End If
    End Sub

    Private Sub frmNavigation_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseMove

        'This procedure allows the user to move the form when the 
        'mouse button is dragging the form. The form does not have borders, so it
        'needs to be coded to move.

        Dim curScreen As Screen
        curScreen = Screen.PrimaryScreen 'curScreen = Screen.AllScreens(0)
        Dim height As Integer = curScreen.Bounds.Height
        Dim width As Integer = curScreen.Bounds.Width
        width = curScreen.WorkingArea.Width
        height = curScreen.WorkingArea.Height

        If IsFormBeingDragged Then
            Dim temp As System.Drawing.Point = New System.Drawing.Point()

            Xs = MouseDownX
            Ys = MouseDownY

            temp.X = Me.Location.X + (e.X - MouseDownX)
            temp.Y = Me.Location.Y + (e.Y - MouseDownY)
            Me.Location = temp
            temp = Nothing
        End If

End Sub

End Class


So far, this works as designed, it moves the form without any issue whatsoever. The issue starts when I add code to dock the form under the mouse move event as:


Private Sub frmNavigation_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseMove

    'This procedure allows the user to move the form when the 
    'mouse button is dragging the form. The form does not have borders, so it
    'needs to be coded to move.

    Dim curScreen As Screen
    curScreen = Screen.PrimaryScreen 'curScreen = Screen.AllScreens(0)
    Dim height As Integer = curScreen.Bounds.Height
    Dim width As Integer = curScreen.Bounds.Width
    width = curScreen.WorkingArea.Width
    height = curScreen.WorkingArea.Height

    If IsFormBeingDragged Then
        Dim temp As System.Drawing.Point = New System.Drawing.Point()

        Xs = MouseDownX
        Ys = MouseDownY

        temp.X = Me.Location.X + (e.X - MouseDownX)
        temp.Y = Me.Location.Y + (e.Y - MouseDownY)
        Me.Location = temp
        temp = Nothing
    End If

    If IsFormBeingDragged = True And e.Button = MouseButtons.Left Then
        'if the drag flag is true and left mouse button is pressed...

        'set Top side docking
        If Me.Top + (MouseDownY - Ys) < DockScale Then
            Me.Top = 0
            Exit Sub
        End If

        'set bottom side docking
        If Me.Top + (MouseDownY - Ys) + Me.Height > (height - DockScale) Then
            Me.Top = height - Me.Height
            Exit Sub
        End If

        'move the form finally
        Me.Left = Me.Left + (MouseDownX - Xs)
        Me.Top = Me.Top + (e.Y - Ys)
    End If


End Sub


When I add the code for docking and I try to move the form, it moves and it docks, but it flickers like crazy when holding the mouse down and moving. I can't see why this happens, is the first time I dabble with something like this, so I am not sure where I am going wrong.
Tags: Visual Basic, Windows Forms

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900