By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,132 Members | 1,260 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,132 IT Pros & Developers. It's quick & easy.

TextBox Trim question

P: n/a
It is hard for me to imagine a time when you would not want to have the data
in a textbox control trimmed. I am hoping there is some sort of switch that
can be set for all text boxes so that when the user leaves the text box it
gets trimmed. Is there such a thing. If not, does this mean I need to
write some sort of trimming code for each text box I place on a Winform. If
so, what is the simplest and easiest way for me to see to it that all text
boxes get trimmed?

Nov 20 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
AFAIK, there's not a built in method for this but you could easily derive a
control and when text is 'set', make sure you trim it in there.
"Woody Splawn" <wo***@splawns.com> wrote in message
news:OY**************@TK2MSFTNGP11.phx.gbl...
It is hard for me to imagine a time when you would not want to have the data in a textbox control trimmed. I am hoping there is some sort of switch that can be set for all text boxes so that when the user leaves the text box it
gets trimmed. Is there such a thing. If not, does this mean I need to
write some sort of trimming code for each text box I place on a Winform. If so, what is the simplest and easiest way for me to see to it that all text
boxes get trimmed?

Nov 20 '05 #2

P: n/a
Cor
Hi Woody,

Two days ago someone did ask something, that did look strange in all the
answer that have been given here, but when we did something (not one person
but more persons) in this newsgroup it did work and it is quiet normal that
it does work.

I thought with that I can do in future something.
Now you are asking for something I thought that I could, so I did make it.

Try it, I thought that it does what you ask

I hope this was what you where looking for?

Cor

\\\
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim txtboxarea As TextBox() = New TextBox() {TextBox1, TextBox2}
'In the array are all the textboxes you want to trim that you made in design
time
For Each txt As TextBox In txtboxarea
AddHandler txt.LostFocus, AddressOf TextBox_LostFocus
Next
End Sub
Private Sub TextBox_LostFocus(ByVal sender As Object, _
ByVal e As System.EventArgs)
DirectCast(sender, TextBox).Text = _
Trim(DirectCast(sender, TextBox).Text)
End Sub
///



It is hard for me to imagine a time when you would not want to have the data in a textbox control trimmed. I am hoping there is some sort of switch that can be set for all text boxes so that when the user leaves the text box it
gets trimmed. Is there such a thing. If not, does this mean I need to
write some sort of trimming code for each text box I place on a Winform. If so, what is the simplest and easiest way for me to see to it that all text
boxes get trimmed?

Nov 20 '05 #3

P: n/a
"Woody Splawn" <wo***@splawns.com> schrieb
It is hard for me to imagine a time when you would not want to have
the data in a textbox control trimmed. I am hoping there is some
sort of switch that can be set for all text boxes so that when the
user leaves the text box it gets trimmed. Is there such a thing. If
not, does this mean I need to write some sort of trimming code for
each text box I place on a Winform. If so, what is the simplest and
easiest way for me to see to it that all text boxes get trimmed?


solution 1: write a derived Textbox that trims in the leave event.
solution 2: handle the leave event in the same event handler for all
textboxes.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #4

P: n/a
* "Woody Splawn" <wo***@splawns.com> scripsit:
It is hard for me to imagine a time when you would not want to have the data
in a textbox control trimmed. I am hoping there is some sort of switch that
can be set for all text boxes so that when the user leaves the text box it
gets trimmed. Is there such a thing. If not, does this mean I need to
write some sort of trimming code for each text box I place on a Winform. If
so, what is the simplest and easiest way for me to see to it that all text
boxes get trimmed?


You can create an inherited control (inheriting from
'System.Windows.Forms.TextBox' which handles its 'Validating' event and
trims its contents there. Then you will have to use the new textbox
instead of the standard Windows Forms textbox.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #5

P: n/a
Cor
Hi Herfried,

Did you look at my solution I think it is wonderful?

Cor
You can create an inherited control (inheriting from
'System.Windows.Forms.TextBox' which handles its 'Validating' event and
trims its contents there. Then you will have to use the new textbox
instead of the standard Windows Forms textbox.

Nov 20 '05 #6

P: n/a
HI Woody,

Did you have any concern on this issue?
If so please post in the newsgroup.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 20 '05 #7

P: n/a
HI Woody,

Did you have any concern on this issue?
If so please post in the newsgroup.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.