How can I see if a Control on the form gets the Focus (GotFocus) AND what Control that is (Control.name) ?
Printable View
How can I see if a Control on the form gets the Focus (GotFocus) AND what Control that is (Control.name) ?
Read help on ActiveControl.
you could put code in the gotfocus event of the control you are working with..Quote:
Originally posted by Kars Lensen
How can I see if a Control on the form gets the Focus (GotFocus) AND what Control that is (Control.name) ?
VB Code:
Me.ActiveControl
Depending upon what you are trying to do, you can use this in a global sub...
I use it to change the backcolor of the control which receives focus... ;)
Thanks all, I now I can do it in the Gotfocus event but I wil do that only once in the form (or the project ...), James can you give an example ?
Very simple example :
VB Code:
' ON GOT FOCUS EVENT ON CERTAIN CONTROLS Function YELLOWGOTFOCUS(GETCONTROL As Control) On Error Resume Next If TypeOf GETCONTROL Is TextBox Then GETCONTROL.BackColor = vbYellow GETCONTROL.ForeColor = vbRed GETCONTROL.SelStart = 0 GETCONTROL.SelLength = Len(GETCONTROL) ElseIf TypeOf GETCONTROL Is ComboBox Then GETCONTROL.BackColor = vbYellow GETCONTROL.ForeColor = vbRed End If End Function ' ON LOST FOCUS OF CERTAIN CONTROLS Function WHITELOSTFOCUS(GETCONTROL As Control) If TypeOf GETCONTROL Is TextBox Then GETCONTROL.BackColor = vbWhite GETCONTROL.ForeColor = vbBlack ElseIf TypeOf GETCONTROL Is ComboBox Then GETCONTROL.BackColor = vbWhite GETCONTROL.ForeColor = vbBlack End If End Function
If you add this code to a module, you can access from anywhere inside your project...;)