visual-studio 如何在另一个控件的顶部添加自定义控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4090776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 12:41:24  来源:igfitidea点击:

How to add a custom control on TOP of another one

c#visual-studiowinforms

提问by Mathieu

I'm using a winForm. I have 2 custom controls that I want to add dynamically. The first one is added at the opening of the form. The second one is added when the user clicks a button. Nothing magic here. The problem is that when I instanciate and add the second control, instead of appearing on top of the other one, it appears under.

我正在使用 winForm。我有 2 个要动态添加的自定义控件。第一个是在表单打开时添加的。当用户单击按钮时添加第二个。这里没有什么魔法。问题是,当我实例化并添加第二个控件时,它没有出现在另一个控件的顶部,而是出现在下面。

There must be a way to add the control in a way that will make it fully visible (on top of the rest). Here is how I create the second control (same way as the first control). I tried using show/hide methods, but this won't change the stack order.

必须有一种方法来添加控件,使其完全可见(在其余部分之上)。这是我创建第二个控件的方法(与第一个控件相同)。我尝试使用显示/隐藏方法,但这不会改变堆栈顺序。

    private void lbRappel_Click(object sender, EventArgs e)
    {
        NoteCallBack noteCallBack = new NoteCallBack("test");
        this.Controls.Add(noteCallBack);
        noteCallBack.Location = new Point(200, 250);
    }

Thank you very much in advance for your help.

非常感谢您的帮助。

Mathieu

马修

回答by SwDevMan81

You could try the BringToFrontcontrol function:

您可以尝试BringToFront控制功能:

private void lbRappel_Click(object sender, EventArgs e)
{
    NoteCallBack noteCallBack = new NoteCallBack("test");
    this.Controls.Add(noteCallBack);
    noteCallBack.Location = new Point(200, 250);
    noteCallBack.BringToFront();
}

回答by Beth

Can you create them at design time with the z-order you want, then only make them visible at runtime?

您能否在设计时使用所需的 z 顺序创建它们,然后仅在运行时使它们可见?