Results 1 to 13 of 13

Thread: Same Event for Every Textbox!

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2008
    Posts
    111

    Same Event for Every Textbox!

    Hello, i have 48 textbox between tabs and i want to do this
    Code:
    textbox.selstart = 0
    textbox.sellength = len(trim(textbox))
    in the got_focus event. I have to write this 48 times? it there a way to add this event for all textbox on load-time or something? thanks.

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Same Event for Every Textbox!

    If you can convert all of your textboxes to Control Array (same Name but each will have unique Index) then you will be able to do something like this:
    Code:
    Private Sub Text1_GotFocus(Index As Integer)
        Text1(Index).SelStart = 0
        Text1(Index).SelLength = Len(Text1(Index).Text)
    End Sub

  3. #3
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: Same Event for Every Textbox!

    Quote Originally Posted by CVDpr
    Hello, i have 48 textbox between tabs and i want to do this
    Code:
    textbox.selstart = 0
    textbox.sellength = len(trim(textbox))
    in the got_focus event. I have to write this 48 times? it there a way to add this event for all textbox on load-time or something? thanks.
    What do you mean by this? Also, I hope that these 48 TextBoxes are in a Control Array.
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jun 2008
    Posts
    111

    Re: Same Event for Every Textbox!

    If all have the same name, then its gonna be difficult to remember what textbox is for and add the values to a storeprocedure.

    txtResLowLimit
    txtResHighlimit
    txtMax

    etc..

  5. #5
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Same Event for Every Textbox!

    Quote Originally Posted by CVDpr
    If all have the same name, then its gonna be difficult to remember what textbox is for and add the values to a storeprocedure.
    Not really - you can write some name to a Tag property when you create new control - you do that anyway by assigning the Name property so instead you'd use Tag.

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Same Event for Every Textbox!

    Quote Originally Posted by CVDpr
    If all have the same name, then its gonna be difficult to remember what textbox is for
    I agree which is why I never (well, almost never) use control arrays.

    The one, and only, other alternative is to place your code in each individual textbox - all 48 one by each.

    A method I find infinately preferable to using a control array.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jun 2008
    Posts
    111

    Re: Same Event for Every Textbox!

    if i use the tag, how to msgbox the value of the textox that have the tag "low"?
    msgbox Text1.Item(1), this work, but how to use the tagname "low" and not the index number?

  8. #8

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jun 2008
    Posts
    111

    Re: Same Event for Every Textbox!

    Quote Originally Posted by RhinoBull
    If you use control array then you must provide control's Index:
    Code:
    MsgBox Text1(Index).Tag

    but i want the textbox value not the tag value :-(

    textbox1(1) has 125.00 and tag "low"
    i want to msgbox the textbox.text that has the tag "low"
    Last edited by CVDpr; Jan 9th, 2009 at 10:44 AM.

  10. #10
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Same Event for Every Textbox!

    Then use the .Text property (Text1(1).Text)

  11. #11
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Same Event for Every Textbox!

    Quote Originally Posted by CVDpr
    i want to msgbox the textbox.text that has the tag "low"
    Try this then:
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
    Dim myText As String
    
        myText = GetText("Text1", "low")
        MsgBox myText
    
    End Sub
    
    Public Function GetText(myName As String, myTag As String) As String
    Dim txt As Control, myText As String
    
        For Each txt In Me.Controls
            If TypeOf txt Is TextBox And LCase(txt.Name) = LCase(myName) Then
                If LCase(txt.Tag) = LCase(myTag) Then
                    myText = txt.Text
                    Exit For
                End If
            End If
        Next txt
        
        GetText = myText
    
    End Function

  12. #12
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Same Event for Every Textbox!

    Quote Originally Posted by Hack
    I agree which is why I never (well, almost never) use control arrays....
    I'm rather shocked. Why not do this?

    Code:
    Option Explicit
    Private Enum EmpName
        EmpFirstName
        EmpMI
        EmpLastName
    End Enum
    Private Sub Form_Load()
    
        txtName(EmpFirstName) = "marty"
        
    End Sub

  13. #13

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