One handler for multiple controls' got focus/lost focus events - basic.visual
This is a discussion on One handler for multiple controls' got focus/lost focus events - basic.visual ; Hello, all, I'm not sure there is an easy way around this but I
thought I'd ask first before doing it the hard way.
I have a VB6 app for data-entry that has dozens of forms and thousands
of controls, ...
-
One handler for multiple controls' got focus/lost focus events
Hello, all, I'm not sure there is an easy way around this but I
thought I'd ask first before doing it the hard way.
I have a VB6 app for data-entry that has dozens of forms and thousands
of controls, most of which are textboxes. What I have decided to do
is to add color to the program to aid visual identification of the
active control and readability. What I've done for the initial testing
is to put code on one form, in each textbox control's GotFocus and
LostFocus event. This, of course, works great, but it's very time
consuming to enter the code and then to set the control's name like
this:
Private Sub Text1_GotFocus()
If Not ChangingDataYN Then
Text1.BackColor = vbRed
End If
End Sub
Private Sub Text1_LostFocus()
If Not ChangingDataYN Then
Text1.BackColor = vbWhite
End If
End Sub
I check to see that I'm not doing the change programatically
(populating the data at form load) and then change the color (to other
colors than what I've listed here, I just used those for brevity).
I have to enter this code and then modify it for each control in two
places. What I would like to know is if there is a way to do this
with one routine for each event? I can find which control has the
focus with Form1.ActiveControl but I do not know how to find out that
the form's focus has *changed* from one control to another. And, if I
knew that the focus changed I would still not know from which control
the focus came (which form lost focus) so I'd still be in a pickle.
I cannot just put all the controls on a form in to one control array
since that would break all my code to read, set, and verify data
entered or displayed in the controls.
My only other thought is to write a program to rip through my frm
files and create code segments for each control, making a sub for
GotFocus and LostFocus. Not fun, but better than doing it manually.
Any suggestions before I do this the hard way?
Thank you for your time.
--HC
-
Re: One handler for multiple controls' got focus/lost focus events
HC <hboothe@gte.net> wrote:
>I have a VB6 app for data-entry that has dozens of forms and thousands
>of controls, most of which are textboxes. What I have decided to do
>is to add color to the program to aid visual identification of the
>active control and readability.
How about something like this. Create a class (called clsColorChanger
here):
Option Explicit
Public WithEvents TargetTextBox As TextBox
Private Sub TargetTextBox_GotFocus()
TargetTextBox.BackColor = vbRed
End Sub
Private Sub TargetTextBox_LostFocus()
TargetTextBox.BackColor = vbWindowBackground
End Sub
Then, in your form, do something like this:
Option Explicit
Private m_ColorChangers As Collection
Private Sub Form_Load()
Set m_ColorChangers = New Collection
Dim obj As clsColorChanger
Dim ctrl As Variant
For Each ctrl In Me.Controls
If TypeOf ctrl Is TextBox Then
Set obj = New clsColorChanger
Set obj.TargetTextBox = ctrl
m_ColorChangers.Add obj
End If
Next
End Sub
--
--------- Scott Seligman <scott at <firstname> and michelle dot net> ---------
Nothing is so permanent as a temporary solution.
-- Ahmad Chalabi
-
Re: One handler for multiple controls' got focus/lost focus events
On Mar 7, 7:37 pm, "Scott Seligman" <selig...@example.com> wrote:
> HC <hboo...@gte.net> wrote:
> >I have a VB6 app for data-entry that has dozens of forms and thousands
> >of controls, most of which are textboxes. What I have decided to do
> >is to add color to the program to aid visual identification of the
> >active control and readability.
>
> How about something like this. Create a class (called clsColorChanger
> here):
>
> Option Explicit
>
> Public WithEvents TargetTextBox As TextBox
>
> Private Sub TargetTextBox_GotFocus()
> TargetTextBox.BackColor = vbRed
> End Sub
>
> Private Sub TargetTextBox_LostFocus()
> TargetTextBox.BackColor = vbWindowBackground
> End Sub
>
> Then, in your form, do something like this:
>
> Option Explicit
>
> Private m_ColorChangers As Collection
>
> Private Sub Form_Load()
> Set m_ColorChangers = New Collection
> Dim obj As clsColorChanger
> Dim ctrl As Variant
> For Each ctrl In Me.Controls
> If TypeOf ctrl Is TextBox Then
> Set obj = New clsColorChanger
> Set obj.TargetTextBox = ctrl
> m_ColorChangers.Add obj
> End If
> Next
> End Sub
>
> --
> --------- Scott Seligman <scott at <firstname> and michelle dot net> ---------
> Nothing is so permanent as a temporary solution.
> -- Ahmad Chalabi
Scott, thank you for your reply.
I have tested this in a separate app (I hate introducing trial code to
my large production app) and it works freakin' great. I'm afraid I'm
going to have to read it several more times (and do that when it's not
at the end of the day) to maybe understand it: I don't usually do much
with classes. :-/
I have one form in my big app where I had started doing it (the
GotFocus and LostFocus changes) manually so I could preview it for a
couple of clients; tomorrow I will start yanking that code and replace
it with yours to see how that goes. I will let you know.
Thank you very much, I think this is going to save me tons of time and
aggravation.
--HC
-
Re: One handler for multiple controls' got focus/lost focus events
On Mar 9, 10:55 pm, HC <hboo...@gte.net> wrote:
> On Mar 7, 7:37 pm, "Scott Seligman" <selig...@example.com> wrote:
>
>
>
> > HC <hboo...@gte.net> wrote:
> > >I have a VB6 app for data-entry that has dozens of forms and thousands
> > >of controls, most of which are textboxes. What I have decided to do
> > >is to add color to the program to aid visual identification of the
> > >active control and readability.
>
> > How about something like this. Create a class (called clsColorChanger
> > here):
>
> > Option Explicit
>
> > Public WithEvents TargetTextBox As TextBox
>
> > Private Sub TargetTextBox_GotFocus()
> > TargetTextBox.BackColor = vbRed
> > End Sub
>
> > Private Sub TargetTextBox_LostFocus()
> > TargetTextBox.BackColor = vbWindowBackground
> > End Sub
>
> > Then, in your form, do something like this:
>
> > Option Explicit
>
> > Private m_ColorChangers As Collection
>
> > Private Sub Form_Load()
> > Set m_ColorChangers = New Collection
> > Dim obj As clsColorChanger
> > Dim ctrl As Variant
> > For Each ctrl In Me.Controls
> > If TypeOf ctrl Is TextBox Then
> > Set obj = New clsColorChanger
> > Set obj.TargetTextBox = ctrl
> > m_ColorChangers.Add obj
> > End If
> > Next
> > End Sub
>
> > --
> > --------- Scott Seligman <scott at <firstname> and michelle dot net> ---------
> > Nothing is so permanent as a temporary solution.
> > -- Ahmad Chalabi
>
> Scott, thank you for your reply.
>
> I have tested this in a separate app (I hate introducing trial code to
> my large production app) and it works freakin' great. I'm afraid I'm
> going to have to read it several more times (and do that when it's not
> at the end of the day) to maybe understand it: I don't usually do much
> with classes. :-/
>
> I have one form in my big app where I had started doing it (the
> GotFocus and LostFocus changes) manually so I could preview it for a
> couple of clients; tomorrow I will start yanking that code and replace
> it with yours to see how that goes. I will let you know.
>
> Thank you very much, I think this is going to save me tons of time and
> aggravation.
>
> --HC
I started to add this to my app and tested it on one form. The
problem is that it ate itself when it ran across a control array (of
which I have many in my app). I am looking through code to get around
having common handlers for control arrays but have not found a
suitable answer yet. I am leaving town for a few days so my time will
be limited but I will continue to work on that angle. I'll let you
know when I have it solved. Thanks again.
--HC