Hi guys
i have subcalss the textbox so it can actually change the entered no in ####.##.##.## format where # belongs to digit
i have made a function and put it in the OnLostFocus event of my subclassed textbox all is working fine but now i want to use the same formating when someone set the TEXT property of the control
i mean when someone write
Textbox1.text="4444667788" then it instantly changed it to the above format i used the Text property of my subclassed textbox but its giving error...here is my code snippet
ublic Class ctlSpecialHSCodeTextBox
Inherits Windows.Forms.TextBox
Private strText As String
Private blnMsgbox As Boolean 'to stop msgbox fired twice
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
components = New System.ComponentModel.Container()
End Sub
#End Region
Private Function ShowFormatText(ByVal valText As String) As String
''Format for HS code is "0000.00.00.00"
Dim intDecimalPos As Integer
Select Case Len(valText)
Case Is = 0
'do nothing
Return Me.Text
Case Is = 10 'valid length
Me.Text = valText.Substring(0, 4) & "." & valText.Substring(4, 2) & "." & valText.Substring(6, 2) & "." & valText.Substring(8, 2)
Return Me.Text
'valid HSCode after its formatted
Case Is = 13
intDecimalPos = InStr(valText, ".", CompareMethod.Text)
'5th position
If intDecimalPos = 5 Then
'8 position
intDecimalPos += InStr(Mid(valText, (intDecimalPos + 1)), ".", CompareMethod.Text)
If intDecimalPos = 8 Then
'11 position
intDecimalPos += InStr(Mid(valText, (intDecimalPos + 1)), ".", CompareMethod.Text)
If intDecimalPos = 11 Then
'do nothing its valid HSCode
Return Me.Text
Else
If blnMsgbox = False Then
MsgBox("Invalid HS Code", MsgBoxStyle.Information, "HSCode Validation"
blnMsgbox = True
Me.SelectionStart = 0
Me.SelectionLength = Me.Text.Length
Me.Focus()
blnMsgbox = False
End If
End If
Else
If blnMsgbox = False Then
MsgBox("Invalid HS Code", MsgBoxStyle.Information, "HSCode Validation"
blnMsgbox = True
Me.SelectionStart = 0
Me.SelectionLength = Me.Text.Length
Me.Focus()
blnMsgbox = False
End If
End If
Else
If blnMsgbox = False Then
MsgBox("Invalid HS Code", MsgBoxStyle.Information, "HSCode Validation"
blnMsgbox = True
Me.SelectionStart = 0
Me.SelectionLength = Me.Text.Length
Me.Focus()
blnMsgbox = False
End If
End If
Case Else
If blnMsgbox = False Then
MsgBox("Invalid HS Code", MsgBoxStyle.Information, "HSCode Validation"
blnMsgbox = True
Me.SelectionStart = 0
Me.SelectionLength = Me.Text.Length
Me.Focus()
blnMsgbox = False
End If
End Select
End Function
Protected Overrides Sub OnLostFocus(ByVal e As System.EventArgs)
ShowFormatText(Me.Text)
End Sub
Private Sub ctlSpecialHSCodeTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
If Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) And Not Char.IsPunctuation(e.KeyChar) Then
e.Handled = True
End If
End Sub
'This thing giving error
''Public Overrides Property Text() As String
'' Get
'' Return strText
'' End Get
'' Set(ByVal Value As String)
'' If Len(Value) = 10 Or 13 Then
'' strText = ShowFormatText(Value)
'' Else
'' strText = Value
'' End If
'' End Set
''End Property
End Class
Thanks
Nouman
i have subcalss the textbox so it can actually change the entered no in ####.##.##.## format where # belongs to digit
i have made a function and put it in the OnLostFocus event of my subclassed textbox all is working fine but now i want to use the same formating when someone set the TEXT property of the control
i mean when someone write
Textbox1.text="4444667788" then it instantly changed it to the above format i used the Text property of my subclassed textbox but its giving error...here is my code snippet
ublic Class ctlSpecialHSCodeTextBox
Inherits Windows.Forms.TextBox
Private strText As String
Private blnMsgbox As Boolean 'to stop msgbox fired twice
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
components = New System.ComponentModel.Container()
End Sub
#End Region
Private Function ShowFormatText(ByVal valText As String) As String
''Format for HS code is "0000.00.00.00"
Dim intDecimalPos As Integer
Select Case Len(valText)
Case Is = 0
'do nothing
Return Me.Text
Case Is = 10 'valid length
Me.Text = valText.Substring(0, 4) & "." & valText.Substring(4, 2) & "." & valText.Substring(6, 2) & "." & valText.Substring(8, 2)
Return Me.Text
'valid HSCode after its formatted
Case Is = 13
intDecimalPos = InStr(valText, ".", CompareMethod.Text)
'5th position
If intDecimalPos = 5 Then
'8 position
intDecimalPos += InStr(Mid(valText, (intDecimalPos + 1)), ".", CompareMethod.Text)
If intDecimalPos = 8 Then
'11 position
intDecimalPos += InStr(Mid(valText, (intDecimalPos + 1)), ".", CompareMethod.Text)
If intDecimalPos = 11 Then
'do nothing its valid HSCode
Return Me.Text
Else
If blnMsgbox = False Then
MsgBox("Invalid HS Code", MsgBoxStyle.Information, "HSCode Validation"
blnMsgbox = True
Me.SelectionStart = 0
Me.SelectionLength = Me.Text.Length
Me.Focus()
blnMsgbox = False
End If
End If
Else
If blnMsgbox = False Then
MsgBox("Invalid HS Code", MsgBoxStyle.Information, "HSCode Validation"
blnMsgbox = True
Me.SelectionStart = 0
Me.SelectionLength = Me.Text.Length
Me.Focus()
blnMsgbox = False
End If
End If
Else
If blnMsgbox = False Then
MsgBox("Invalid HS Code", MsgBoxStyle.Information, "HSCode Validation"
blnMsgbox = True
Me.SelectionStart = 0
Me.SelectionLength = Me.Text.Length
Me.Focus()
blnMsgbox = False
End If
End If
Case Else
If blnMsgbox = False Then
MsgBox("Invalid HS Code", MsgBoxStyle.Information, "HSCode Validation"
blnMsgbox = True
Me.SelectionStart = 0
Me.SelectionLength = Me.Text.Length
Me.Focus()
blnMsgbox = False
End If
End Select
End Function
Protected Overrides Sub OnLostFocus(ByVal e As System.EventArgs)
ShowFormatText(Me.Text)
End Sub
Private Sub ctlSpecialHSCodeTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
If Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) And Not Char.IsPunctuation(e.KeyChar) Then
e.Handled = True
End If
End Sub
'This thing giving error
''Public Overrides Property Text() As String
'' Get
'' Return strText
'' End Get
'' Set(ByVal Value As String)
'' If Len(Value) = 10 Or 13 Then
'' strText = ShowFormatText(Value)
'' Else
'' strText = Value
'' End If
'' End Set
''End Property
End Class
Thanks
Nouman