Reputation: 3896
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?
Upvotes: 1
Views: 3299
Reputation: 11064
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);
}
Upvotes: 1
Reputation: 65682
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.
Upvotes: 1