|
-
Jul 7th, 2004, 10:29 AM
#1
Thread Starter
Member
Textbox allows only < 100 & late binding
I have 3 textboxes on my form and each can only contain a number < 100, so I made the following;
Private Sub STH(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Text1.KeyPress, Text2.KeyPress, Text3.KeyPress
Dim KeyAscii As Integer
KeyAscii = Asc(e.KeyChar)
Select Case KeyAscii
Case 8, 13
Case 48 To 57
If sender.Text.Length > 1 Then
KeyAscii = 0
End If
Case Else
KeyAscii = 0
End Select
If KeyAscii = 0 Then
e.Handled = True
Else
e.Handled = False
End If
End Sub
But I've got option strict on and this gives the error;
Option Strict On disallows late binding.
I can ignore the error message, or remove option strict and it works, but is there a way to fix this?
-
Jul 7th, 2004, 10:53 AM
#2
Hyperactive Member
yep, try this:
VB Code:
Private WithEvents ctl As Control
Private Sub frmComboExample_Activated(ByVal sender As Object, ByVal e System.EventArgs) Handles MyBase.Activated
Me.ctl = Me.ActiveControl
End Sub
Private Sub ctl_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ctl.KeyPress
Dim tbReference As New TextBox
If ctl.GetType.Equals(tbReference.GetType) And ctl.Text.Length < 1 Then Exit Sub
Select Case ctl.Name
Case Me.Text1.Name, Me.Text2.Name, Me.Text3.Name
Dim KeyAscii As Integer
KeyAscii = Asc(e.KeyChar)
Select Case KeyAscii
Case 8, 13
Case 48 To 57
If ctl.Text.Length > 1 Then
KeyAscii = 0
End If
Case Else
KeyAscii = 0
End Select
If KeyAscii = 0 Then
e.Handled = True
Else
e.Handled = False
End If
End Select
End Sub
Last edited by CyberHawke; Jul 7th, 2004 at 01:49 PM.
Whadayamean it doesn't work....
It works fine on my machine!

-
Jul 7th, 2004, 11:18 AM
#3
I wonder how many charact
Sender is an object, you have to use DirectCast to let the compiler know it will be working with a Textbox.
VB Code:
Dim tbPassedTextBox as TextBox = DirectCast(Sender, TextBox)
if tbPassedTextBox.Text.Length > 0 Then ...
The culprit line is Sender.Text.Length, because .Text is not a property of the Object class. All you needed to do was use the above lines of code, no need to change anything else.
-
Jul 7th, 2004, 11:22 AM
#4
Hyperactive Member
Nice catch, I expected it to be the use of multiple object event declarations that the method was handling. The code I provided will work, but this is much simpler and doesn't require such a significant change to his code.
Whadayamean it doesn't work....
It works fine on my machine!

-
Jul 7th, 2004, 12:25 PM
#5
Thread Starter
Member
-
Jul 7th, 2004, 01:34 PM
#6
Hyperactive Member
I need to add one more method to that code I sent you to make it work correctly
VB Code:
Private Sub ctl_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles ctl.Leave
Me.ctl = Me.ActiveControl
End Sub
I also found mistakes in my original code, so it has now been edited and is tested and works
Last edited by CyberHawke; Jul 7th, 2004 at 01:48 PM.
Whadayamean it doesn't work....
It works fine on my machine!

-
Jul 7th, 2004, 01:50 PM
#7
Thread Starter
Member
For those who want it, been trying to make a textbox that only accepts a time, the code follows below. But after making it I considered it too much code so I made this, which you execute whenever you want to use the entered time (in my case a button click)
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim EnteredTime As DateTime
Try
EnteredTime = TimeValue(Textbox1.Text)
Catch ex As Exception
MsgBox("You didn't enter a valid time!")
End Try
End Sub
This is the code from my first try;
VB Code:
Private Sub TimeInTextox(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim KeyAscii As Integer
Dim StrLength As Integer
StrLength = DirectCast(sender, TextBox).Text.Length
KeyAscii = Asc(e.KeyChar)
Select Case StrLength
Case 0
Select Case KeyAscii
Case 48 To 57
Case Else
KeyAscii = 0
End Select
Case 1
Select Case KeyAscii
Case 8, 48 To 58
Case Else
KeyAscii = 0
End Select
Case 2
Select Case KeyAscii
Case 8
Case 48 To 57
If InStr(DirectCast(sender, TextBox).Text, ":") = 0 Then
KeyAscii = 0
End If
Case 58
If InStr(DirectCast(sender, TextBox).Text, ":") <> 0 Then
KeyAscii = 0
End If
Case Else
KeyAscii = 0
End Select
Case 3
Select Case KeyAscii
Case 8, 48 To 57
Case Else
KeyAscii = 0
End Select
Case 4
Select Case KeyAscii
Case 8
Case 48 To 57
If DirectCast(sender, TextBox).Text.IndexOf(":") = 1 Then
KeyAscii = 0
End If
Case Else
KeyAscii = 0
End Select
Case Else
Select Case KeyAscii
Case 8
Case Else
KeyAscii = 0
End Select
End Select
If KeyAscii = 0 Then
e.Handled = True
Else
e.Handled = False
End If
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|