-
Oct 29th, 2008, 02:13 PM
#1
Thread Starter
Lively Member
[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.....
-
Oct 29th, 2008, 02:39 PM
#2
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
-
Oct 29th, 2008, 03:54 PM
#3
Thread Starter
Lively Member
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........
-
Oct 29th, 2008, 04:05 PM
#4
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
-
Oct 29th, 2008, 04:07 PM
#5
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
-
Oct 29th, 2008, 06:50 PM
#6
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.
Last edited by MartinLiss; Oct 30th, 2008 at 08:51 AM.
-
Oct 30th, 2008, 04:09 AM
#7
Thread Starter
Lively Member
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.......
-
Oct 30th, 2008, 07:19 AM
#8
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
-
Oct 30th, 2008, 07:24 AM
#9
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.
-
Oct 30th, 2008, 07:40 AM
#10
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
-
Oct 30th, 2008, 09:01 AM
#11
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
-
Oct 30th, 2008, 09:35 AM
#12
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
-
Oct 30th, 2008, 10:40 AM
#13
Re: Showing Hidden Controls at Runtime
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.
-
Oct 30th, 2008, 02:48 PM
#14
Thread Starter
Lively Member
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......
-
Oct 31st, 2008, 09:18 AM
#15
Thread Starter
Lively Member
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:
'==========
Option Explicit
Public dDebug as Boolean
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case bDebug
Case True 'If already in debug mode then close debug mode
If (Shift And vbCtrlMask) = vbCtrlMask And (Shift And vbShiftMask) = vbShiftMask And (KeyCode = vbKeyP) Then
Call DebugMode(False)
End If
Case False 'If not in debug mode then open in debug mode
If (Shift And vbCtrlMask) = vbCtrlMask And (Shift And vbShiftMask) = vbShiftMask And (KeyCode = vbKeyP) Then
Call DebugMode(True)
End If
End Select
End Sub
'=====
Private Sub DebugMode(bValue As Boolean)
Select Case bValue
Case False
bDebug = False
With frmXXX
.Width = 10650 'Reduces Form Size Back To Users View
.Height = 7060 'Reduces Form Size Back To Users View
End With
Case True
bDebug = True
With frmXXX
.Width = 14820 'Enlarges Form Size Back To Debug View
.Height = 7680 'Enlarges Form Size Back To Debug View
End With
End Select
With frmXXX
Dim Ctrl As Control
For Each Ctrl In .Controls
If Ctrl.Tag = "Debug" Then
Ctrl.Visible = bDebug
End If
Next
End With
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|