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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need DataBound Masked TextboxUsercontrol

Status
Not open for further replies.

stfarm

Programmer
May 31, 2001
179
CA
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
 
Use a module level DataView declared WithEvents

Private WithEvents mDataView As New DataView

Add a new property to the control


Public Property DataSource() As DataView
Get
Return mDataView
End Get
Set(ByVal Value As DataView)
mDataView = Value
End Set
End Property

In the ListChanged handler the event args properties can be used to iterate through the Data.

Private Sub mDataView_ListChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ListChangedEventArgs) Handles mDataView.ListChanged
'e.ListChangedType()
'e.NewIndex()
'e.OldIndex()
End Sub

hope this helps
regards
steve
 
actually the return value of the datasource property should be IList as follows. the rest of the code is OK.

Public Property DataSource() As IList
Get
Return mDataView
End Get
Set(ByVal Value As IList)
mDataView = CType(Value, DataView)
End Set
End Property
regards
steve
 
Thank you so much, I will check that out in a few. I assume, once I have this code in place, I can use the rugular binding code to display the data, right?

Thanks again,

Steve
 
Yeah, you should be able to bind to anything that implements IList


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top