Results 1 to 7 of 7

Thread: Textbox allows only < 100 & late binding

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2004
    Location
    The Netherlands
    Posts
    37

    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?

  2. #2
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    yep, try this:

    VB Code:
    1. Private WithEvents ctl As Control
    2.  
    3.     Private Sub frmComboExample_Activated(ByVal sender As Object, ByVal e System.EventArgs) Handles MyBase.Activated
    4.         Me.ctl = Me.ActiveControl
    5.     End Sub
    6.  
    7.     Private Sub ctl_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ctl.KeyPress
    8.         Dim tbReference As New TextBox
    9.         If ctl.GetType.Equals(tbReference.GetType) And ctl.Text.Length < 1 Then Exit Sub
    10.  
    11.         Select Case ctl.Name
    12.             Case Me.Text1.Name, Me.Text2.Name, Me.Text3.Name
    13.                 Dim KeyAscii As Integer
    14.  
    15.                 KeyAscii = Asc(e.KeyChar)
    16.                 Select Case KeyAscii
    17.                     Case 8, 13
    18.                     Case 48 To 57
    19.                         If ctl.Text.Length > 1 Then
    20.                             KeyAscii = 0
    21.                         End If
    22.                     Case Else
    23.                         KeyAscii = 0
    24.                 End Select
    25.  
    26.                 If KeyAscii = 0 Then
    27.                     e.Handled = True
    28.                 Else
    29.                     e.Handled = False
    30.                 End If
    31.         End Select
    32.     End Sub
    Last edited by CyberHawke; Jul 7th, 2004 at 01:49 PM.
    Whadayamean it doesn't work....
    It works fine on my machine!

  3. #3
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Sender is an object, you have to use DirectCast to let the compiler know it will be working with a Textbox.

    VB Code:
    1. Dim tbPassedTextBox as TextBox = DirectCast(Sender, TextBox)
    2.  
    3. 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.

  4. #4
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    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!

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2004
    Location
    The Netherlands
    Posts
    37
    Thanks to both for replies

    Although I must admit CyberHawke's solution is beyond me (for now ) I really like the fact that the list of 'handles' is put inside the procedure, as I have 20 or so textboxes, doesn't affect how things work, just like the way it looks.

    I tried converting before I posted this, but then I got the same 'late binding' error, later I discovered;

    If CType(sender, TextBox).Text.Length > 4 Then

    Reading the help files shows that DirectCast is better (as I'm 100% sure of the type of 'sender'), so I'm going to use that

    Next piece of code is a textbox that only accepts times (11:12, 19:46 etc. etc.) but I'm gonna look at creating my own textbox control for that one.

  6. #6
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    I need to add one more method to that code I sent you to make it work correctly
    VB Code:
    1. Private Sub ctl_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles ctl.Leave
    2.         Me.ctl = Me.ActiveControl
    3.     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!

  7. #7

    Thread Starter
    Member
    Join Date
    Jun 2004
    Location
    The Netherlands
    Posts
    37
    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:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim EnteredTime As DateTime
    3.  
    4.         Try
    5.             EnteredTime = TimeValue(Textbox1.Text)
    6.         Catch ex As Exception
    7.             MsgBox("You didn't enter a valid time!")
    8.         End Try
    9.  
    10.     End Sub


    This is the code from my first try;

    VB Code:
    1. Private Sub TimeInTextox(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    2.  
    3.         Dim KeyAscii As Integer
    4.         Dim StrLength As Integer
    5.  
    6.         StrLength = DirectCast(sender, TextBox).Text.Length
    7.         KeyAscii = Asc(e.KeyChar)
    8.  
    9.         Select Case StrLength
    10.             Case 0
    11.                 Select Case KeyAscii
    12.                     Case 48 To 57
    13.                     Case Else
    14.                         KeyAscii = 0
    15.                 End Select
    16.             Case 1
    17.                 Select Case KeyAscii
    18.                     Case 8, 48 To 58
    19.                     Case Else
    20.                         KeyAscii = 0
    21.                 End Select
    22.             Case 2
    23.                 Select Case KeyAscii
    24.                     Case 8
    25.                     Case 48 To 57
    26.                         If InStr(DirectCast(sender, TextBox).Text, ":") = 0 Then
    27.                             KeyAscii = 0
    28.                         End If
    29.                     Case 58
    30.                         If InStr(DirectCast(sender, TextBox).Text, ":") <> 0 Then
    31.                             KeyAscii = 0
    32.                         End If
    33.                     Case Else
    34.                         KeyAscii = 0
    35.                 End Select
    36.             Case 3
    37.                 Select Case KeyAscii
    38.                     Case 8, 48 To 57
    39.                     Case Else
    40.                         KeyAscii = 0
    41.                 End Select
    42.             Case 4
    43.                 Select Case KeyAscii
    44.                     Case 8
    45.                     Case 48 To 57
    46.                         If DirectCast(sender, TextBox).Text.IndexOf(":") = 1 Then
    47.                             KeyAscii = 0
    48.                         End If
    49.                     Case Else
    50.                         KeyAscii = 0
    51.                 End Select
    52.             Case Else
    53.                 Select Case KeyAscii
    54.                     Case 8
    55.                     Case Else
    56.                         KeyAscii = 0
    57.                 End Select
    58.         End Select
    59.  
    60.         If KeyAscii = 0 Then
    61.             e.Handled = True
    62.         Else
    63.             e.Handled = False
    64.         End If
    65.     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
  •  



Click Here to Expand Forum to Full Width