Solved

How to move a form in c#

Posted on 2009-07-01
9
572 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

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
 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

685 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