Generic trim code

W

Woody Splawn

Could someone give me a suggestion for writing generic code in a validating
event for trimming the value of a textbox entry?

I thought something like the following might work but it does not.

Dim txt As TextBox = DirectCast(sender, TextBox)
txt.Text = Trim(txt.Text)

I get an invalid cast error
 
A

Armin Zingler

Woody Splawn said:
Could someone give me a suggestion for writing generic code in a
validating event for trimming the value of a textbox entry?

I thought something like the following might work but it does not.

Dim txt As TextBox = DirectCast(sender, TextBox)
txt.Text = Trim(txt.Text)

I get an invalid cast error

Insert
msgbox (sender.gettype.fullname)
as the first line in the event handler. What is displayed?
 
P

Peter Huang

Hi Woody,

I agree with Armin's suggestion.

Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
MsgBox(sender.GetType.FullName) 'Is this an TextBox?
Dim txt As TextBox = DirectCast(sender, TextBox)
MsgBox(Len(txt.Text))
txt.Text = Trim(txt.Text)
MsgBox(Len(txt.Text))
End Sub

You may try my code, if this does not work for you, please post your code
here.(You may need to simplify your code as long as it can reproduce the
problem.

If you have any concern on this issue, please post here.

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.
 
J

Jay B. Harlow [MVP - Outlook]

Woody,
In addition to Armin's comments.

Is your validating event handler handling only TextBox.Validating events or
do you have other controls also being handled by the event handler? I would
consider NOT having different control types handled by the same validating
event handler, as that event handler may need "unneeded" conditional logic.

You can use the TypeOf expression to ensure you have a TextBox control.

If TypeOf sender Is TextBox Then
Dim txt As TextBox = DirectCast(sender, TextBox)
txt.Text = Trim(txt.Text)
End If

Hope this helps
Jay
 
C

Cor

Hi Woody,

I did give you that in the other tread something wrong with it?

Cor
Could someone give me a suggestion for writing generic code in a validating
event for trimming the value of a textbox entry?

I thought something like the following might work but it does not.
Cor
 
W

Woody Splawn

I did look at your code and tried to adopt parts of it but without success.
I did not try to adopt it as a whole because I did not see the benefit of
having it in the load event of a form. My reasoning is this. The changes
will be made after the form is loaded and therefore the trimming needs to be
done after the data is entered and not before. Maybe I missed something in
what you were trying to say but the way you said it was just difficult
enough for me to understand that I put it off.

Hope you're well.

Woody
 
C

Cor

Hi Woody,

And it is so nice for your problem, I am really proud on it.
I tell you line by line what it does.

\\\
-Private Sub Form1_Load(ByVal sender As Object, _
-ByVal e As System.EventArgs) Handles MyBase.Load

It has to be somewhere in the initfase but after the initializing the form
and before showing
I use always the Mybase.load for it, because that is just before the loading
of the form.

-Dim txtboxarea As TextBox() = New TextBox() {TextBox1, TextBox2}
An array of all textboxes on your form you want to trim, only needed to add
the handler to the textbox.

-For Each txt As TextBox In txtboxarea
- AddHandler txt.LostFocus, AddressOf TextBox_LostFocus
-Next
End Sub

Setting the events with the same name for all textbox you want to trim when
they lose focus

Private Sub TextBox_LostFocus(ByVal sender As Object, _
ByVal e As System.EventArgs)
DirectCast(sender, TextBox).Text = _
Trim(DirectCast(sender, TextBox).Text)
End Sub

This is the trim Sub itself

///

And this has to be only on one place in a form class.
(It is even to do with everywhere "control" instead of "textbox", because
all controls have a text property, but I did not wanted to make the sample
to difficult for you)

Cor
 
W

Woody Splawn

msgbox (sender.gettype.fullname)
as the first line in the event handler. What is displayed?

Your question was insightfull. I'm sorry. I am still new to all of this
and sometimes overlook the fundamentals. The problem is that I am using a
textbox control provided by Infragistics. The code below works as expected:

Dim txt As UltraTextEditor = DirectCast(sender, UltraTextEditor)
txt.Text = Trim(txt.Text)

Would I be too forward if I make an observation? I realize that I am new to
the VS arena but I have been doing this kind of thing for a long time. One
of my frustrations is that VS does not come complete for someone who just
wants to get some work done and does not have a degree in computer science.
I mean, it is not possible for me to get anything done quickly without
having a third party tool (like Infragistics) to supply what I had hoped was
in the VS IDE to begin with. True, if I were a big I.T. department I could
duplicate much of what a tool like this does but I don't have the time or
resources to do it from scratch.

The same thing is true of the Report Writer Microsoft furnishes with VS. I
tried hard for an extended period of time but could not get Crystal Reports,
or more directly, support at Crystal reports to work with me. Finally went
to ActiveReports with which I have been pleased.

Bottom line, though I am not complaining and wouldn't change, VS in it's
present incarnation is incomplete for my needs. Wish it did more things
like lookup capabilities for fields in grids etc., then I wouldn't have to
use third party products and ask questions that are so dumb. The reason I
am using VS is not because it makes things easier. It is because of
market-share, market-share, market-share. I am not even interested in the
net. I want a client/server tool that I and my clients can be sure will be
around tomorrow.

Just one small guy's opinion.
 
P

Peter Huang

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top