Solved

How to move a form in c#

Posted on 2009-07-01
9
563 Views
Last Modified: 2013-12-17
How can I move a form like how you click+hold the control box and move the mouse around.  I currently have my control box = false.  I have tried the function SetDesktopLocation but it will just refresh the box every point it moves and I would like to just drag it and not refresh the box at every point.  I am using a regular Windows Form.
0
Comment
Question by:htn5ca
9 Comments
 
LVL 8

Expert Comment

by:dericstone
ID: 24757514
It's not entirely clear what you want to do, but maybe these methods will help:

SuspendLayout()
...
form.Location = new System.Drawing.Point(x, y);
...
ResumeLayout()
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24758492
A form with no ControlBox still has a Title Bar.  Just click and drag on that...

If you have a Borderless Form, though, then use code like this:
    public partial class Form1 : Form

    {
 

        public Form1()

        {

            InitializeComponent();

        }
 

        private const int HTCLIENT = 0x0001;

        private const int HTCAPTION = 0x0002;

        private const int WM_NCHITTEST = 0x0084;
 

        protected override void WndProc(ref Message m)

        {

            base.WndProc(ref m);

            if ((m.Msg == WM_NCHITTEST) & (m.Result.ToInt32() == HTCLIENT))

            {

                m.Result = (IntPtr)HTCAPTION;

            }

        }
 

    }

Open in new window

0
 
LVL 8

Expert Comment

by:InternalStatic
ID: 24759282
The Location, Top, and Left properties should work just fine.
Both former posts should also be accurate--a form with its ControlBox property set to false still has a title bar, which allows the user to move it. Do you mean you want to allow them to maximize and minimize the form? If so, use the following demonstrated in the short snippet below.
Hope it helps,
Nate
 

// To move both the form with both coordinates, use

formName.Location = new System.Drawing.Point(xCoordinate, yCoordinate);

// To just move its X coordinate, use

formName.Left = newXCoordinate;

// To move its Y coordinate, use

formName.Top = newYCoordinate;
 

// To maximize the form, use

formName.WindowState = FormWindowState.Maximized;

// To minimize the form, use

formName.WindowState = FormWindowState.Minimized;

// To set the form back to normal (not maximized or minimized), use

formName.WindowState = FormWindowState.Normal;

Open in new window

0
 

Author Comment

by:htn5ca
ID: 24769286
Sorry I don't think I was clear enough.
I will give you my scenario:

I want to have a borderless form slide across the screen.  The SetLocation method when I call it with the X coordinate and just add 1, it does not refresh the box until it stops.  The movement I am looking for is how you simulate someone holding down the title bar and dragging the form across the window and all the content inside is still visible.  The signature is something like this; private void SlideWindow(Form window,int startx, int starty)

hopefully this will clear up any complications
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:htn5ca
ID: 24769290
Sorry the signature is suppose to be like this; private void SlideWindow(Form window).  This will just slide the window across the screen horizontally from left to right.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24769977
"...it does not refresh the box until it stops."

INSIDE your loop, place a call to DoEvents():

    someLoopingStructure {
        window.Location = new Point(newX, newY);
        Application.DoEvents();
    }
0
 

Author Comment

by:htn5ca
ID: 24770115
Actually I figured it out.  I just used a Timer and on every 10ms tick, I moved the form over 1.  I just checked the Location to see if the form went off the screen.  Thanks for all the help.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 125 total points
ID: 24770554
That'll work too...inbetween Tick() events the pending messages will get processed allowing the form to repaint itself in each position.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

15 Experts available now in Live!

Get 1:1 Help Now