Form resizing itself when using code to drag form around

Posted on 2007-10-01
Last Modified: 2010-04-23
    Using VB.NET 2005 and I have a form that I want to be able to drag around by clicking anywhere in the form window. I achieve this by using the below code that has been posted on this site several times. The problem is: after I add this code to a form any time I make changes in the code for the form and then switch back to the design view of that form the form is resizing itself to a smaller size. So if my form size is (453, 200) , after making some changes and going back into the design view the form will all of a sudden be (449, 196).  This keeps happening over and over, the form keeps getting smaller ! So I am constantly having to change the size of the form back and move the controls around again to reposition them. Any idea why this is happening ? Is there a way to prevent this ?  Any help would be greatly appreciated ! Thanks !

    Const WM_NCHITTEST As Integer = &H84
    Const HTCLIENT As Integer = &H1
    Const HTCAPTION As Integer = &H2
    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        Select Case m.Msg
            Case WM_NCHITTEST
                If m.Result.ToInt32 = HTCLIENT Then m.Result = IntPtr.op_Explicit(HTCAPTION)
            Case Else
        End Select
    End Sub
Question by:kasowitz
    LVL 9

    Expert Comment

    I think you want to check if your in designmode before executing the code

    if not me.designmode then...

    Author Comment

    The resizing is not happening when I execute the code. It is happening while I am still working on and designing the code/form.
    LVL 9

    Expert Comment

    So if you remove the code it doesnt resize in design? Is this a usercontrol?

    Author Comment

    Actually, I just tried Removing the code out of my form, and it is still doing it. Why would my form be making itself smaller all the time?  Its just a standard form with a couple of buttons on it.  Any time I make a change to the code of the form, and switch back to the design view of the form, the form size is getting smaller.  This makes no sense.
    LVL 85

    Accepted Solution

    Try creating a NEW project with just ONE form on it.  Does the problem persist?

    If it is only happening on one project/form then it is something local to that project.  It is either "corrupt" or possibly it has a third party control on it?

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now