|
-
Aug 21st, 2008, 09:54 AM
#1
Thread Starter
Addicted Member
[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.
-
Aug 21st, 2008, 10:07 AM
#2
Hyperactive Member
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
-
Aug 21st, 2008, 10:51 AM
#3
Thread Starter
Addicted Member
Re: [2008] Automatically trim textboxes on enter
Thanks for your reply, but it isn't working. Its seeing my ActiveControl as being my SplitContainer.
-
Aug 21st, 2008, 11:26 AM
#4
Lively Member
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
-
Aug 21st, 2008, 12:09 PM
#5
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)
-
Aug 21st, 2008, 12:14 PM
#6
Hyperactive Member
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
-
Aug 21st, 2008, 01:16 PM
#7
Lively Member
Re: [2008] Automatically trim textboxes on enter
Yikes you're right....I didn't think that one through, obviously!
My bad.
-
Aug 21st, 2008, 05:08 PM
#8
Thread Starter
Addicted Member
Re: [2008] Automatically trim textboxes on enter
 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.
-
Aug 21st, 2008, 05:11 PM
#9
Hyperactive Member
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
-
Aug 22nd, 2008, 03:18 AM
#10
Thread Starter
Addicted Member
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"))
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|