Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a custom control on TOP of another one

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.

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

like image 451
Mathieu Avatar asked Nov 03 '10 19:11

Mathieu


2 Answers

You could try the BringToFront control function:

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();
}
like image 135
SwDevMan81 Avatar answered Oct 14 '22 17:10

SwDevMan81


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

like image 39
Beth Avatar answered Oct 14 '22 15:10

Beth