469,913 Members | 2,022 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,913 developers. It's quick & easy.

how to make a form "stick" to another window?

Hi,

How do we make a form "stick" to another window (something like Winamp)?

I'm using C# .NET 2003.

Thank you very much.
Marty
Nov 17 '05 #1
2 5528
"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
Nov 17 '05 #2
Hi Bjorn,

Thank you for your fast reply, very useful.
Have a nice day!
Marty

Bjorn Abelli wrote:
"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

Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

388 posts views Thread by maniac | last post: by
5 posts views Thread by Sue | last post: by
43 posts views Thread by markryde | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.