?
Solved

How to move a form in c#

Posted on 2009-07-01
9
Medium Priority
?
577 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:Nate Feinberg
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
A new era in Cloud training has arrived.

A day that will go down in Cloud history.. But are you ready for it? Will you accept this Cloud challenge?

 

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 375 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

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

752 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