首页 > 解决方案 > 如何制作 TopMost 拨动开关

问题描述

您好,我正在尝试制作 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。

标签: c#toggleswitchbunifu

解决方案


您需要做的就是将对 Main 的引用传递到您的设置表单中。一种方法是当您调用Show()or时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);
    }
}

推荐阅读