Results 1 to 10 of 10

Thread: [2008] Automatically trim textboxes on enter

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Location
    Manchester, England
    Posts
    255

    [2008] Automatically trim textboxes on enter

    Hello,
    Does anybody have an efficient, reusable, method of trimming the content of TextBoxes and Combos when the control has been entered?
    Other than creating a single sub to handle the .Enter for each control, checking to see if the control has the focus, and then trimming it, I can't think of a good way of just trimming the content of whichever control has the focus.
    At home - VB.NET 2005/2008 Express, Visual Web Developer 2005 Express
    At work - VS 2008 Standard (VB)
    .NET 2.0/3.5


    Visual Studio Express Learning Centre | How do I videos | MSDN VB Express Forum | MSDN VB Developer Centre

  2. #2
    Hyperactive Member gnaver's Avatar
    Join Date
    Jul 2005
    Location
    Denmark/Sweden
    Posts
    289

    Re: [2008] Automatically trim textboxes on enter

    you could do something like this, dont know if its the best way, but it works...

    in the form_keydown event put this

    Code:
     If e.KeyCode = Keys.Enter Then
                If TypeOf Me.ActiveControl Is TextBox Then
                    DirectCast(Me.ActiveControl, TextBox).Text = DirectCast(Me.ActiveControl, TextBox).Text.Trim()
                End If
            End If
    remember to turn keypreview to true in the forms properties

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Location
    Manchester, England
    Posts
    255

    Re: [2008] Automatically trim textboxes on enter

    Thanks for your reply, but it isn't working. Its seeing my ActiveControl as being my SplitContainer.
    At home - VB.NET 2005/2008 Express, Visual Web Developer 2005 Express
    At work - VS 2008 Standard (VB)
    .NET 2.0/3.5


    Visual Studio Express Learning Centre | How do I videos | MSDN VB Express Forum | MSDN VB Developer Centre

  4. #4
    Lively Member
    Join Date
    Jul 2008
    Posts
    89

    Re: [2008] Automatically trim textboxes on enter

    you could create a custom control and inherit from the textbox and then add code to the keydown event in your custom control:

    Code:
    If e.KeyCode = Keys.Enter Then
       me.text = me.text.trim
    end if

  5. #5
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: [2008] Automatically trim textboxes on enter

    Yikes, I wouldn't be doing it that way unless you want the end user to never keep a space they typed in.

    Use the TB's Enter event for this, never the KeyDown, KeyPress and/or KeyUp events.

    Also you could use the TB's Validate event, which wouldn't let them leave the TB until the criteria's been met (in this case there's no criteria)
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  6. #6
    Hyperactive Member mulhearn22's Avatar
    Join Date
    Jun 2007
    Location
    Cherry Hill, NJ
    Posts
    347

    Re: [2008] Automatically trim textboxes on enter

    Don't you want the content trimmed on the control's exit rather than enter? What good does an enter fire do you? You can create a custom sub that handles this and then on the form load, simply scroll through the controls and add the handler. I used this method once so textbox's background color would change one enter/exit. That way I only had one sub controlling every textbox!
    VS 2010 / .NET 4.0 / ASP.NET 4.0

  7. #7
    Lively Member
    Join Date
    Jul 2008
    Posts
    89

    Re: [2008] Automatically trim textboxes on enter

    Yikes you're right....I didn't think that one through, obviously!

    My bad.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Location
    Manchester, England
    Posts
    255

    Re: [2008] Automatically trim textboxes on enter

    Quote Originally Posted by mulhearn22
    Don't you want the content trimmed on the control's exit rather than enter? What good does an enter fire do you?
    No, it needs to be on enter because the data is coming from an Informix database which simply pads out the text with spaces up to the maximum number of characters allowed in the column - even though I trim it before writing to the database.

    Because of this, I'm finding that when I tab through each TextBox, the text AND the trailing spaces are highlighted. I just want to trim the trailing spaces, and highlight the text as its more user friendly that way.
    At home - VB.NET 2005/2008 Express, Visual Web Developer 2005 Express
    At work - VS 2008 Standard (VB)
    .NET 2.0/3.5


    Visual Studio Express Learning Centre | How do I videos | MSDN VB Express Forum | MSDN VB Developer Centre

  9. #9
    Hyperactive Member mulhearn22's Avatar
    Join Date
    Jun 2007
    Location
    Cherry Hill, NJ
    Posts
    347

    Re: [2008] Automatically trim textboxes on enter

    Well then you should really have the trim function occur when you are populating the textbox.
    VS 2010 / .NET 4.0 / ASP.NET 4.0

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Location
    Manchester, England
    Posts
    255

    Re: [2008] Automatically trim textboxes on enter

    D'oh! Trust me to over-complicate things!!
    OK - is it possible to bind, and trim at the same time? I'm using this at the moment:
    Code:
    Me.txtShipCode.DataBindings.Add(New Binding("Text", myBind, "ship_key"))
    At home - VB.NET 2005/2008 Express, Visual Web Developer 2005 Express
    At work - VS 2008 Standard (VB)
    .NET 2.0/3.5


    Visual Studio Express Learning Centre | How do I videos | MSDN VB Express Forum | MSDN VB Developer Centre

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