|
-
Apr 2nd, 2003, 10:32 AM
#1
Thread Starter
Frenzied Member
How to determine what control has focus
Is there an easy way to check to see what control on a form has focus and lost focus?
Example:
I have a form with 10 textboxes.
in each textbox "GotFocus" event I have this code..
(keep in mind the "text." name changes in each event)
VB Code:
text.backcolor = vbred
doevents
and in each textbox "LostFocus" event I have this code...
VB Code:
text.backcolor = vbwhite
doevents
I would like to have 2 events like ControlGotFoucs and ControlLostFocus and use some type of case statment (or something else) to change the backcolor for the controls...
Is this possible or do I need to code each and every controls events?
Thanks for the info,
Rudy
10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".
-
Apr 2nd, 2003, 10:34 AM
#2
Retired VBF Adm1nistrator
If you use a Control Array you could just use one copy of the code.
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Apr 2nd, 2003, 10:35 AM
#3
Lively Member
If you make a control array of all your textboxes you would have only one event proc!
(plenderj replied faster: go get a live )
-
Apr 2nd, 2003, 10:48 AM
#4
Retired VBF Adm1nistrator
Originally posted by blaff
(plenderj replied faster: go get a live )
:P
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Apr 2nd, 2003, 10:52 AM
#5
Thread Starter
Frenzied Member
Yea, a control array would be great but this project has way to many controls to go back and change them all to an array. I was hoping for some kind of an event that is triggered when a control gets/looses focus other than the one that I mentioned above.
Rudy
10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".
-
Apr 2nd, 2003, 11:06 AM
#6
Frenzied Member
It's not hard to add them all to a control array:- Select one of the TextBoxes (the "first" one, if you care about the order of their indexes)
- Give it a more generic name that is appropriate for all the textboxes (like instead of txtFirstName, make it txtCustInfo)
- Copy that name from the Property window
- With the Name property for the TextBox control still highlighted in the Property window, select the next TextBox and Paste (Ctrl+V)
- Say "Yes" to the "do you want to make a control array" dialog (only happens once)
- Then just continue with the rest of the textboxes, selecting a textbox and pasting.
Should take less than a minute for 100 controls (depending on how long it takes you to Left Click, Ctrl+V)
The more painful part would be if you have more specific code written already for data validation that references the TextBoxes by name (instead of index). This would require module level text replacement (Replace all instances of txtFirstName with txtCustInfo(0), for each TextBox).
-
Apr 2nd, 2003, 11:07 AM
#7
Frenzied Member
You could install a thread local WH_CBT hook and trap for the hcbt_focuschanged which is triggered whenever focus shifts from one control to another in your app.
-
Apr 2nd, 2003, 11:07 AM
#8
Lively Member
You can make your own textbox control, which makes its own bkg red after getting the focus.
But this way you would have to replace all textboxes with your own textbox...
-
Apr 2nd, 2003, 11:12 AM
#9
Frenzied Member
To do the WH_CBT thing with the EventVB.dll:
VB Code:
Option Explicit
Dim WithEvents vbLink As EventVB.APIFunctions
Dim WithEvents vbHook As EventVB.ApiSystemHook
Private Sub Form_Load()
Set vbLink = New APIFunctions
Set vbHook = vbLink.System.Hooks
vbHook.StartHook WH_CBT, HOOK_LOCAL_PROCESS
End Sub
Private Sub vbHook_BeforeFocusChanged(ByVal GainFocus As EventVB.ApiWindow, ByVal LoseFocus As EventVB.ApiWindow, Cancel As Boolean)
Debug.Print "Window " & GainFocus.hWnd & " gains focus from " & LoseFocus.hWnd
End Sub
-
Apr 2nd, 2003, 11:53 AM
#10
Thread Starter
Frenzied Member
Originally posted by MerrionComputin
To do the WH_CBT thing with the EventVB.dll:
VB Code:
Option Explicit
Dim WithEvents vbLink As EventVB.APIFunctions
Dim WithEvents vbHook As EventVB.ApiSystemHook
Private Sub Form_Load()
Set vbLink = New APIFunctions
Set vbHook = vbLink.System.Hooks
vbHook.StartHook WH_CBT, HOOK_LOCAL_PROCESS
End Sub
Private Sub vbHook_BeforeFocusChanged(ByVal GainFocus As EventVB.ApiWindow, ByVal LoseFocus As EventVB.ApiWindow, Cancel As Boolean)
Debug.Print "Window " & GainFocus.hWnd & " gains focus from " & LoseFocus.hWnd
End Sub
MerrionComputin - This is what I was looking for... Thanks! I will try this out and see how it turns out.
Thanks guys! -
Rudy
10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".
-
Apr 2nd, 2003, 12:27 PM
#11
Thread Starter
Frenzied Member
MerrionComputin,
I got the dll, looks nice. I have a couple of questions..
when exiting the program I need to add the folowing code Correct?
VB Code:
Set vbHook = Nothing
Set vbLink = Nothing
Next,
How do I translate that number (GainFocus.hWnd and LooseFocus.hWnd) into the control name?
Thanks for the help,
Rudy
10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".
-
Apr 2nd, 2003, 12:39 PM
#12
Here's a non-API solution which uses some basic Class functionality:
Class Module: (clsTextbox)
VB Code:
Option Explicit
Private WithEvents moTextBox As TextBox
Private moBoxes As clsTextBoxes
Private Sub Class_Terminate()
UnBindTextBox
End Sub
Friend Sub BindTextBox(ByRef TextBoxControl As TextBox, ByRef Boxes As clsTextBoxes)
Set moBoxes = Boxes
Set moTextBox = TextBoxControl
End Sub
Friend Sub UnBindTextBox()
Set moTextBox = Nothing
Set moBoxes = Nothing
End Sub
Private Sub moTextBox_GotFocus()
moBoxes.Fire_GotFocus moTextBox
End Sub
Private Sub moTextBox_LostFocus()
moBoxes.Fire_LostFocus moTextBox
End Sub
Class Module: (clsTextBoxes)
VB Code:
Option Explicit
Public Event GotFocus(ByRef TextBoxControl As TextBox)
Public Event LostFocus(ByRef TextBoxControl As TextBox)
Private moBoxes As Collection
Private Sub Class_Initialize()
Set moBoxes = New Collection
End Sub
Private Sub Class_Terminate()
Dim oBox As clsTextBox
For Each oBox In moBoxes
oBox.UnBindTextBox
Next
Set moBoxes = Nothing
End Sub
Public Sub Add(ByRef TextBoxControl As TextBox)
Dim oBox As clsTextBox
Set oBox = New clsTextBox
oBox.BindTextBox TextBoxControl, Me
moBoxes.Add oBox
End Sub
Friend Sub Fire_GotFocus(ByRef TextBoxControl As TextBox)
RaiseEvent GotFocus(TextBoxControl)
End Sub
Friend Sub Fire_LostFocus(ByRef TextBoxControl As TextBox)
RaiseEvent LostFocus(TextBoxControl)
End Sub
Example Usage:
VB Code:
Option Explicit
Private WithEvents TextBoxes As clsTextBoxes
Private Sub Form_Load()
Dim oControl As Control
Set TextBoxes = New clsTextBoxes
For Each oControl In Me
If TypeOf oControl Is TextBox Then
TextBoxes.Add oControl
End If
Next
End Sub
Private Sub Form_Unload(Cancel As Integer)
Set TextBoxes = Nothing
End Sub
Private Sub TextBoxes_GotFocus(TextBoxControl As TextBox)
TextBoxControl.BackColor = vbRed
End Sub
Private Sub TextBoxes_LostFocus(TextBoxControl As TextBox)
TextBoxControl.BackColor = vbWindowBackground
End Sub
-
Apr 2nd, 2003, 01:10 PM
#13
Thread Starter
Frenzied Member
Arron,
That was very good. It worked perfectly for what I needed.
Thanks a million!
Rudy
10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".
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
|