Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GotFocu/LostFocus Question

Status
Not open for further replies.

PaulinKC

Programmer
Apr 2, 2002
50
0
0
US
Visual Basic 6.0 Question:


I know you can do numerous things when a control either gets focus or loses focus. What I am trying to accomplish should be rather simple, but I generally am trying to make things more complicated than it really turns out to be.

What I want to do is instead of doing the following for EVERY single Control on Multiple Forms:

Private Sub txtFirstName_GotFocus()

txtFirstname.Background = vbRed

End Sub

Private Sub txtFirstName_LostFocus()

txtFirstname.Background = vbWhite

End Sub

_______________________________________

I thought about writing a function in a module that would essentially do the same thing, except that I wouldn't have to type the above code for every form.

The module I came up with was:

Public Function CheckFocus(d as control)

If d_GotFocus then

d.BackGround = vbRed

elseif d_LostFocus then

d.BackGround = vbWhite

End If

End Function

I don't think the above function will even work. Does anyone have any ideas or suggestions to accomplish this task?
 
why not use a control array?

that way u can atleast reduce the code...

Known is handfull, Unknown is worldfull
 
I would use a control array, except wouldn't that require me to re-create all controls on all forms?
 
I don't know about using one Function but was able to do it with two..

Public Sub MakeRed(d As Control)
d.BackColor = vbRed
End Sub

Public Sub MakeWhite(d As Control)
d.BackColor = vbWhite
End Sub

Private Sub txtfirstname_GotFocus()
MakeWhite Me.ActiveControl
End Sub

Private Sub txtfirstname_Validate(Cancel As Boolean)
MakeRed Me.ActiveControl
End Sub

Could not use the Lost Focus Event as it would already show the next control as the activecontrol, so I had to move it the Validate event whichs I beleive fires before the loss of focus. Could be wrong, I'm sure if I am, I will be corrected

But it Worked, and while not one function to cover both, if you have many controls it would still cut down on some code...
 
Of course when I say it would reduce code, I'm assume you are going to have a lot more code in the Function other that just changing the backcolor?
 
Interesting approach Quaaazy. I was thinking along those lines, however I was hoping that I could find a way to create a "watch dog" that would initialize on form_load and watch for GotFocus and LostFocus on any control and change the background color accordingly.

Looks like I may actually have to code a few lines for each control.

Thanks for you input though!!
 
Here's an approach you might consider:

Add a Timer control to your form and set its Interval to a very low setting like 1. Then add code similar to the following to its Timer event. The timer maintains in its Tag property the name of the active control and, therefore, when the active control changes, the timer.tag stores the last control with focus.
Code:
Private Sub Timer1_Timer()
  'Do nothing if the active control has not
  'changed since the last call to this event.
  If Timer1.Tag = Me.ActiveControl.Name Then
    Exit Sub
  End If

  Dim C As Control

  'If the last control was a textbox, restore
  'its BackColor to vbWindowBackground.
  If Timer1.Tag <> &quot;&quot; Then
    Set C = Me.Controls(Timer1.Tag)
    If (TypeOf C Is TextBox) Then
      If (C.BackColor <> vbWindowBackground) Then
        C.BackColor = vbWindowBackground
      End If
    End If
  End If

  'Set the timer's tag to the active control.
  Timer1.Tag = Me.ActiveControl.Name

  'If the new control was a textbox, set
  'its BackColor to vbYellow.
  If (TypeOf Me.ActiveControl Is TextBox) Then
    If (Me.ActiveControl.BackColor <> vbYellow) Then
      Me.ActiveControl.BackColor = vbYellow
    End If
  End If
End Sub

I probably need a legal disclaimer for recommending renegade code like this so...buyer beware. [wink]

Good Luck!

Dave
 
>set its Interval to a very low setting like 1

(leading question time)

And the point of this would be?
 
you're cruel

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
StrongM, I'm not sure I understand your question, but doesn't the Interval specify the number of milliseconds between calls to a Timer control's Timer event? And if you want the Timer to continuously monitor which control is active, wouldn't you need to set this interval to be very low?
 
As I said, it's a leading question. There's no real point setting it as low as 1ms because Windows can't actually raise the necessary event that quickly. Under W95/98/Me the best interval you can achieve is about 55ms, and under NT4/2000/XP the best is about 10ms
 
Maybe it 's a silly remark, but if you use this control very much, why not concider creating a user control that you have to program once and you can use it time after time again.

Maybe it's very stupid what I say but that's something that I would think about.
 
strongm,

>There's no real point setting it as low as 1ms

Not true. The default setting is 0 which, if left as is, will never trigger the event. Changing the setting to 1ms will trigger the event as often as the operating system is capable of doing so.
 
Using a timer sound kind of expensive. I think you sould use the GotFocus and LostFocus events. If possible avoid using generic things like Control, use TextBox. Also use the control name (txtMyTextBox) rather than Me.ActiveControl. I think using Me.ActiveControl is why the code did not work in LostFocus (???).

A user control would certainly work, but kind of hard to create in VB 6.0.

Hope this helps.

KCI
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top