?
Solved

Creating a general procedure to set methods for text controls

Posted on 2003-03-28
12
Medium Priority
?
213 Views
Last Modified: 2010-05-01
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!
0
Comment
Question by:Krule
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +3
12 Comments
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 60 total points
ID: 8225294
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
 
LVL 4

Expert Comment

by:trkcorp
ID: 8225295
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
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 8225314
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 6

Expert Comment

by:VK
ID: 8225359
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
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 8225531
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
 
LVL 6

Expert Comment

by:VK
ID: 8225584
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
 
LVL 6

Expert Comment

by:VK
ID: 8225598
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
 
LVL 4

Expert Comment

by:trkcorp
ID: 8226237
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
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 8226555
Krule, what is your feedback on all this?
0
 
LVL 3

Expert Comment

by:y2ksw
ID: 8228068
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
 
LVL 3

Author Comment

by:Krule
ID: 8228237
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
 
LVL 3

Author Comment

by:Krule
ID: 8228281
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

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month14 days, 3 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question