View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Jacob Skaria Jacob Skaria is offline
external usenet poster
 
Posts: 8,520
Default Can I detect what Control the cursor is at in a form?

Option 1....

Dim ctlTemp As Control
Private Sub CommandButton1_Click()
MsgBox ctlTemp.Name
End Sub
Private Sub TextBox1_Enter()
Set ctlTemp = Me.ActiveControl
End Sub
Private Sub TextBox2_Enter()
Set ctlTemp = Me.ActiveControl
End Sub

Private Sub TextBox3_Enter()
Set ctlTemp = Me.ActiveControl
End Sub

Private Sub TextBox4_Enter()
Set ctlTemp = Me.ActiveControl
End Sub

Private Sub TextBox5_Enter()
Set ctlTemp = Me.ActiveControl
End Sub


Option 2 (will work only for mouse click)

Dim ctlTemp As Control
Private Sub CommandButton1_Click()
MsgBox ctlTemp.Name
End Sub
Private Sub CommandButton1_MouseMove(ByVal Button As Integer, _
ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Set ctlTemp = Me.ActiveControl
End Sub

--
Jacob


"Dreiding" wrote:

I dynamically build a form with textboxes.
Is there a way to detect which text box the cursor is at when a button is
clicked?

Tried and failed with the following code (not a surprise)
Function SelectedControl() as string
Dim ctl As Control
SelectedControl=""
For Each ctl In Me.Controls
If ctl.setfocus = true Then
SelectedControl=ctl.name
exit for
End If
Next ctl
End Sub

I suspect my problem is two-fold.
1. No way to detect current focus (can't read the SetFocus)
2. When the button is clicked, the focus moves..

Any help or suggestions appreciated.
tia, Pat