TopMost Windows Forms

1

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

    
asked by anonymous 20.06.2017 / 22:18

1 answer

2

If both are set to TopMost , the last one that was focused or the last one that was opened will always be over. But if you want that when you open window2, window1 appears at the top, you can call the .BringToFront(); method of window1.

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();
    }
    
20.06.2017 / 22:40