Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to move a form in c#

Posted on 2009-07-01
9
Medium Priority
?
587 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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…
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…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

636 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