locked
I want my form to be not movable in runtime.what should i do? RRS feed

  • Question

  • hi guys,
    i have a winform.I set its lock property to true.but it is still moveable.
    Brainstorm your Brain and find solution,if no result stuck to Brainstormer.
    Saturday, July 11, 2009 7:21 PM

Answers

  • Hi!

    I agree with Tergiver, it's usually not good to "lock" a window. However, if you can't ignore this requirement, you can subscribe to your window's Move event and move the window back to it's original position.

    public partial class Form1 : Form
    {
        Point defaultLocation;
    
        public Form1()
        {
            InitializeComponent();
            defaultLocation = this.Location;
        }
    
        private void Form1_Move(object sender, EventArgs e)
        {
            this.Location = defaultLocation;
        }
    }
    But again, try to find another way to achieve the desired functionality and behavior.

    David
    • Proposed as answer by David Fulop Sunday, July 12, 2009 10:12 AM
    • Marked as answer by Harry Zhu Friday, July 17, 2009 4:06 AM
    Sunday, July 12, 2009 10:11 AM
  • Hi Simosi,

    I found some helpful code that can extends System.Windows.Forms.Control to implement a consistent way of allowing and disallowing users to edit the contents of the control.
    Please see: http://69.10.233.10/KB/dotnet/Extension_LockAndUnlock.aspx (It's VB.NET code, you can try changing it to C# code.)

    Hope this can help you a lot.

    Thanks
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Harry Zhu Friday, July 17, 2009 4:06 AM
    • Edited by YiChun Chen Friday, July 17, 2009 4:10 AM typo
    Tuesday, July 14, 2009 11:02 AM
  • Hi,

    This can also be done by overriding the WndProc method which omits the move message.  


           const int WM_SYSCOMMAND = 0x0112;
           const int SC_MOVE = 0xF010;
            protected override void WndProc(ref Message message)
            {            
                switch(message.Msg)
                {
                case WM_SYSCOMMAND:
                int command = message.WParam.ToInt32() & 0xfff0;
                if (command == SC_MOVE)
                return;
                break;
                }
                base.WndProc(ref message)   
            }
            
    Harry


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Harry Zhu Friday, July 17, 2009 4:06 AM
    Friday, July 17, 2009 4:06 AM

All replies

  • It is not your place to tell the user where their windows belong. It is their desktop, not yours. Although there are ways to hack your application and make it unmovable, I would encourage you to rethink this requirement. They may have a legitimate reason to move your window. Probably you've made it misbehave in other ways and they need to get it out of the way of something more pressing.
    Saturday, July 11, 2009 7:50 PM
  • Hi!

    I agree with Tergiver, it's usually not good to "lock" a window. However, if you can't ignore this requirement, you can subscribe to your window's Move event and move the window back to it's original position.

    public partial class Form1 : Form
    {
        Point defaultLocation;
    
        public Form1()
        {
            InitializeComponent();
            defaultLocation = this.Location;
        }
    
        private void Form1_Move(object sender, EventArgs e)
        {
            this.Location = defaultLocation;
        }
    }
    But again, try to find another way to achieve the desired functionality and behavior.

    David
    • Proposed as answer by David Fulop Sunday, July 12, 2009 10:12 AM
    • Marked as answer by Harry Zhu Friday, July 17, 2009 4:06 AM
    Sunday, July 12, 2009 10:11 AM
  • Hi Simosi,

    I found some helpful code that can extends System.Windows.Forms.Control to implement a consistent way of allowing and disallowing users to edit the contents of the control.
    Please see: http://69.10.233.10/KB/dotnet/Extension_LockAndUnlock.aspx (It's VB.NET code, you can try changing it to C# code.)

    Hope this can help you a lot.

    Thanks
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Harry Zhu Friday, July 17, 2009 4:06 AM
    • Edited by YiChun Chen Friday, July 17, 2009 4:10 AM typo
    Tuesday, July 14, 2009 11:02 AM
  • Hi,

    This can also be done by overriding the WndProc method which omits the move message.  


           const int WM_SYSCOMMAND = 0x0112;
           const int SC_MOVE = 0xF010;
            protected override void WndProc(ref Message message)
            {            
                switch(message.Msg)
                {
                case WM_SYSCOMMAND:
                int command = message.WParam.ToInt32() & 0xfff0;
                if (command == SC_MOVE)
                return;
                break;
                }
                base.WndProc(ref message)   
            }
            
    Harry


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Harry Zhu Friday, July 17, 2009 4:06 AM
    Friday, July 17, 2009 4:06 AM