Re: Handle Worked - can someone please double check

Tech-Archive recommends: Fix windows errors by optimizing your registry



Miro wrote:

Is this the right way to do this?
.. . .
Private Sub TrimValues(ByVal sender As Object, ByVal e As EventArgs) _
Handles txtOne.Leave, txtTwo.Leave, txtThree.Leave, txtFour.Leave, _
txtFive.Leave, txtSix.Leave, txtSeven.Leave

DirectCast(sender, TextBox).Text _ = Trim(DirectCast(sender, TextBox).Text)
End Sub

That'll do nicely, with just one suggestion:

DirectCast(sender, TextBox).Text _
= Trim(DirectCast(sender, TextBox).Text)

might be clearer as

With DirectCast(sender, TextBox)
.Text = .Text.Trim()
End With

Or, you could go further still and make a custom TextBox class, inherited from TextBox and build this code into an Override for the OnLeave method. That way, you can reuse it in as many Forms/applications as you like - something like

Class TrimmedTextBox
Inherits TextBox

Public Sub New()
MyBase.New()
End Sub

Protected Overrides Sub OnLeave()
' Raise the "Leave" Event
MyBase.OnLeave()

' Force the Text to be trimmed
Me.Text = Me.Text.Trim()

End Sub

End Class

Then replace the TextBox declarations in the "Generated Code" .. er .. code with your custom class.

Replace

Friend WithEvents txtOne As ...TextBox
. . .
txtOne = New ...TextBox

with

Friend WithEvents txtOne As TrimmedTextBox
. . .
txtOne = New TrimmedTextBox

HTH,
Phill W.
.



Relevant Pages

  • Re: Numeric Textbox Code
    ... This textbox checks the entered text upon the user leaving the textbox, ... Public Sub New ... 'This call is required by the Windows Form Designer. ... ' String representation of the RegEx that will be used to ...
    (microsoft.public.dotnet.languages.vb)
  • Re: How To Use The Number Part Of A TextBox Name As A Variable
    ... This requested code would need to detect what the name of the sub is ... The same demo shows how to process any textbox only by supplying its number ... Private Sub TextBox1_Enter' similar ... exiting the control. ...
    (microsoft.public.excel.programming)
  • Re: How To Use The Number Part Of A TextBox Name As A Variable
    ... You don't need to know how many digits ... Private Sub tbx_Change ... The same demo shows how to process any textbox only by supplying its number ... Private Sub TextBox1_Enter' similar ...
    (microsoft.public.excel.programming)
  • Referencing a Subprocedures Name
    ... The trick is to iterate through all the controls, ... Sub PastReply ... checkboxes to the collection that hosts the classes. ... >which is textbox specific I can simply reference the ...
    (microsoft.public.excel.programming)
  • Re: Bookmarks inside text box to insert text or pictures
    ... Sub FillMark ... Dim oRng As Range ... selection.goto method of going to a bookmark couldn't find a bookmark ... selection.goto statement for the bookmark inside the TextBox where I ...
    (microsoft.public.word.vba.general)