Results 1 to 5 of 5

Thread: [RESOLVED] Toggling boolean value with keystroke

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2014
    Posts
    20

    Resolved [RESOLVED] Toggling boolean value with keystroke

    I want to change the border of my form (on/off) by pressing the keyboard key "F10." The code below accomplishes that, but when the border is visible I have to hit F10 twice to hide it again. It's a minor nuissance, but I'd really like to fix it. I can't see what I'm missing here.

    Code:
    Public Class Form1
        Dim ToggleF10 As Boolean = False
    
        Sub Form1Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
            Me.KeyPreview = True
        End Sub
    
        Private Sub FormBorderTog(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
            If (e.KeyCode = Keys.F10) Then
                ' When F10 is pressed, switch boolean value
                Call Toggle()
            End If
            If ToggleF10 = True Then
                'toggle form border on/off according to boolean value
                Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedSingle
    
            Else
                Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
            End If
        End Sub
    
        Private Sub Toggle()
            'toggle boolean value
            If ToggleF10 = False Then
                ToggleF10 = True
            Else
                ToggleF10 = False
            End If
        End Sub

  2. #2
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,284

    Re: Toggling boolean value with keystroke

    I'm getting the same thing, pressing F10 once will show the window border, but to hide it I have to press F10 twice. It's probably a windows api feature, but I don't know for sure.
    Here's the code I used to test it:
    vb Code:
    1. Friend Class Form1
    2.     Private m_ShowBorder As Boolean
    3.  
    4.     Sub Form1Load(ByVal sender As System.Object, ByVal e As EventArgs) Handles Me.Load
    5.         Me.FormBorderStyle = FormBorderStyle.None
    6.         m_ShowBorder = False
    7.     End Sub
    8.  
    9.     Private Sub Form_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
    10.         If (e.KeyCode = Keys.F10) Then
    11.             ' When F10 is pressed, switch boolean value
    12.             Call ToggleBorder()
    13.  
    14.             Me.FormBorderStyle = If(m_ShowBorder, FormBorderStyle.FixedSingle, FormBorderStyle.None)
    15.         End If
    16.     End Sub
    17.  
    18.     Private Sub ToggleBorder()
    19.         'toggle boolean value
    20.         m_ShowBorder = Not m_ShowBorder
    21.     End Sub
    22.  
    23. End Class

  3. #3
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,580

    Re: Toggling boolean value with keystroke

    ...

    Edit:
    F10 is used by Windows to activate the form's main menu, you cannot see the effect of first F10 hit because your form doesn't have menu.
    Last edited by 4x2y; Feb 10th, 2015 at 02:50 PM.



  4. #4

    Thread Starter
    Junior Member
    Join Date
    Aug 2014
    Posts
    20

    Re: Toggling boolean value with keystroke

    Quote Originally Posted by 4x2y View Post
    ...

    Edit:
    F10 is used by Windows to activate the form's main menu, you cannot see the effect of first F10 hit because your form doesn't have menu.
    You're right. I switched it to F9 and it works like a charm. Thanks, 4x2y!

  5. #5
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,119

    Re: Toggling boolean value with keystroke

    F10 is used by windows to activate the active window's menu bar.
    The next key you press should be an accelerator for a menu item.
    A second F10 will cancel the menu activation (so Windows traps that F10 and your application doesn't see it).

    edit: I see that was mentioned by x24y as I was testing.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width