Changing TopMost of a form from another form

108 views Asked by At

I'm trying to change the topmost of my Main form from the Settings form and it doesnt work. Here is the code:

private void button1_Click(object sender, EventArgs e)
        {
            if (this.button1.Text == "Top Most: ON")
            {
                this.button1.Text = "Top Most: OFF";

                var main = new Main();
                main.TopMost = false;
            }

            else if (this.button1.Text == "Top Most: OFF")
            {
                this.button1.Text = "Top Most: ON";

                var main = new Main();
                main.TopMost = true;
            }
        }
1

There are 1 answers

0
Caius Jard On

In the settings form:

private MainForm _mf;

public SettingsForm(MainForm mf){
    InitializeComponent();
    _mf = mf;
}


private void button1_Click(object sender, EventArgs e)
{
    _mf.TopMost = !_mf.TopMost;
    this.button1.Text == "Top Most: " + _mf.TopMost ? "ON" : "OFF;
}

And in your main form that shows the settings:

new SettingsForm(this).Show...