Hi everybody,
I have some code for a user control that formats a phone number in a text box nicely.
But I can't databind it.
Could somebody please explain to me how to do that?
Thank you...
Steve
Here is the code I have so far.
Delegate Sub Changed(ByVal sender As Object, ByVal e As EventArgs)
Public Shadows Event TextChanged As Changed
Public Property Value() As String
Get
Return txtPhone.Text.Replace(" ", "").Replace("(", "").Replace(")", "").Replace("-", "")
End Get
Set(ByVal Value As String)
Try
If IsDBNull(Value) Then
txtPhone.Text = ""
Else
If Value.Replace(" ", "").Replace("(", "").Replace(")", "").Replace("-", "").Length > 0 Then
txtPhone.Text = Format(CType(Value.Replace(" ", "").Replace("(", "").Replace(")", "").Replace("-", ""), Long), "(###) ###-####")
Else
txtPhone.Text = ""
End If
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Invalid Phone Number", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly)
End Try
End Set
End Property
Private Sub PTPhone_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.GotFocus
txtPhone.SelectAll()
End Sub
Private Sub mskPhone_GotFocus(ByVal sender As System.Object, ByVal e As System.EventArgs)
txtPhone.SelectAll()
End Sub
Private Sub mskPhone_Change(ByVal sender As System.Object, ByVal e As System.EventArgs)
RaiseEvent TextChanged(sender, e)
End Sub
Private Sub txtPhone_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPhone.LostFocus
Try
If txtPhone.Text.Trim.Length > 0 Then
If txtPhone.Text.Replace(" ", "").Replace("(", "").Replace(")", "").Replace("-", "").Length <> 10 Then
txtPhone.SelectAll()
MessageBox.Show("Please enter a 10 digit phone number.", "Invalid Phone Number", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly)
Else
txtPhone.Text = Format(CType(txtPhone.Text.Replace(" ", "").Replace("(", "").Replace(")", "").Replace("-", ""), Long), "(###) ###-####")
End If
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Invalid Phone Number", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly)
Me.Focus()
End Try
End Sub
Private Sub txtPhone_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPhone.KeyPress
If e.KeyChar.IsDigit(e.KeyChar) = False Then
If e.KeyChar.IsControl(e.KeyChar) = False Then
Select Case e.KeyChar
Case "(", ")", " ", "-"
e.Handled = True
Case Else
e.Handled = False
End Select
End If
End If
End Sub
End Class
I have some code for a user control that formats a phone number in a text box nicely.
But I can't databind it.
Could somebody please explain to me how to do that?
Thank you...
Steve
Here is the code I have so far.
Delegate Sub Changed(ByVal sender As Object, ByVal e As EventArgs)
Public Shadows Event TextChanged As Changed
Public Property Value() As String
Get
Return txtPhone.Text.Replace(" ", "").Replace("(", "").Replace(")", "").Replace("-", "")
End Get
Set(ByVal Value As String)
Try
If IsDBNull(Value) Then
txtPhone.Text = ""
Else
If Value.Replace(" ", "").Replace("(", "").Replace(")", "").Replace("-", "").Length > 0 Then
txtPhone.Text = Format(CType(Value.Replace(" ", "").Replace("(", "").Replace(")", "").Replace("-", ""), Long), "(###) ###-####")
Else
txtPhone.Text = ""
End If
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Invalid Phone Number", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly)
End Try
End Set
End Property
Private Sub PTPhone_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.GotFocus
txtPhone.SelectAll()
End Sub
Private Sub mskPhone_GotFocus(ByVal sender As System.Object, ByVal e As System.EventArgs)
txtPhone.SelectAll()
End Sub
Private Sub mskPhone_Change(ByVal sender As System.Object, ByVal e As System.EventArgs)
RaiseEvent TextChanged(sender, e)
End Sub
Private Sub txtPhone_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPhone.LostFocus
Try
If txtPhone.Text.Trim.Length > 0 Then
If txtPhone.Text.Replace(" ", "").Replace("(", "").Replace(")", "").Replace("-", "").Length <> 10 Then
txtPhone.SelectAll()
MessageBox.Show("Please enter a 10 digit phone number.", "Invalid Phone Number", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly)
Else
txtPhone.Text = Format(CType(txtPhone.Text.Replace(" ", "").Replace("(", "").Replace(")", "").Replace("-", ""), Long), "(###) ###-####")
End If
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Invalid Phone Number", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly)
Me.Focus()
End Try
End Sub
Private Sub txtPhone_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPhone.KeyPress
If e.KeyChar.IsDigit(e.KeyChar) = False Then
If e.KeyChar.IsControl(e.KeyChar) = False Then
Select Case e.KeyChar
Case "(", ")", " ", "-"
e.Handled = True
Case Else
e.Handled = False
End Select
End If
End If
End Sub
End Class