Link to home
Start Free TrialLog in
Avatar of Krule
KruleFlag for Canada

asked on

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!
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of trkcorp
trkcorp

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)
trkcorp, that will ONLY work IF (big IF) the activecontrol is a TextBox....otherwise you will generate a run-time TYPE MISMATCH error.

AW
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.
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
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
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
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.
Krule, what is your feedback on all this?
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.
Avatar of Krule

ASKER

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.
Avatar of Krule

ASKER

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!