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

vb.net 2003 ... Global Formatting ?

P: n/a
the data input app im writing has some 30 + input fields and i want to be
able to format them.

I know i can use the .validate on each textbox and format the 'string'
however this require loads of repetitive codeing. Is there a way possible
to globally format the textboxes both on the .validate function, as well as
when the enter key is pressed.

Any suggestions or examples wouldbe very usefull, as i have a loads of these
data forms to create .. all requiring formatting
as a cheeky footnote before every one shouts 'Character Casing' i am using
the vbProperCase. I have this public fubction, but it strill requites a
huge amount od screen handling code.

Public Function FormatString(ByVal StringtoFormat As String, ByVal
TypeofFormat As Integer) As String
Try
Select Case TypeofFormat
Case ProperCase
FormatString = StrConv(StringtoFormat,
VbStrConv.ProperCase)
Case LowerCasing
Case UpperCasing
End Select
Catch NoFormat As Exception
MsgBox("Unable to format String. Please contatc your systems
administrator", MsgBoxStyle.Exclamation, " Formatting")
FormatString = StringtoFormat
End Try
End Function

Thank you in advance
Oct 9 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
You can have one validate handler for all text boxes assuming that the
formatting is the same for each text box, this should work, i.e.,

Private sub handes_Validating (ByVal source as object, ByVal e as eventargs) _
handles txtbox1.validating, txtbox2.validating, ....., txtbox30.validating
Dim tx as TextBox = DirectCast(source,Textbox)
tx.text = myformatter(tx.text)
End Suib
--
Dennis in Houston
"Peter Newman" wrote:
the data input app im writing has some 30 + input fields and i want to be
able to format them.

I know i can use the .validate on each textbox and format the 'string'
however this require loads of repetitive codeing. Is there a way possible
to globally format the textboxes both on the .validate function, as well as
when the enter key is pressed.

Any suggestions or examples wouldbe very usefull, as i have a loads of these
data forms to create .. all requiring formatting
as a cheeky footnote before every one shouts 'Character Casing' i am using
the vbProperCase. I have this public fubction, but it strill requites a
huge amount od screen handling code.

Public Function FormatString(ByVal StringtoFormat As String, ByVal
TypeofFormat As Integer) As String
Try
Select Case TypeofFormat
Case ProperCase
FormatString = StrConv(StringtoFormat,
VbStrConv.ProperCase)
Case LowerCasing
Case UpperCasing
End Select
Catch NoFormat As Exception
MsgBox("Unable to format String. Please contatc your systems
administrator", MsgBoxStyle.Exclamation, " Formatting")
FormatString = StringtoFormat
End Try
End Function

Thank you in advance
Oct 9 '06 #2

P: n/a
Dennis wrote:
You can have one validate handler for all text boxes assuming that the
formatting is the same for each text box, this should work, i.e.,

Private sub handes_Validating (ByVal source as object, ByVal e as eventargs) _
handles txtbox1.validating, txtbox2.validating, ....., txtbox30.validating
Dim tx as TextBox = DirectCast(source,Textbox)
tx.text = myformatter(tx.text)
End Suib
--
Dennis in Houston
Technically correct, but since Peter seems to strongly dislike
repetitive labor, I'm guessing he'll dislike having to type the
"txtbox1.validating, txtbox2.validating, ....., txtbox30.validating"
part for each form. And remembering to keep it up-to-date whenever he
adds new textboxes.

Peter, the best way is probably to create your own UserControl that
Inherits System.Windows.Forms.TextBox, adds a custom property for your
TextFormat, and automatically catches the Validating event of its base
class and applies the correct formatting. And whatever other
extensions you need to make the perfect TextBox.

If you don't understand what I just said, or if that's just not an
option, here's another way which is a bit of a hack by comparison, but
will do the trick. The idea is to use the Tag property of the textbox
to select the format, and attach your own validator to every textbox on
a form that has a non-empty tag, with just one line of code per form:

1) Include this in the Load event of every Form you want to apply
special TextBox formatting to:

AddMyValidator(Me)

2) Include this in a module:

Sub AddMyValidator(ByVal frm As Object)
' Attaches our custom validator to every textbox on a form that
' has something in the Tag.
Dim tb As TextBox
For Each ctl As Object In frm.Controls
If ctl.GetType.ToString = "System.Windows.Forms.TextBox" Then
tb = DirectCast(ctl, TextBox)
If tb.Tag <"" Then AddHandler tb.Validating, AddressOf
MyValidator
End If
Next
End Sub

Sub MyValidator(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs)
' Custom textbox validator.
Dim tb As TextBox = DirectCast(sender, TextBox)
Select Case tb.Tag
Case "Lower" : tb.Text = Strings.LCase(tb.Text)
Case "Upper" : tb.Text = Strings.UCase(tb.Text)
Case "Proper" ' etc
End Select
End Sub

3) Now set the Tag of the textboxes to the format you want, which can
be done easily and to multiple textboxes at a time.

Oct 10 '06 #3

P: n/a
If the loop/addhandler method is used (and it's a good technique), it needs
to be in recursive form to catch any textboxes that are children of another
control on the form. Do you know of any way this can be done by hooking the
windows messages?
--
Dennis in Houston
"te******@hotmail.com" wrote:
Dennis wrote:
You can have one validate handler for all text boxes assuming that the
formatting is the same for each text box, this should work, i.e.,

Private sub handes_Validating (ByVal source as object, ByVal e as eventargs) _
handles txtbox1.validating, txtbox2.validating, ....., txtbox30.validating
Dim tx as TextBox = DirectCast(source,Textbox)
tx.text = myformatter(tx.text)
End Suib
--
Dennis in Houston

Technically correct, but since Peter seems to strongly dislike
repetitive labor, I'm guessing he'll dislike having to type the
"txtbox1.validating, txtbox2.validating, ....., txtbox30.validating"
part for each form. And remembering to keep it up-to-date whenever he
adds new textboxes.

Peter, the best way is probably to create your own UserControl that
Inherits System.Windows.Forms.TextBox, adds a custom property for your
TextFormat, and automatically catches the Validating event of its base
class and applies the correct formatting. And whatever other
extensions you need to make the perfect TextBox.

If you don't understand what I just said, or if that's just not an
option, here's another way which is a bit of a hack by comparison, but
will do the trick. The idea is to use the Tag property of the textbox
to select the format, and attach your own validator to every textbox on
a form that has a non-empty tag, with just one line of code per form:

1) Include this in the Load event of every Form you want to apply
special TextBox formatting to:

AddMyValidator(Me)

2) Include this in a module:

Sub AddMyValidator(ByVal frm As Object)
' Attaches our custom validator to every textbox on a form that
' has something in the Tag.
Dim tb As TextBox
For Each ctl As Object In frm.Controls
If ctl.GetType.ToString = "System.Windows.Forms.TextBox" Then
tb = DirectCast(ctl, TextBox)
If tb.Tag <"" Then AddHandler tb.Validating, AddressOf
MyValidator
End If
Next
End Sub

Sub MyValidator(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs)
' Custom textbox validator.
Dim tb As TextBox = DirectCast(sender, TextBox)
Select Case tb.Tag
Case "Lower" : tb.Text = Strings.LCase(tb.Text)
Case "Upper" : tb.Text = Strings.UCase(tb.Text)
Case "Proper" ' etc
End Select
End Sub

3) Now set the Tag of the textboxes to the format you want, which can
be done easily and to multiple textboxes at a time.

Oct 10 '06 #4

P: n/a
Dennis wrote:
If the loop/addhandler method is used (and it's a good technique), it needs
to be in recursive form to catch any textboxes that are children of another
control on the form. Do you know of any way this can be done by hooking the
windows messages?
--
Dennis in Houston
Hmm, I didn't think of that. I suppose this is a bit sloppy, but it
does work for any TextBox within a panel or any other Windows container
(not sure about UserControls):

Sub AddMyValidator(ByVal frm As Object)
' Attaches our custom validator to every textbox on a form that
' has something in the Tag.
Dim tb As TextBox
Try
For Each ctl As Object In frm.Controls
If ctl.GetType.ToString = "System.Windows.Forms.TextBox" Then
tb = DirectCast(ctl, TextBox)
If tb.Tag <"" Then AddHandler tb.Validating, AddressOf
MyValidator
Else
AddMyValidator(ctl)
End If
Next
Catch
End Try
End Sub

Oct 10 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.