Hi all,
I would like to ask one question. Can I know where the focus is in a main Form? Eg: there will have some buttons, text boxes and labels. How to check the focus?
Please give me some ideals.
Thanks,
zawpai
Hi all,
I would like to ask one question. Can I know where the focus is in a main Form? Eg: there will have some buttons, text boxes and labels. How to check the focus?
Please give me some ideals.
Thanks,
zawpai
Short answer: use the form's ActiveControl to find which control currently has focus, and enable the form to receive key events with KeyPreview = True. and were right that TabIndex controls tab order, but it does not tell you which control has focus at runtime. pointed toward ActiveControl — that is the correct direction.
Common reasons the sample posted by didn't behave as expected:
KeyPreview is True. TabIndex is static; it does not change to indicate focus. KeyDown uses virtual key codes; character checks are simpler in KeyPress where you can use Asc("1"). A simple, robust pattern in VB6 is: set KeyPreview = True, handle Form_KeyPress to detect the character, use Me.ActiveControl and TypeOf ... Is TextBox to confirm focus, and use a short Timer to distinguish a single tap from a continuous hold. Example (adapt names to your form):
' put a Timer (Name = tmrHold) on the form, Interval = 300 (ms) and disabled
Private pressedOnce As Boolean
Private Sub Form_Load()
Me.KeyPreview = True
End Sub
Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = Asc("1") Then
If Not (Me.ActiveControl Is Nothing) _
And TypeOf Me.ActiveControl Is TextBox Then
If pressedOnce Then
KeyAscii = 0 ' swallow repeated char
Dim tb As TextBox
Set tb = Me.ActiveControl
tb.Text = "" ' clear
btnAction.SetFocus ' move focus (use your button)
pressedOnce = False
tmrHold.Enabled = False
Else
pressedOnce = True
tmrHold.Enabled = True ' will reset pressedOnce
End If
End If
End If
End Sub
Private Sub tmrHold_Timer()
pressedOnce = False
tmrHold.Enabled = False
End Sub Extra tips: always check If Me.ActiveControl Is Nothing before using it; use TypeOf to avoid treating non-text controls the same; test with numpad keys and use KeyDown with vbKey... constants if you need virtual-key behavior.
Jump to Post— ryan_vietnow 13Look at the tab index property of them at the properties window,ok?
Jump to Post— Jx_Man 987as ryan said look in controls propeties, the minor one of tab index value will get focus.
ie : text1 - tabIndex = 0 -> this control will get focus
text2 - tabIndex = 1 -> focus after text1
....
or see where the cursor flicker when it run.
Jump to Post— ryan_vietnow 13try removing the Exit Sub
Look at the tab index property of them at the properties window,ok?
as ryan said look in controls propeties, the minor one of tab index value will get focus.
ie : text1 - tabIndex = 0 -> this control will get focus
text2 - tabIndex = 1 -> focus after text1
....
or see where the cursor flicker when it run.
Hi ryan & Jx,
I know one of the control properties have tabIndex, I think that is mostly used or control when we press 'Tab' key, isn't it?
Eg: I have two text boxes and command buttons. When I click text1, I want to know that is text1. After that, I will key '1' key in text1 if i press one time. If I press continuously '1' key, I will not show the text in text1 and then do the focus on button1.
My question will confuse to you. I will show my sample code.
Dim PressingCounter As Integer 'Detect
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If (KeyCode = 97) Then
If (Text1.TabIndex = 0) And (PressingCounter > 0) Then
Text1.Text = ""
Command1.SetFocus
PressingCounter = 0
Exit Sub
ElseIf (Text2.TabIndex = 1) And (PressingCounter > 0) Then
Text2.Text = ""
Command2.SetFocus
PressingCounter = 0
Exit Sub
End If
PressingCounter = PressingCounter + 1
End If
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
If (KeyCode = 97) Then
PressingCounter = 0
End If
End Sub
But, I can't sense text2. I think my program is something wrong. Please check it for me.
Thanks,
zawpai
Hi all,
Actually, my program will have many text boxes. That why I want to know where the focus.
If the focus is in text box, my program will need to check whether pressing key is one time or continuous. I will do the other tax while pressing the key continuously and the key in value will not put in text box.
How should I solve that problem.
Best regards,
zawpai
try removing the Exit Sub
below code Return Active control name in "Form1" :
Form1.ActiveControl.Name Hi,
ryan.. Although I remove 'Exit Sub', still can't get the answer that i want. Have you done the test run with my sample code?
Mbt925.. I try to write with 'Form1.ActiveControl.Name', but I get an error. Can you show me some sample code? Because I don't know how to use it.
Thanks,
zawpai
See this sample:
Hi Mbt925,
I have already tested your program. Now, I understand about 'AcitveControl.Name'.
Thanks,
zawpai
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.