简体   繁体   English

如何使一种形式不断覆盖另一种形式?

[英]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. 我需要form2在form1的顶部,并且具有与form1相同的大小和位置。 Especially when form1's location changes. 特别是当form1的位置更改时。 Simply, how do i get form2 to follow form1? 简而言之,我如何让Form2跟随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. 通过在构造函数中或通过Visual Studio中的属性菜单添加事件处理程序来订阅form1SizeChanged事件,并在其中更新form2的大小和位置。

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). (如果您只键入this.SizeChanged +=this.SizeChanged +=的其余部分使用Tab键两次,然后将为您创建事件处理程序方法)。 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. 您可能还必须订阅ResizeEnd事件。

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 . 我要做的是创建2个用户控件 ,一个用于当前的Form1 ,一个用于当前的Form2

Put the scrolling text in UserControl1 and the Image in UserControl2 . 将滚动文本放在UserControl1 ,将Image放在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. UserControl2Visibile属性Visibile设置为false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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