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

Need Help in TextBox Subclassing? 1

Status
Not open for further replies.

nomi2000

ISP
Feb 15, 2001
676
0
0
CA
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 &quot;0000.00.00.00&quot;
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) & &quot;.&quot; & valText.Substring(4, 2) & &quot;.&quot; & valText.Substring(6, 2) & &quot;.&quot; & valText.Substring(8, 2)
Return Me.Text
'valid HSCode after its formatted
Case Is = 13
intDecimalPos = InStr(valText, &quot;.&quot;, CompareMethod.Text)
'5th position
If intDecimalPos = 5 Then
'8 position
intDecimalPos += InStr(Mid(valText, (intDecimalPos + 1)), &quot;.&quot;, CompareMethod.Text)
If intDecimalPos = 8 Then
'11 position
intDecimalPos += InStr(Mid(valText, (intDecimalPos + 1)), &quot;.&quot;, CompareMethod.Text)
If intDecimalPos = 11 Then
'do nothing its valid HSCode
Return Me.Text
Else
If blnMsgbox = False Then
MsgBox(&quot;Invalid HS Code&quot;, MsgBoxStyle.Information, &quot;HSCode Validation&quot;)
blnMsgbox = True
Me.SelectionStart = 0
Me.SelectionLength = Me.Text.Length
Me.Focus()
blnMsgbox = False
End If
End If
Else
If blnMsgbox = False Then
MsgBox(&quot;Invalid HS Code&quot;, MsgBoxStyle.Information, &quot;HSCode Validation&quot;)
blnMsgbox = True
Me.SelectionStart = 0
Me.SelectionLength = Me.Text.Length
Me.Focus()
blnMsgbox = False
End If
End If
Else
If blnMsgbox = False Then
MsgBox(&quot;Invalid HS Code&quot;, MsgBoxStyle.Information, &quot;HSCode Validation&quot;)
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(&quot;Invalid HS Code&quot;, MsgBoxStyle.Information, &quot;HSCode Validation&quot;)
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 think it's the If statement you have there.
You cannot compare one value to two vales, you have to compare the first value to each value you are trying to match.

If Len(Value) = 10 Or 13 Then

change to :-

If Len(Value) = 10 Or Len(Value) = 13 Then

hope this helps

TJ

if at first you don't succeed, get a 10lb lump hammer
 
I am sorry, but it appears i am talking out of my ass.
VB.Net does allow comparisons like that.

Is the logic human-related or is the code looking at whether 12 is 'true'??

eugh - it's friday, my brain is mush.

TJ

if at first you don't succeed, get a 10lb lump hammer
 
This line is A problem.
If Len(Value) = 10 Or 13 Then

If you do not have Option Strict On then it may execute as
If Len(Value) = (10 Or 13) then ' 10 Bitwise OR with 13



Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
thanks John
then how i have to write it
If Len(value)=10 or Len(value)=13?
i tried this but its not giving me desired result
i just want to know can i OVERRIDE the text proeprty of the subclassed textbox? if yes then how? as when i want to enter the text in design mode (just for checking) it should change its format
Thanks
Nouman
 
If Len(value) = 10 or Len(value) = 13 then
is a correct way. I would use ORELSE as a matter of principle.
I mentioned the line as A problem, not THE problem.

I do not see how you are keeping
Private strText As String
and the base class Text Property in sync when all keyboard actions are going to be handled by the base TextBox and TextBoxBase classes.


Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Hi John
&quot;I do not see how you are keeping
Private strText As String&quot;
i don't know what do you mean by it i have a private variable named strText which is inside the control

&quot;the base class Text Property in sync when all keyboard actions are going to be handled by the base TextBox and TextBoxBase classes.&quot;
thats the thing which i have to understand how i can implement the Text proerty so the user can set and get it
Thanks alot
Nouman
 
You can not keep the Text. You do not have complete control of Text. The bas clas does. Try Mybase.Text
Public Overrides Property Text() As String
Get
Return Mybase.Text
End Get
Set(ByVal Value As String)
If Len(Value) = 10 Or 13 Then
Mybase.Text = ShowFormatText(Value)
Else
Mybase.Text = Value
End If
End Set
End Property


Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Thanks alot John
its the Mybase preprty which i should have used
thanks again
Nouman
 
Yeah John i have chnaged the thing you mentioned also i used MyBase to handle the TEXT property set and let and now its working fine in design mode as well
thanks again you help is precious
Nouman
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top