Results 1 to 15 of 15

Thread: [RESOLVED] Showing Hidden Controls at Runtime

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    84

    Resolved [RESOLVED] Showing Hidden Controls at Runtime

    Hi,

    I have a number of controls, checkboxes, textboxes etc that are hidden (visible = false) from the user and only available by me when when in IDE mode, I use these controls when debugging the program etc.

    When I have compiled the software, it would be helpful to press a certain key sequence \ combination on the keyboard that will then make the hidden controls appear should I wish to check the setting and data etc. The user will not know the sequence.

    Then I can use the key sequence combination etc to hide the controls again?

    If this is possible what would be the best method \ route to take?

    Thanks in advance

    Daz.....

  2. #2
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: Showing Hidden Controls at Runtime

    Set the form's KeyPreview to True then add code similar to this.
    Code:
    Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
        Static bShow As Boolean
        
        If KeyCode = vbKeyE Then
            If Shift = 1 Then
                bShow = Not bShow
                cntlX.Visible = bShow
            End If
        End If
    End Sub
    Change cntlX to your control name
    It will toggle when you hit Shift + E, you can change the key to whatever you like.

    P.S. this could be called an Easter Egg

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    84

    Re: Showing Hidden Controls at Runtime

    Hi,

    Thanks for the code.

    I was playing with the code and trying to check for three keys pressed, but could not get it to work, works on 2 OK. I have also tried it in Select case for 3 keys.

    Is three not possible or more sensitive to pick up in the code?

    Select Case KeyCode
    Case vbKeyU
    If KeyCode = vbKeyK Then
    If Shift = 1 Then
    sMsg = MsgBox("OK To Show Hidden Controls Here")
    Else
    sMsg = MsgBox("Controls Are Hidden")
    End If
    End If
    Case Else
    sMsg = MsgBox("Controls Are Hidden")
    End Select

    Daz........

  4. #4
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: Showing Hidden Controls at Runtime

    look at this then
    Code:
        If KeyCode = vbKeyE Then
    '        If Shift = 1 Then
    '            bShow = Not bShow
    '            cntlX.Visible = bShow
    '        End If
            Select Case Shift
                Case 0 'no keys pressed
                    Stop
                Case vbShiftMask
                    Stop
                Case vbCtrlMask
                    Stop
                Case vbAltMask
                    Stop
                Case vbShiftMask Or vbCtrlMask 'shift and Ctrl are both pressed
                    Stop
                Case vbCtrlMask Or vbAltMask ' Ctrl and Alt are pressed
                    Stop
                Case vbAltMask Or vbShiftMask 'Alt and Shift are pressed
                    Stop
                Case vbShiftMask Or vbCtrlMask Or vbAltMask 'all 3 are down
                    Stop
            End Select
        End If
    BTW, Don't leave those Stops in there, they'll just help you while you figure it out

  5. #5
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Showing Hidden Controls at Runtime

    3 keys? Depends on which 3 keys
    Your keycode is only going to have one key at a time. Therefore, if you want to press, say: A B C and test if all three are down, there are a couple of ways
    1. Use APIs to get the state of the keyboard

    2. Use some booleans, i.e., bKeyA, bKeyB, bKeyC
    .. now in the KeyDown event, when A is pressed, set bKeyA=True, etc
    .. in the KeyUp event, when A is released, set bKeyA=False.
    .. In your KeyDown, when bKeyA=True And bKeyB=True And bKeyC=True then all 3 are pressed.

    3. If one or more of the keys is the shift, alt, or control, then use the Shift variable by comparing it to the shift mask.
    Code:
    If Shift And vbShiftMask Then ' shift is down
    If Shift And vbCtrlMask Then ' ctrl is down'
    If Shift And vbAltMask Then ' Alt is down
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  6. #6
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: Showing Hidden Controls at Runtime

    The simplest thing to do would be to add sub to the app called, for example, MySecretTestingSub and in that sub have

    List1.Visible = True
    Combo1.Visible = True
    etc.

    Then when you want to debug just type MySecretTestingSub in the Immediate window.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    84

    Re: Showing Hidden Controls at Runtime

    Hi,

    Thanks for the responses.

    I already use 'MartinLiss' suggestion and that works OK, I just set a flag boolean 'bDebugView = True' or False to view the controls whilst in the IDE.

    But I was after something that could do the same with with but in compiled EXE version. Something that a user would not stumble upon by pressing random keys etc. Hence the number of keys that need to be pressed.

    I will have a play with the suggestions posted.

    Thanks again

    Daz.......

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Showing Hidden Controls at Runtime

    For each of the controls you are using, put the word: Admin
    in its Tag property. Then, use this to toggle their visibility at runtime
    Code:
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    If (Shift And vbCtrlMask) = vbCtrlMask _
        And (Shift And vbShiftMask) = vbShiftMask And KeyCode = vbKeyP Then
        Dim ctrl As Control
          For Each ctrl In Me.Controls
             If ctrl.Tag = "Admin" Then
                If ctrl.Visible = False Then
                   ctrl.Visible = True
                Else
                   ctrl.Visible = False
                End If
             End If
          Next
    End If
    End Sub
    Of course, you don't need to use vbKeyP, but this is a three key combination consisting of Ctrl+Shift+P

  9. #9
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: Showing Hidden Controls at Runtime

    If you have a visible textbox on the form, here's an idea:
    Code:
    Private Sub TextBox1_Change()
        If TextBox1.Text = "MySecretTestingSub" Then
            MySecretTestingSub
        End If
    End Sub
    A user isn't likely to stumble onto that one
    It could be done with most controls that accept text input from the user.

  10. #10
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: Showing Hidden Controls at Runtime

    here's another way to do Hacks code:
    Code:
        Dim ctrl As Control
          For Each ctrl In Me.Controls
             If ctrl.Tag = "Admin" Then
                ctrl.Visible = Not ctrl.Visible
             End If
          Next

  11. #11
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: Showing Hidden Controls at Runtime

    You could also verify the current user name with a call to this UserName function in your Form_Load and only show the controls if UserName was your name.
    Code:
    Option Explicit
    Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    Public Function UserName() As String
    Dim strBuffer As String * 512
    Dim x As Long
    UserName = ""
    x = GetUserName(strBuffer, Len(strBuffer) - 1)
    If x > 0 Then
      x = InStr(strBuffer, vbNullChar)
      If x > 0 Then UserName = Left$(strBuffer, x - 1)
    End If
    End Function

  12. #12
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Showing Hidden Controls at Runtime

    I do like this way better - kudos longwolf
    Code:
    Dim ctrl As Control
          For Each ctrl In Me.Controls
             If ctrl.Tag = "Admin" Then
                ctrl.Visible = Not ctrl.Visible
             End If
          Next

  13. #13
    Frenzied Member longwolf's Avatar
    Join Date
    Oct 2002
    Posts
    1,343

    Re: Showing Hidden Controls at Runtime

    Quote Originally Posted by Hack
    - kudos longwolf
    Thx
    It's a handy way to to do anything that 'toggles'
    I use it a lot to enable/disable menu items.

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    84

    Re: Showing Hidden Controls at Runtime

    Hi,

    Lots of things to have a look at here, thanks for the responses I will let you know how i get on.

    Thanks

    Daz......

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    84

    Re: Showing Hidden Controls at Runtime

    Hi,

    Thanks for eveyones responses.

    I eventually used a mixture of 'Hacks', 'Longfwolfs' and some of my own idea's.

    It toggles the hidden controls to be visible if the right key sequence is entered, then using the sequence again, hides the controls again.

    Code below if anyone else needs something similar.

    Thanks agin.

    Daz.....
    vb Code:
    1. '==========
    2. Option Explicit
    3. Public dDebug as Boolean
    4.  
    5. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    6.     Select Case bDebug
    7.     Case True   'If already in debug mode then close debug mode
    8.         If (Shift And vbCtrlMask) = vbCtrlMask And (Shift And vbShiftMask) = vbShiftMask And (KeyCode = vbKeyP) Then
    9.             Call DebugMode(False)
    10.         End If
    11.     Case False    'If not in debug mode then open in debug mode
    12.         If (Shift And vbCtrlMask) = vbCtrlMask And (Shift And vbShiftMask) = vbShiftMask And (KeyCode = vbKeyP) Then
    13.             Call DebugMode(True)
    14.         End If
    15.     End Select
    16. End Sub
    17.  
    18. '=====
    19.  
    20. Private Sub DebugMode(bValue As Boolean)
    21. Select Case bValue
    22.     Case False
    23.         bDebug = False
    24.         With frmXXX
    25.             .Width = 10650 'Reduces Form Size Back To Users View
    26.             .Height = 7060  'Reduces Form Size Back To Users View
    27.         End With
    28.     Case True
    29.         bDebug = True
    30.         With frmXXX
    31.                 .Width = 14820 'Enlarges Form Size Back To Debug View
    32.                 .Height = 7680  'Enlarges Form Size Back To Debug View
    33.         End With
    34.     End Select
    35.  
    36. With frmXXX
    37. Dim Ctrl As Control
    38.     For Each Ctrl In .Controls
    39.         If Ctrl.Tag = "Debug" Then
    40.             Ctrl.Visible = bDebug
    41.         End If
    42.     Next
    43. End With
    44. End Sub
    Last edited by Hack; Oct 31st, 2008 at 01:08 PM. Reason: Added Highlight Tags

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