Automatically Trim Spaces for DataBound TextBoxes

Posted on 2006-04-04
Last Modified: 2012-05-05
What I'd like to do is automatically trim spaces (or "space characters") from all text boxes just before the data is sent to the database.

I can do that with a global TextChanged even handler, but I'd rather do it just once instead of every time the "text changes".

Is there any event I can catch that might give me an opprotunity to trim() all the strings before they go into the DB/DataSet?

Question by:mitchvincent
    1 Comment
    LVL 62

    Accepted Solution

    Hi mitchvincent;

    You can use the Validating event handler to make the change once. It fires just before losing focus.

        Private Sub TextBox_Validating(ByVal sender As Object, _
            ByVal e As System.ComponentModel.CancelEventArgs) Handles _
            TextBox1.Validating, TextBox2.Validating

            Dim tb As TextBox = CType(sender, TextBox)
            tb.Text = tb.Text.Trim()

        End Sub


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now