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.
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
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.
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..
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.
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.
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?
Re: Same Event for Every Textbox!
If you use control array then you must provide control's Index:
Code:
MsgBox Text1(Index).Tag
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"
Re: Same Event for Every Textbox!
Then use the .Text property (Text1(1).Text)
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
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
Re: Same Event for Every Textbox!
Not a bad idea, Marty. :thumb: