"Marty" <xm******@hotmail.com> skrev i meddelandet
news:opmef.196944$ir4.50531@edtnps90...
How do we make a form "stick" to another window (something like Winamp)?
I'm using C# .NET 2003.
Here's a "stripped down" example on how you can do it.
The key to this solution is to have them have references to each other.
When form1 changes size or location it calls a method in form2 to adjust the
location of that form.
public class Form1 : System.Windows.Forms.Form
{
private Form2 form2 = null;
public Form1()
{
InitializeComponent();
form2 = new Form2(this);
}
// ...
private void InitializeComponent()
{
// ...
this.SizeChanged += new
System.EventHandler(this.Form1_LocationChanged);
this.LocationChanged += new
System.EventHandler(this.Form1_LocationChanged);
// ...
}
private void Form1_LocationChanged(object sender, System.EventArgs e)
{
form2.AdjustToForm1();
}
}
public class Form2 : System.Windows.Forms.Form
{
private Form1 form1 = null;
public Form2(Form1 m)
{
form1 = m;
InitializeComponent();
AdjustToForm1();
}
// ...
internal void AdjustToForm1()
{
this.Left = form1.Left;
this.Top = form1.Bottom;
this.Width = form1.Width;
this.Refresh();
}
private void Form2_Load(object sender, System.EventArgs e)
{
AdjustToForm1();
}
}
// Bjorn A