Handle Worked - can someone please double check

M

Miro

I wanted certain text boxes ( only certain ones ) to always be Trim'd so
that spaces are not in the begining,
nor the end of the text entered.

I created my own "Handle ?" - i hope thats the right terminology,
and it works. Is this the right way to do this?
I had to use DirectCast to get the textbox name.

Private Sub TrimValues(ByVal sender As System.Object, ByVal e As
EventArgs) _
Handles txtOne.Leave, txtTwo.Leave, txtThree.Leave, txtFour.Leave, _
txtFive.Leave, txtSix.Leave, txtSeven.Leave

DirectCast(sender, TextBox).Text = Trim(DirectCast(sender,
TextBox).Text)

End Sub

Thanks,

Miro
 
C

Cor Ligthert [MVP]

Miro,

Using VB.Net, there are more roads that leads to Rome, but there is in my
idea nothing wrong with your solution.

You can use as well a kind of generic setting of the handle in by instance
the load form event

\\\
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
doset(Me)
End Sub
Private Sub doSet(ByVal parentCtr As Control)
Dim ctr As Control
For Each ctr In parentCtr.Controls
if TypeOf ctr Is Textbox then
AddHandler ctr.LostFocus, AddressOf TrimValues
doSet(ctr)
Next
End Sub
////

And then your procedure.

This is a recursive method, because the textboxes in by instance a groupbox
have to be handled as well.

I hope this helps a little bit?

Cor
 
M

Miro

It does,
1. That the first is ok
2. That I can go thru what you have given me as the second option.

Slowely this vb stuff is starting ot make sence :)

Thank you very much for the help!!
I appreciate it.

Miro
 
J

jeff

And another path to rome ...

If you want to reproduce this functionality on other forms ... ie. trim text
in text boxes ... you will need to duplicate this code on each form.
cut/paste/find and replace.

For reuseability ... create your own class - myTextBox - that inherits from
the base class - Windows.Forms.TextBox ... and override / extend the leave
event.

This way, you can drop the myTextBox on the form ... you are off to the
races ... no need to code anything on the form ... and if you ever need to
change the behavior of the trim ... you change it in one place ... done.

---------------------------------------------

Imports System.Windows.Forms

Namespace myControls

Public Class myTextBox

Inherits System.Windows.Forms.TextBox

Protected Overrides Sub OnLeave(ByVal e As System.EventArgs)

MyBase.OnLeave(e)
Me.Text = Trim(Me.Text)

End Sub

End Class

End Namespace

---------------------------------------------

Now, if you want to extend this a little further ... you could include a
property ... _TrimText ... set this in the designer whenever you want to
trim the spaces... This way you can always use the myTextBox control, and
set the property accordingly. Reason, if you add a text control to a
form...do your stuff with it ... coding, setting properties and so on ...
then realize you need to 'trim the text' ... you do not have to delete the
control, add your myTextBox, code it ... configure it ... again. If you
used your base control - myTextBox - from the begining, the TrimText is now
a property, set it, have it - done.

-------------------------------------
Namespace GlobalContainer

Imports System.Windows.Forms

Public Class myTextBox
Inherits System.Windows.Forms.TextBox

Private _TrimText As Boolean

<System.ComponentModel.Description("Do you want to trim the
textbox."),
System.ComponentModel.Category("Behavior")> _
Public Property TrimText() As Boolean
Get
Return _TrimText
End Get
Set(ByVal value As Boolean)
_TrimText = value
End Set
End Property

Protected Overrides Sub OnLeave(ByVal e As System.EventArgs)
MyBase.OnLeave(e)

If TrimText Then
Call ueTrimText()
End If

End Sub

Public Sub ueTrimText()

Me.Text = Trim(Me.Text)

End Sub

End Class
End Namespace
 
P

Phill W.

Miro said:
Is this the right way to do this? .. . .
Private Sub TrimValues(ByVal sender As Object, ByVal e As EventArgs) _
Handles txtOne.Leave, txtTwo.Leave, txtThree.Leave, txtFour.Leave, _
txtFive.Leave, txtSix.Leave, txtSeven.Leave

DirectCast(sender, TextBox).Text _
= Trim(DirectCast(sender, TextBox).Text)
End Sub

That'll do nicely, with just one suggestion:

DirectCast(sender, TextBox).Text _
= Trim(DirectCast(sender, TextBox).Text)

might be clearer as

With DirectCast(sender, TextBox)
.Text = .Text.Trim()
End With

Or, you could go further still and make a custom TextBox class,
inherited from TextBox and build this code into an Override for the
OnLeave method. That way, you can reuse it in as many
Forms/applications as you like - something like

Class TrimmedTextBox
Inherits TextBox

Public Sub New()
MyBase.New()
End Sub

Protected Overrides Sub OnLeave()
' Raise the "Leave" Event
MyBase.OnLeave()

' Force the Text to be trimmed
Me.Text = Me.Text.Trim()

End Sub

End Class

Then replace the TextBox declarations in the "Generated Code" .. er ..
code with your custom class.

Replace

Friend WithEvents txtOne As ...TextBox
. . .
txtOne = New ...TextBox

with

Friend WithEvents txtOne As TrimmedTextBox
. . .
txtOne = New TrimmedTextBox

HTH,
Phill W.
 
M

Miro

I never knew you could put "custom properties" into vb.net forms designer.
I really like that option.

I will try that tonight once I get home from work.
I might be responding here, if I get stuck and might need a little help.
All depends on what google returns.

I have read up on the MyBase stuff but that still confuses me a bit. I'll
let that sink in - in a couple days and retry
to read that up.

Thank you.

Miro
 
J

jeff

all the myBase does ... is ensure the ancestor's event is called ... before
your override is called...

look at inheritance.

Jeff.
 
M

Miro

Jeff,

Im a bit stumped. - I was wondering if you can help me out just a bit
further. Im on the edge of the cliff, I just need
a bit more of a push. :)
Ive never done anything like this before so I may be just searching for the
wrong thing in help.

The closest I have found a walkthru on how to do this is in the msdn

Walkthrough: Authoring a User Control with Visual Basic .NET
ms-help://MS.VSCC/MS.MSDNVS/vbcon/html/vbconWalkthroughCreatingCompositeWFCControl.htm

All examples show how to make a brand new custom control. Is that what your
example shows?
I understood your example as the TextBox given by the forms designer was to
get the _TrimText property somehow.

I took the code you supplied and made a new module / .vb or whatever it is
that it is called, and added that to my project.
I did have to change one thing in your code, I had to move this " Imports
System.Windows.Forms " before the namespace command. It didnt like it
after,

But I cant figure out what to do with this now. How do I get the custom
property to show up in the windows form designer.

Ive googled as well, and in that case all I found was an example like this:
http://www.codeproject.com/cs/combobox/betterchecked.asp
http://www.learn247.net/weRock247/labs/winforms/vb/section_4.htm
but i dont think thats what im looking for either.

I think i just need a walkthru or something of this one example linke or
something and then I can go on creating custom controls everywhere :)

Cause if this works - which what you did is open a whole new world, as I
understand it, Instead of having a command button that has an Icon on it,
and coding everywhere the OnEnter, OnLeave and OnClick to change the ICON
picture, I can create 3 additional properties that hold pictures / icons and
then write a handler to reference those, instead of having the same /
simillar code written over and over again changing the icon everywhere. -
But thats another newsgroup question - if and when i get stuck on that. :)

Thank you for your time so far.

Miro
 
M

Miro

I think I got it Jeff.

Here is what I had to do. I was using a vb.net ( pre 2003 ) version.
Old company I worked for gave it to me as a parting gift cause they dropped
it.
But for what I am learning, I uninstalled it, and downloaded vb.net 2005
express and now I see the
"myTextBox" in my own components toolbar.

If i put it on a form I can see the Trim Text property there. The icon on
the text box looks like a gear.

Im assuming this is what you had in mind?

Miro
 
J

jeff

yup.

That is what I expected ... Sorry, I am new to VB and VB.Net (just started
using it about 6 months ago) and I am not sure what is specific to VS 2005.
That is all I have ... never used other versions.

As you can see, the custom class (user control) will appear in your toolbox
and from there, you can drag and drop it on to other controls.

As for you next question ... button with picture box ... yes this is a
perfect example for using inheritance ...

Lets says you have two types of buttons you want to do this (onLever ,
onEnter, onClick) with ... an OK button and a CANCEL button.

First,
1. Create a base myCommandButton ... inherited from CommandButtons.
2. Create the necessary properties ... icon file or whatever ...
3. Code / Extend the events accordingly ... on enter ... on leave ... on
click ...

Create a new class myOKButton ... that inhertis from myCommandButton

1. Set it's custom ICON properties to what you want for OK buttons.
2. Now, when you want to use an OK button ... you are done. Drag it, drop
it on to your other controls.
3. You do not need to worry about setting its custom properties ...
4. Now if you ever want to change the settings for you OK button throughout
your application, you change your 'myOKButton' ...done.

Create a new class myCANCELButton ... that inherits from myCommandButton ...

1. repeat process ...

Hope this helps ...

Jeff.
 
M

Miro

It does help greatly.

Im new to vb as well, and I am glad I updated to the 2005 ver. Even if it
is express. It is way better, and handles some objects better. Plus now
when I go search on google, I know everything will work.

Thank you for all your help.

Two last quick questions.
1. In the code - where you define the "Namespace" . Is there a list of
"reserved" namespaces or can I choose any word I want for a namespace.

2. I am supposing I should keep the myTrimText.vb file with the property
setting on 'Compile' ?

Ive been searching google with "inheritance" but things come up that I'm not
really looking for.

Thank you again for all ur help.

Miro
 

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

Top