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!

active control

Status
Not open for further replies.

everytime

Programmer
Feb 21, 2001
31
0
0
GB
Can someone tell me how to get the name of the control that has focus on a form? Thks
 
Hi,

Try something on the lines of:

Private Sub cmdTest_Click()
Dim ctlPrevious As Control
Dim sControlName As String
Set ctlPrevious = Screen.PreviousControl

sControlName = ctlPrevious.NAME

MsgBox sControlName

end sub Hope this is ok. If not, just let me know.

Nick (Everton Rool OK!)
 
thx Nick - I was trying to do a mouseover effect for menus that I could put in a class and so reuse. I got it down now. If you weant the code here it is. Put it in a class module

Option Explicit

Private PreviousControl As String
Private CurrentForm As Form
Private MenuControls() As String
Private MenuItems As Integer
Private MenuFocus() As String

Public Sub SetMouseOver(CurrentControl As String, Optional FocusControl As String, Optional HelpControl As String, Optional HelpText As String)
On Error Resume Next
'clear the board
ClearMenu
'Do what you want to the control that has the mouse over it
With CurrentForm(CurrentControl)
.FontBold = 1
End With
'Set the focus to any passed control
If IsNull(FocusControl) = False Then
CurrentForm(FocusControl).SetFocus
End If
'Set the help text to any passed label
If IsNull(HelpControl) = False Then
CurrentForm(HelpControl).Caption = HelpText
End If
'Add the control to the menu array
AddMenuControl CurrentControl
'Set the previous control
PreviousControl = CurrentControl
End Sub

Public Sub RemoveMouseOver()
'Take off mouseover
ClearMenu
End Sub

Private Sub ClearMenu()
Dim i As Integer

On Error Resume Next
'Loop through all controls and remove any mouseover effect
For i = 0 To (MenuItems - 1)
With CurrentForm(MenuControls(i))
.FontBold = 0
End With
Next i
End Sub

Public Static Property Let ActiveForm(ByVal FormObject As Form)
'Set which is the active form affected
Set CurrentForm = Nothing
Set CurrentForm = FormObject
End Property

Private Sub AddMenuControl(Control As String)
Dim i As Integer
Dim cycleitems As Integer
Dim ExistingControl As Boolean

'Loop through the passed control and add it to this array if it isn't there already
'This array is used to ensure that the mouseover effect is removed from all controls
'That no longer should have the effect
ReDim Preserve MenuControls(MenuItems)
For i = 0 To MenuItems
For cycleitems = 0 To UBound(MenuControls)
If Control = MenuControls(i) Then
'control is already listed
ExistingControl = True
Exit Sub
End If
Next cycleitems
Next i
If ExistingControl = False Then
ReDim Preserve MenuControls(MenuItems)
MenuControls(MenuItems) = Control
MenuItems = MenuItems + 1
End If
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top