Link to home
Start Free TrialLog in
Avatar of Pretzel_Jesus
Pretzel_Jesus

asked on

How To Force A Form So It Cannot Be Dragged (C#)

I have an application that opens a second form which is a settings style dialog window. I like this because it forces the settings window to stay above everything else and must be closed before using the application again. I currently have it at a fixed size, and it opens in the center of the screen. I dont like the fact that it can currently be dragged around the screen. How can I force the form to be "undraggable" without doing anything strange like changing the border style, etc?
Avatar of kaufmed
kaufmed
Flag of United States of America image

You could do something like the following--it may appear to drag, but once user lets go, it will return to original location.
    public partial class Form1 : Form
    {
        private Point startLocation;
 
        public Form1()
        {
            InitializeComponent();
            this.startLocation = this.Location;
        }
 
        private void Form1_Move(object sender, EventArgs e)
        {
            this.Location = this.startLocation;
        }
    }

Open in new window

Avatar of Pretzel_Jesus
Pretzel_Jesus

ASKER

Is there a better way? This makes the form flash and behave strangely. It looks very clumsy in a professional application.
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This was very sexy and worked like a charm. Thanks man.
NP. Glad to help.