Tyler Malo
Tyler Malo

Reputation: 63

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

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?

Upvotes: 6

Views: 32138

Answers (5)

user10878603
user10878603

Reputation:

To have it toggled On and Off use:

TopMost = CheckBox1.Checked

Just make sure you replace CheckBox1 to whatever you are using.

Upvotes: 0

nullpotent
nullpotent

Reputation: 9260

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

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

Form.TopMost = True

To disable TopMost, use this:

Form.TopMost = False

Upvotes: 7

PDX Geek
PDX Geek

Reputation: 51

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

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)

Upvotes: 3

HacKer_CaN
HacKer_CaN

Reputation: 1

It can be:

Me.TopMost = true 

or false.

Upvotes: 0

rob05c
rob05c

Reputation: 1233

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

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

Upvotes: 11

Related Questions