|
-
Jun 30th, 2005, 07:50 AM
#1
Thread Starter
Fanatic Member
Generic Setfocus [Resolved]
HI,
I'm trying to write a generic set focus sub/function but am having trouble handling control arrays i.e. option list can anyone see how this can be handled or if this can be actually be done?
This is what I have so far; when I pass in the name of my option array it goes into the case else saying the control name is "object"
VB Code:
Private Sub GenericSetFocus(sCtrlName As String, Optional lStart As Long = 0)
On Error GoTo ErrorHandler
With Controls(sCtrlName)
Select Case TypeName(Controls(sCtrlName))
Case "TextBox"
.SelStart = lStart
.SelLength = Len(.Text)
.SetFocus
Case "CheckBox", "CommandButton", "ComboBox"
.SetFocus
Case "ListBox"
' Need to look into dropping focus back onto specific item in list
.SetFocus
Case "OptionButton"
' Need to look into index issue here if a control array which it should be
' how to specify item to select by default
.SetFocus
Case Else
MsgBox "This control hasn't been handled yet: " & TypeName(Controls(sCtrlName))
End Select
End With
Exit Sub
ErrorHandler:
If Err.Number <> 0 Then
MsgBox Err.Number & vbNewLine & _
Err.Description
End If
End Sub
Private Sub cmdValidate_Click()
'GenericSetFocus "txtTesting", 2
'GenericSetFocus "cmdValidate"
'GenericSetFocus "option"
GenericSetFocus CStr(Me.optMyOptions(0).Name)
'GenericSetFocus "chk1"
'GenericSetFocus "cbo1"
'GenericSetFocus "lst1"
End Sub
Last edited by aconybeare; Jul 1st, 2005 at 03:25 AM.
Reason: Resolved
-
Jun 30th, 2005, 08:12 AM
#2
Re: Generic Setfocus
This code works for both arrays and normal controls, and does nothing if it can't set the focus at all.
See how ugly VB looks without Try/Catch blocks 
VB Code:
Private Sub GenericSetFocus(ByVal pstrControlName As String, _
Optional ByVal plngIndex As Long = 0)
Dim lngTemp As Long
On Error GoTo CatchNonArray
lngTemp = Controls(pstrControlName).ubound
' Still here, must be an array then:
Controls(pstrControlName).Item(plngIndex).SetFocus
Exit Sub
CatchNonArray:
' Not an array, try to set focus directly
On Error GoTo CatchNoSetFocus
Controls(pstrControlName).SetFocus
Exit Sub
CatchNoSetFocus:
End Sub
-
Jun 30th, 2005, 08:31 AM
#3
Thread Starter
Fanatic Member
Re: Generic Setfocus
Nice one Penagate Thanks a bunch
Cheers Al
-
Jun 30th, 2005, 08:36 AM
#4
Thread Starter
Fanatic Member
Re: Generic Setfocus [Resolved]
Penagate,
One thing though what if I want the text selected i.e. in a text box or in a mulitline text box a block of text selected?
I original function handled text boxes by taking a start position, but would be better if it could take an end position too
Cheers Al
-
Jun 30th, 2005, 08:49 AM
#5
Re: Generic Setfocus
VB Code:
Private Sub GenericSetFocus(ByVal pstrControlName As String, _
Optional ByVal plngIndex As Long = 0, _
Optional ByVal plngSelStart As Long = -1, _
Optional ByVal plngSelLength As Long = 0)
Dim objControl As Control
On Error GoTo CatchNonArray
' Try control array:
Set objControl = Controls(pstrControlName).Item(plngIndex)
GoTo TrySetFocus
CatchNonArray:
' Not a control array:
Set objControl = Controls(pstrControlName)
TrySetFocus:
On Error GoTo CatchNoSetFocus
' Try to set focus
With objControl
.SetFocus
If (TypeOf objControl Is TextBox) Then
If (plngSelStart >= 0) Then
.SelStart = plngSelStart
.SelLength = plngSelLength
End If
End If
End With
CatchNoSetFocus:
End Sub
I'm sure there's a better way then all those Gotos, but it's the snappiest I could come up with
-
Jun 30th, 2005, 10:06 AM
#6
Thread Starter
Fanatic Member
-
Jun 30th, 2005, 10:34 AM
#7
Re: Generic Setfocus
Just wondering why you chose to pass the control name to the function rather than a reference to the control? There would then be no need to worry about control arrays.
Also, before calling SetFocus make sure the control is Visible and Enabled or an error will occur.
-
Jun 30th, 2005, 11:00 AM
#8
Thread Starter
Fanatic Member
Re: Generic Setfocus
Brucevde,
I did try that initially but couldn't get it to work so in the interests of moving things along I changed to passing the control name. If you think it'll be faster or a better way of doing it then maybe I should reconsider my approach
Thanks I'll add in a check for enabled and visible too, nice one
Cheers Al
-
Jun 30th, 2005, 12:41 PM
#9
Re: Generic Setfocus
Oh I would have preferred just this three line function. Setus the focus if possible otherwise does nothing:
VB Code:
Private Sub GenericSetFocus(Ctrl As Control, Optional lStart As Long = 0)
On Error Resume Next
With Ctrl
.SelStart = lStart
.SelLength = Len(.Text)
.SetFocus
End With
End Sub
Pradeep
-
Jun 30th, 2005, 12:48 PM
#10
Re: Generic Setfocus
I would use something like
VB Code:
'in a code module
'Public so all Forms can call this procedure
Public Sub GenericSetFocus(ByRef pobjControl As Control, _
Optional ByVal plngSelStart As Long = -1, _
Optional ByVal plngSelLength As Long = 0)
On Error GoTo ErrorHandler
With pobjControl
If (.Visible And .Enabled) Then
.SetFocus
End If
If (TypeOf pobjControl Is TextBox) Then
If (plngSelStart >= 0) Then
.SelStart = plngSelStart
.SelLength = plngSelLength
End If
End If
End With
ExitPoint:
Exit Sub
ErrorHandler:
Select Case Err.Number
Case 438 '438 = Object doesn't support this property or method
'do nothing
Case Else
'Do Something
End Select
Resume ExitPoint
End Sub
'call GenericSetFocus
GenericSetFocus Me.Combo1
GenericSetFocus Me.Text1,0,5
-
Jul 1st, 2005, 03:20 AM
#11
Thread Starter
Fanatic Member
Re: Generic Setfocus
Pradeep & Bruce,
Thanks a lot, those are a big help
Cheers Al
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|