【问题标题】:Changing TopMost of a form from another form从另一个表单更改表单的 TopMost
【发布时间】:2021-08-20 21:44:28
【问题描述】:

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

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

【问题讨论】:

  • var main = new Main(); 这不引用现有的主窗体。这是“新的”。

标签: c# winforms .net-core topmost


【解决方案1】:

在设置表单中:

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...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-02
    • 2012-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-10
    相关资源
    最近更新 更多