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

Recommended Answers

All 9 Replies

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.