Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim str1 As String = TextBox1.Text
Dim strFormatted As String = ""
strFormatted = digitsOnly(str1)
checkNumberLength(strFormatted)
TextBox1.Text = strFormatted
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim strFormatted As String = "("
TextBox1.Text = digitsOnly(TextBox1.Text)
checkNumberLength(TextBox1.Text)
Dim x As Integer = 1
For Each c As String In TextBox1.Text
If x = 4 Then
strFormatted += ") "
End If
If x = 7 Then
strFormatted += "-"
End If
strFormatted += c
x += 1
Next
TextBox1.Text = strFormatted
End Sub
Private Function digitsOnly(ByVal str As String) As String
Dim strFormatted As String = ""
For Each c As String In str
If Char.IsDigit(c) Then
strFormatted += c
End If
Next
Return strFormatted
End Function
Private Sub checkNumberLength(ByVal num As String)
If num.Length <> 10 Then
MsgBox("Sorry, you need 10 numbers")
End If
End Sub
End Class
Monday, April 4, 2011
Visual Basic Methods
The following is an example of how to use a sub and a function in order to reuse code.
Labels:
methods,
visual basic
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment