如何制作 TopMost 拨动开关

How to make TopMost toggle switch

你好,我正在尝试制作 TopMost 切换开关,这是代码: private void bunifuiOSSwitch1_OnValueChange(object sender, EventArgs e)

private void bunifuiOSSwitch1_OnValueChange(object sender, EventArgs e)
        {
            Main main = new Main();
            if(bunifuiOSSwitch1.Value == true)
            {
                main.TopMost = true;
            }
            else
            {
                main.TopMost = false;
            }
        }

起初它在切换为 true 时工作,在切换为 false 时工作但是当我尝试重新切换它时它没有工作,之后我尝试再次更改代码但那也没有工作.. . 现在连 TopMost 都没有了。

您需要做的是将对 Main 的引用传递到您的设置表单中。一种方法是调用 Show()ShowDialog():

// ... in Form Main ...
private void button1_Click(object sender, EventArgs e)
{
    Settings settings = new Settings();
    settings.Show(this); // pass in this instance of Main as the "owner" of settings
}

然后,在“设置”中,您可以将 .Owner 属性 转换回 Main 类型并对其执行操作:

// ... in Settings Form ...
private void bunifuiOSSwitch1_OnValueChange(object sender, EventArgs e)
{
    if (this.Owner!=null && this.Owner is Main)
    {
        Main main = (Main)this.Owner;
        main.TopMost = (bunifuiOSSwitch1.Value == true);
    }
}