473,396 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Generic trim code

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
Nov 20 '05 #1
8 1381
"Woody Splawn" <wo***@splawns.com> schrieb
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?
--
Armin

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

Nov 20 '05 #2
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.

Nov 20 '05 #3
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
"Woody Splawn" <wo***@splawns.com> wrote in message
news:Oq*************@TK2MSFTNGP12.phx.gbl... 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

Nov 20 '05 #4
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
Nov 20 '05 #5
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

Nov 20 '05 #6
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
Nov 20 '05 #7
>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.



Nov 20 '05 #8
Hi Woody,

You may take a look at the website below.
There are some samples.
http://www.gotdotnet.com/

Here is a book will be a good start for you.
Applied Microsoft .NET Framework Programming
by Jeffrey Richter (Author)
http://www.amazon.com/exec/obidos/tg...552087-6994412
?v=glance
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 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Carlos Ribeiro | last post by:
Hello all, While studying some of the templating systems available for Python, I've come up with an idea to implement templates using decorators. The following code snippet runs in Python 2.3; a...
22
by: Simon | last post by:
Hi, I have written a function to trim char *, but I have been told that my way could be dangerous and that I should use memmove(...) instead. but I am not sure why my code could be 'dangerous'...
2
by: orekinbck | last post by:
Hi There I am probably missing something fundamental here, but I cannot see a method to search the values of a generic dictionary so that I can find the key ? Of course I could enumerate...
11
by: Darren Anderson | last post by:
I have a function that I've tried using in an if then statement and I've found that no matter how much reworking I do with the code, the expected result is incorrect. the code: If Not...
22
by: Terry Olsen | last post by:
I have an app that makes decisions based on string content. I need to make sure that a string does not contain only spaces or newlines. I am using the syntax 'Trim(String)" and it works fine. I...
31
by: rkk | last post by:
Hi, I've written a small trim function to trim away the whitespaces in a given string. It works well with solaris forte cc compiler, but on mingw/cygwin gcc it isn't. Here is the code: char...
3
by: =?Utf-8?B?Sm9obiBB?= | last post by:
Hi all, below at the bottom are 2 nearly identical overloads for the same method. As you can see the algorithm is exactly the same. However try as I may I cannot seem too create a usefull...
2
by: ilitirit | last post by:
Can anyone explain why the following program doesn't work? The attributes and elements of the MessageList class are not being generated. Am I doing something incorrectly? Or if this is a bug...
14
by: Ron | last post by:
I am trying to build a generic static method that will return a specified type from a method. My problem is that I need to return say an image from the method...and I'm having trouble...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.