Results 1 to 11 of 11

Thread: Generic Setfocus [Resolved]

  1. #1

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Resolved 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:
    1. Private Sub GenericSetFocus(sCtrlName As String, Optional lStart As Long = 0)
    2.     On Error GoTo ErrorHandler
    3.     With Controls(sCtrlName)
    4.     Select Case TypeName(Controls(sCtrlName))
    5.         Case "TextBox"
    6.             .SelStart = lStart
    7.             .SelLength = Len(.Text)
    8.             .SetFocus
    9.        
    10.         Case "CheckBox", "CommandButton", "ComboBox"
    11.             .SetFocus
    12.        
    13.         Case "ListBox"
    14.             ' Need to look into dropping focus back onto specific item in list
    15.             .SetFocus
    16.            
    17.         Case "OptionButton"
    18.             ' Need to look into index issue here if a control array which it should be
    19.             ' how to specify item to select by default
    20.             .SetFocus
    21.            
    22.         Case Else
    23.             MsgBox "This control hasn't been handled yet: " & TypeName(Controls(sCtrlName))
    24.     End Select
    25.     End With
    26. Exit Sub
    27. ErrorHandler:
    28.     If Err.Number <> 0 Then
    29.         MsgBox Err.Number & vbNewLine & _
    30.                 Err.Description
    31.     End If
    32. End Sub
    33.  
    34. Private Sub cmdValidate_Click()
    35.  
    36.     'GenericSetFocus "txtTesting", 2
    37.     'GenericSetFocus "cmdValidate"
    38.     'GenericSetFocus "option"
    39.     GenericSetFocus CStr(Me.optMyOptions(0).Name)
    40.     'GenericSetFocus "chk1"
    41.     'GenericSetFocus "cbo1"
    42.     'GenericSetFocus "lst1"
    43. End Sub
    Last edited by aconybeare; Jul 1st, 2005 at 03:25 AM. Reason: Resolved

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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:
    1. Private Sub GenericSetFocus(ByVal pstrControlName As String, _
    2.                             Optional ByVal plngIndex As Long = 0)
    3. Dim lngTemp As Long
    4. On Error GoTo CatchNonArray
    5.     lngTemp = Controls(pstrControlName).ubound
    6.     ' Still here, must be an array then:
    7.     Controls(pstrControlName).Item(plngIndex).SetFocus
    8.     Exit Sub
    9. CatchNonArray:
    10.     ' Not an array, try to set focus directly
    11. On Error GoTo CatchNoSetFocus
    12.     Controls(pstrControlName).SetFocus
    13.     Exit Sub
    14. CatchNoSetFocus:
    15. End Sub

  3. #3

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Re: Generic Setfocus

    Nice one Penagate Thanks a bunch

    Cheers Al

  4. #4

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    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

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Generic Setfocus

    VB Code:
    1. Private Sub GenericSetFocus(ByVal pstrControlName As String, _
    2.                             Optional ByVal plngIndex As Long = 0, _
    3.                             Optional ByVal plngSelStart As Long = -1, _
    4.                             Optional ByVal plngSelLength As Long = 0)
    5.     Dim objControl As Control
    6.    
    7. On Error GoTo CatchNonArray
    8.     ' Try control array:
    9.     Set objControl = Controls(pstrControlName).Item(plngIndex)
    10.     GoTo TrySetFocus
    11.    
    12. CatchNonArray:
    13.     ' Not a control array:
    14.     Set objControl = Controls(pstrControlName)
    15.    
    16. TrySetFocus:
    17. On Error GoTo CatchNoSetFocus
    18.     ' Try to set focus
    19.     With objControl
    20.         .SetFocus
    21.         If (TypeOf objControl Is TextBox) Then
    22.             If (plngSelStart >= 0) Then
    23.                 .SelStart = plngSelStart
    24.                 .SelLength = plngSelLength
    25.             End If
    26.         End If
    27.     End With
    28. CatchNoSetFocus:
    29. End Sub
    I'm sure there's a better way then all those Gotos, but it's the snappiest I could come up with

  6. #6

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Re: Generic Setfocus

    Spot on thanks!

  7. #7
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    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.

  8. #8

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    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

  9. #9
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Generic Setfocus

    Oh I would have preferred just this three line function. Setus the focus if possible otherwise does nothing:

    VB Code:
    1. Private Sub GenericSetFocus(Ctrl As Control, Optional lStart As Long = 0)
    2.     On Error Resume Next
    3.     With Ctrl
    4.         .SelStart = lStart
    5.         .SelLength = Len(.Text)
    6.         .SetFocus
    7.     End With
    8. End Sub

    Pradeep
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  10. #10
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Generic Setfocus

    I would use something like

    VB Code:
    1. 'in a code module
    2. 'Public so all Forms can call this procedure
    3. Public Sub GenericSetFocus(ByRef pobjControl As Control, _
    4.                             Optional ByVal plngSelStart As Long = -1, _
    5.                             Optional ByVal plngSelLength As Long = 0)
    6.  
    7. On Error GoTo ErrorHandler
    8.  
    9.     With pobjControl
    10.         If (.Visible And .Enabled) Then
    11.             .SetFocus
    12.         End If
    13.  
    14.         If (TypeOf pobjControl Is TextBox) Then
    15.             If (plngSelStart >= 0) Then
    16.                 .SelStart = plngSelStart
    17.                 .SelLength = plngSelLength
    18.             End If
    19.         End If
    20.     End With
    21.  
    22. ExitPoint:
    23.    Exit Sub
    24.  
    25. ErrorHandler:
    26.     Select Case Err.Number
    27.         Case 438 '438 = Object doesn't support this property or method
    28.             'do nothing
    29.         Case Else
    30.             'Do Something
    31.     End Select
    32.    Resume ExitPoint
    33. End Sub
    34.  
    35. 'call GenericSetFocus
    36. GenericSetFocus Me.Combo1
    37. GenericSetFocus Me.Text1,0,5

  11. #11

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    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
  •  



Click Here to Expand Forum to Full Width