Creating a general procedure to set methods for text controls

I'm attempting to create a general procedure to reduce the redundancy of some code I have.

On every text box I have I have code that looks similar to this:

Private Sub txtPassword_GotFocus()
'Select previous text so it will be overwritten.
    txtPassword.SelStart = 0
    txtPassword.SelLength = Len(txtPassword)
End Sub

What I'm trying to do is create a general procedure so I can have something like

Private Sub txtPassword_GotFocus()
    selText(txtPassword)
End Sub

Or anything similar to that. Thanks!
LVL 3
KruleAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
Use this:

Private Sub Text1_GotFocus()
    Call SelText(Text1)
End Sub

Private Sub Text2_GotFocus()
    Call SelText(Text2)
End Sub

Private Sub SelText(ByRef ptxtTextbox As TextBox)
    With ptxtTextbox
        .SelStart = 0
        .SelLength = Len(.Text)
    End With
End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
trkcorpCommented:
Place a routine like this in a module:

Public Sub SelectTxt(tb As TextBox)
tb.SelStart = 0
tb.SelLength = Len(tb)
End Sub

Then in say, got_focus, all you have to do is:

call SelectTxt(me.activecontrol)
0
Arthur_WoodCommented:
trkcorp, that will ONLY work IF (big IF) the activecontrol is a TextBox....otherwise you will generate a run-time TYPE MISMATCH error.

AW
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

VKCommented:
Public Function SelectText(Optional theControl As Object = Nothing, Optional Message As Boolean = False) As Boolean
    SelectText = False
    If theControl Is Nothing Then Set theControl = Screen.ActiveControl
    With theControl
        Select Case True
            Case theControl Is Nothing
                If Message Then MsgBox "There is no active form, which consists of a control that can get the focus.", vbMsgBoxSetForeground + vbSystemModal
            Case Not theControl.Visible
                If Message Then MsgBox "The control '" & theControl.Name & "' couldn't get the focus because it is invisible!", vbMsgBoxSetForeground + vbSystemModal
            Case Not theControl.Enabled
                If Message Then MsgBox "The control '" & theControl.Name & "' couldn't get the focus because it is disabled!", vbMsgBoxSetForeground + vbSystemModal
            Case Trim(theControl.Text) = ""
            Case Else
                theControl.SelStart = 0
                theControl.SelLength = Len(theControl.Text)
                SelectText = True
        End Select
    End With
End Function

V.K.
0
Arthur_WoodCommented:
VK, that will also throw a runtime error if a control that DOES NOT have a .TEXT property is passed  to it, or if the control DOES NOT support the .SelStart and .SelLength properties.

This may be fine, if the user only applies this to TEXTBOXES, but should be a little more defensive, in case someone ses this code, here, and tries to use it in a different context.  Defensive programming is an approach that we should be enbcouraging, to help new or inexperienced programmer write better code.

AW
0
VKCommented:
Public Function SelectText(Optional theControl As Object = Nothing, Optional Message As Boolean = False) As Boolean
    On Error Resume Next
    SelectText = False
    If theControl Is Nothing Then Set theControl = Screen.ActiveControl
    With theControl
        Select Case True
            Case theControl Is Nothing
                If Message Then MsgBox "There is no active form, which consists of a control that can get the focus.", vbMsgBoxSetForeground + vbSystemModal
            Case Not theControl.Visible
                If Message Then MsgBox "The control '" & theControl.Name & "' couldn't get the focus because it is invisible!", vbMsgBoxSetForeground + vbSystemModal
            Case Not theControl.Enabled
                If Message Then MsgBox "The control '" & theControl.Name & "' couldn't get the focus because it is disabled!", vbMsgBoxSetForeground + vbSystemModal
            Case Trim(theControl.Text) = ""
            Case Else
                theControl.SelStart = 0
                theControl.SelLength = Len(theControl.Text)
                SelectText = True
        End Select
    End With
End Function
0
VKCommented:
OR even better

Public Function SelectText(Optional theControl As Object = Nothing, Optional Message As Boolean = False) As Boolean
    On Error Resume Next
    SelectText = False
    If theControl Is Nothing Then Set theControl = Screen.ActiveControl
    With theControl
        Select Case True
            Case theControl Is Nothing
                If Message Then MsgBox "There is no active form, which consists of a control that can get the focus.", vbMsgBoxSetForeground + vbSystemModal
            Case Not theControl.Visible
                If Message Then MsgBox "The control '" & theControl.Name & "' couldn't get the focus because it is invisible!", vbMsgBoxSetForeground + vbSystemModal
            Case Not theControl.Enabled
                If Message Then MsgBox "The control '" & theControl.Name & "' couldn't get the focus because it is disabled!", vbMsgBoxSetForeground + vbSystemModal
            Case Trim(theControl.Text) = ""
            Case Else
                theControl.SelStart = 0
                theControl.SelLength = Len(theControl.Text)
                If Err = 0 Then SelectText = True
        End Select
    End With
End Function
0
trkcorpCommented:
Yes, Arthur, I know.  It does illustrate the technique though and 99% of the time a text box is what will be addressed in this fashion.  Why would we call a routine with a text box as an input parameter from any place other than a text box?  Answer: We wouldn't if we have any sense.
0
Éric MoreauSenior .Net ConsultantCommented:
Krule, what is your feedback on all this?
0
y2kswCommented:
Krule is off and away... :-))

You could subclass your form(s) and check for the WM_SETFOCUS message. Then you scan all control's hWnd (generic) and once found you send back the respective messages for SelStart and SelLenght. A little bit of work I left over :-) This would avoid you to write on each form for each control a specific line of code, and it works with Combo's and other text-like controls, too (Richtext, ecc). All the one's which don't, will not generate a VB runtime error.
0
KruleAuthor Commented:
Wow..that was effective.

I went to school after posting my question, I'm going to give them all a try and see which one i like most.

So far I like emoreau's approach if it works, I was sort of looking for just that, if it works I think it's what I want.
0
KruleAuthor Commented:
This is what I was looking for, just a way to reduce the redundant amount of code in my work. The procedure will never be called outside of a TextBox_GotFocus action, so there's no worry.

Having a way to just automatically do this for all textboxes without writting subs for each of them would be golden, but alot more work than this is worth :p

Thanks all!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.