vb.net 对于 Always On Top 选项,Visual Basic 代码是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9593074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:59:37  来源:igfitidea点击:

What would the Visual Basic code be for an Always On Top Option?

vb.netvisual-studio-2010

提问by Tyler Malo

I have a Always On Top Tool Strip Menu option and I cant figure out the code that would make it stay on top of other windows when checked, and vice-versa when unchecked. Can you please help?

我有一个 Always On Top Tool Strip Menu 选项,我无法弄清楚在选中时使其保持在其他窗口顶部的代码,反之亦然。你能帮忙吗?

回答by rob05c

To set "always on top," set myForm.TopMost = Truefrom your menu option. See the Form.TopMost documentation.

要设置“始终在最前面” myForm.TopMost = True,请从您的菜单选项中进行设置。请参阅Form.TopMost 文档

To un-set it again, set myForm.TopMost = False.

要再次取消设置,请设置myForm.TopMost = False

回答by nullpotent

To toggle whether the Formis the TopMost, simply change the property Form.TopMost.

要切换 是否FormTopMost,只需更改属性即可Form.TopMost

For example, to set the Form to be on top, use this:

例如,要将 Form 设置为在顶部,请使用以下命令:

Form.TopMost = True

To disable TopMost, use this:

要禁用TopMost,请使用:

Form.TopMost = False

回答by PDX Geek

This is what I used to handle the event if you want it user-driven. You will obviously want to create a checkbox named chkAlwaysOnTopof course. It can also be easily stored in the user settings to keep it state-aware between instances.

如果您希望它由用户驱动,这就是我用来处理事件的方法。您显然希望创建一个名为chkAlwaysOnTop当然的复选框。它也可以很容易地存储在用户设置中,以使其在实例之间保持状态感知。

Private Sub chkAlwaysOnTop_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkAlwaysOnTop.CheckedChanged
    Me.TopMost = chkAlwaysOnTop.Checked            
End Sub

You'll want this in your program if you want to save said state for the user:

如果您想为用户保存所述状态,您将需要在您的程序中使用它:

Private Sub MainActivity_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    My.Settings.AlwaysOnTop = chkAlwaysOnTop.Checked
    My.Settings.Save()
End Sub

You'll also want this in your form load:

您还需要在表单加载中使用它:

 Me.TopMost = My.Settings.AlwaysOnTop
 chkAlwaysOnTop.Checked = My.Settings.AlwaysOnTop

If you're interested in what I used this in, it's here: Rubber Stamp(Includes source code link)

如果您对我在其中使用的内容感兴趣,请在此处:橡皮图章(包括源代码链接)

回答by PDX Geek

To have it toggled On and Off use:

要打开和关闭它,请使用:

TopMost = CheckBox1.Checked

TopMost = CheckBox1.Checked

Just make sure you replace CheckBox1to whatever you are using.

只要确保替换CheckBox1为您正在使用的任何内容即可。

回答by HacKer_CaN

It can be:

有可能:

Me.TopMost = true 

or false.

false