Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formatting Text: Should I be using Modules??? 2

Status
Not open for further replies.

wearytraveller

Programmer
Jul 9, 2005
5
GB
Hello my again, In my MDI application I have several forms and many text boxes on each form. I have been wondering how to ensure that data entered into a text box is correctly formatted, here is my answer;

Private Sub txtContactName_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtContactName.Leave

Dim MyString As String
Dim TrimString As String
Dim newstring As String

'Check that data has been entered in ContactName field, if there is then,
'Remove any spaces at the start of the text string in txtContactName.text, then
'convert text in field to proper case ie STREET/street to Street...
If txtContactName.Text <> "" Then
MyString = txtContactName.Text
TrimString = LTrim(MyString)
txtContactName.Text = TrimString
newstring = StrConv(TrimString, VbStrConv.ProperCase)
txtContactName.Text = newstring
Else
'If there is no entry in this field show message...
Me.DialogResult = DialogResult.None
Beep()
StatusBar1.Text = "Mandatory Field: You must enter Contact Name"

'Set the focus to Address...
txtContactName.Focus()

End If

End Sub


This works well BUT I have had to write this for most of the text boxes can I use a module to do this and reduce the amount of code I have to write??
 
Personally -

I would create a Public Function in a Module (there are many who would disagree thread796-1081338)

However:

the following is typed not tested

Public Function ValidateTextBox (ByRef ATextBox as TextBox) As Boolean

If ATextBox.Text.Trim = "" Then
Return False
Else
ATextBox.Text = ATextBox.Text.Trim
Return True
End If
End Function

and call it with

Private Sub TextBoxName_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBoxName.Leave

If Not ValidateTextBox(TextBoxName) Then
Messagebox.Show(etc. etc.)
TextBoxName.Focus
End If

End Sub


I'm sure you will also receive some Class based suggestions as well.

Hope this helps.
 
The same could be done in a class:
Code:
Public Class ValidationFunctions

 Public Shared Function ValidateTextBox (ByRef ATextBox as TextBox) As Boolean

  If ATextBox.Text.Trim = "" Then
    Return False
  Else
    ATextBox.Text = ATextBox.Text.Trim
    Return True
  End If
End Function

end class

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top