1

I have a main form window which will pop up a new form window. I want to lock the location of the popup form so that the window cannot be moved and it will move at the same time as the main form. (so if a user drags the main form the popup moves with it)

Did a search on the site and some did it like this:

this.FormBorderStyle=System.Windows.Forms.FormBorderStyle.None

and I have the Locked attribute set to True but that doesn't work.

But I want to keep the borders. What's the proper way of locking a form?

sd_dracula
  • 3,796
  • 28
  • 87
  • 158
  • 2
    This is violating a number of UI usability rules. Implodes pretty badly when the user min/maximizes the main window. The simple and intuitive way is to just make your main window bigger. Dock or anchor a user control to the right. – Hans Passant Jan 14 '13 at 22:54
  • Yes that is what I want to do. It's like a little log what should pop out to th side of the main window. What's the best way to do it? – sd_dracula Jan 14 '13 at 23:44

2 Answers2

1

You could do something like this (taken from here):

protected override void WndProc(ref Message message)
{
    const int WM_SYSCOMMAND = 0x0112;
    const int SC_MOVE = 0xF010;

    switch(message.Msg)
    {
        case WM_SYSCOMMAND:
           int command = message.WParam.ToInt32() & 0xfff0;
           if (command == SC_MOVE)
              return;
           break;
    }

    base.WndProc(ref message);
}
Community
  • 1
  • 1
Daniel
  • 10,864
  • 22
  • 84
  • 115
  • This bit causes an error: `0×0112;` it says `; expected` Is that correct syntax for c#? – sd_dracula Jan 14 '13 at 23:08
  • @sd_dracula for some reason the x was a multiply sign (×) when I copy and pasted it. It should compile if you replace it with an x, as in the edit above. – Daniel Jan 15 '13 at 13:53
1
public class Form1
{
    private Form2 Form2 = new Form2();
    private Point form2Location;
    private Point form1Location;
    private void Button1_Click(System.Object sender, System.EventArgs e)
    {
        form1Location = this.Location;
        Form2.Show();
        form2Location = Form2.Location;
    }

    private void Form1_Move(System.Object sender, System.EventArgs e)
    {
        Form2.IsMoving = true;
        Point form2OffSetLocation = new Point(this.Location.X - form2Location.X, this.Location.Y - form2Location.Y);
        Form2.Location = form2OffSetLocation;
        Form2.IsMoving = false;
    }
}    

public class Form2
{

    public bool IsMoving;
    private void Form2_Move(System.Object sender, System.EventArgs e)
    {
        if (IsMoving) return; 
        if (staticLocation.X != 0 & staticLocation.Y != 0) this.Location = staticLocation; 
    }

    private Point staticLocation;
    private void Form2_Load(System.Object sender, System.EventArgs e)
    {
        staticLocation = this.Location;
    }
}

I agree with Hans on this one and I think once you see how dodgy it looks you'll probably agree too.

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321