[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 354
  • Last Modified:

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?
0
Pretzel_Jesus
Asked:
Pretzel_Jesus
  • 3
  • 2
1 Solution
 
käµfm³d 👽Commented:
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

0
 
Pretzel_JesusAuthor Commented:
Is there a better way? This makes the form flash and behave strangely. It looks very clumsy in a professional application.
0
 
käµfm³d 👽Commented:
Taken from the site:

http://www.syncfusion.com/FAQ/windowsforms/faq_c95c.aspx#q1091q
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
 
    protected override void WndProc(ref Message m)
    {
        const int WM_NCLBUTTONDOWN = 161;
        const int WM_SYSCOMMAND = 274;
        const int HTCAPTION = 2;
        const int SC_MOVE = 61456;
 
        if ((m.Msg == WM_SYSCOMMAND) && (m.WParam.ToInt32() == SC_MOVE))
        {
            return;
        }
 
        if ((m.Msg == WM_NCLBUTTONDOWN) && (m.WParam.ToInt32() == HTCAPTION))
        {
            return;
        }
 
        base.WndProc(ref m);
    } 
}

Open in new window

0
 
Pretzel_JesusAuthor Commented:
This was very sexy and worked like a charm. Thanks man.
0
 
käµfm³d 👽Commented:
NP. Glad to help.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now