简体   繁体   中英

How can I make one form constantly overlaying another form?

I need form2 to be on top of form1 and at the same size and location of form1. Especially when form1's location changes. Simply, how do i get form2 to follow form1?

Subscribe to the SizeChanged event of form1 by adding an event handler either in the constructor or via the properties menu in Visual Studio and update the size and position of form2 in that.

To add an event handler manually add the following in your constructor:

this.SizeChanged += new System.EventHandler(this.AlbumChooser_SizeChanged);

(If you just type this.SizeChanged += then tab twice the rest of the line and the event handler method will be created for you). Then the handler will look like this:

    private void AlbumChooser_SizeChanged(object sender, EventArgs e)
    {
        form2.Location = new Point(this.Location);
        ....
    }

You may also have to subscribe to the ResizeEnd event as well.

It looks like you are looking for the wrong solution. What I would do is create 2 User Controls , one for your current Form1 and one for your current Form2 .

Put the scrolling text in UserControl1 and the Image in UserControl2 .

Add both of these User Controls to a form, overlapping, and change the visibility of the user controls instead of creating new forms. When swapping:

private void SwapVisibility() {
    UserControl1.Visible = !UserControl1.Visible;
    UserControl2.Visible = !UserControl2.Visible;
}

Set the Visibile property of UserControl2 to false intially.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM