Advertisement
ClarusDignus

How to check if text box max length has been exceeded?

Oct 23rd, 2014
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'How to check if text box max length has been exceeded?
  2. 'http://stackoverflow.com/questions/26534314/how-to-check-if-text-box-max-length-has-been-exceeded
  3. 'Corrected logic (full code using tooltips).
  4.  
  5. Public Class Form1
  6.     Private Sub Textbox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
  7.         If Not IsNumeric(TextBox1.Text) Then
  8.             If TextBox1.Text.Length > 8 Then
  9.                 TextBox1.Text = TextBox1.Text.Substring(0, 8)
  10.                 TextBox1.Select(TextBox1.TextLength, 0)
  11.                 ToolTip1.IsBalloon = True
  12.                 ToolTip1.ToolTipTitle = "8 character maximum and input must be numeric!"
  13.                 ToolTip1.Active = True
  14.                 ToolTip1.ToolTipIcon = ToolTipIcon.Warning
  15.                 ToolTip1.Show(vbNewLine, TextBox1, 45, -40, 2000)
  16.             ElseIf TextBox1.Text.Length > 0 Then
  17.                 If ToolTip1.GetToolTip(TextBox1) = "" Then
  18.                     ToolTip1.ToolTipTitle = "Input must be numeric!"
  19.                     ToolTip1.Active = True
  20.                     ToolTip1.IsBalloon = True
  21.                     ToolTip1.ToolTipIcon = ToolTipIcon.Warning
  22.                     ToolTip1.Show(vbNewLine, TextBox1, 45, -40, 2000)
  23.                 End If
  24.             End If
  25.         Else
  26.             If TextBox1.Text.Length > 8 Then
  27.                 TextBox1.Text = TextBox1.Text.Substring(0, 8)
  28.                 TextBox1.Select(TextBox1.TextLength, 0)
  29.                 ToolTip1.IsBalloon = True
  30.                 ToolTip1.ToolTipTitle = "8 character maximum!"
  31.                 ToolTip1.Active = True
  32.                 ToolTip1.ToolTipIcon = ToolTipIcon.Warning
  33.                 ToolTip1.Show(vbNewLine, TextBox1, 45, -40, 2000)
  34.             Else
  35.                 ToolTip1.Active = False
  36.                 ToolTip1.Hide(TextBox1)
  37.             End If
  38.         End If
  39.     End Sub
  40. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement