Topmost Windows Forms

Asked

Viewed 146 times

1

I have two screens in Windows Forms that are enabled with Topmost, but when the second window opens, the first one (which also has top Most) is underneath, but I need both windows to be above windows, but the window1 should always be on top of window2.

1 answer

2


If both are defined as TopMost, will always be on top the last one that was focused or the last one that was opened. But if you want to open the window2, the window1 appears on top, you can call the method .BringToFront(); of window 1.

Example:

 Form2 janela1;
    private void button2_Click(object sender, EventArgs e)
    {
        janela1 = new Form2();
        janela1.Text = "1";
        janela1.StartPosition = FormStartPosition.CenterScreen;
        janela1.TopMost = true;
        janela1.Show();

    }

    private void button3_Click(object sender, EventArgs e)
    {
        Form2 janela2 = new Form2();
        janela2.Text = "2";
        janela2.StartPosition = FormStartPosition.CenterScreen;
        janela2.TopMost = true;
        janela2.Show();
        if (janela1 != null && !janela1.IsDisposed)
            janela1.BringToFront();
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.