c# - 如何制作 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。
解决方案
您需要做的就是将对 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);
}
}
推荐阅读
- python - 绘制方程来表示球体的线框?(大圆圈)
- web-standards - 如何在 Web 浏览器中创建和显示 HDR 图像?
- api - 如何在 Azure 中的应用程序网关后面运行 docker 容器化应用程序
- html - 在内联 div 中嵌套 tds 会导致渲染问题吗?
- chronicle - 使用 Chronicle-Queue 作为基于文件的 FIFO 队列
- javascript - 如何使用字符串插值Javascript迭代构建的变量
- python-2.7 - 在 Tkinter 菜单小部件中获取条目列表(或字典或元组)
- azure-cosmosdb - Azure Cosmos Graph DB 复合条件查询
- amazon-web-services - 为什么在我的 lambda 函数中看不到日志?
- c - 为 C 学习项目设置 Visual Studio 2017