TextBox Trim question

  • Thread starter Thread starter Woody Splawn
  • Start date Start date
W

Woody Splawn

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?
 
Woody Splawn said:
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.
 
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.
 
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
///
 
* "Woody Splawn said:
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.
 
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.
 
Back
Top