Solved

How to move a form in c#

Posted on 2009-07-01
9
576 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 86

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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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 86

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 86

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

688 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