Mathieu
Mathieu

Reputation: 4520

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

Upvotes: 7

Views: 12165

Answers (2)

SwDevMan81
SwDevMan81

Reputation: 49978

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();
}

Upvotes: 17

Beth
Beth

Reputation: 9607

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

Upvotes: 2

Related Questions