首页 > 解决方案 > 从另一个表单更改表单的 TopMost

问题描述

我正在尝试从“设置”表单更改我的主表单的最顶部,但它不起作用。这是代码:

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

标签: c#winforms.net-coretopmost

解决方案


在设置表单中:

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

在显示设置的主窗体中:

new SettingsForm(this).Show...

推荐阅读