It looks like MaxLength is a property of TextBoxBase and I couldn't find how it's used, plus you're setting the text internally.
Add this as the first line of your UpdateText() method:
If Me.Text.Length = Me.MaxLength Then Exit Sub
-----Original Message-----
From: systemidotnet-bounces@xxxxxxxxxxxx [mailto:systemidotnet-bounces@xxxxxxxxxxxx] On Behalf Of Mira, Antonio
Sent: Tuesday, January 19, 2010 2:40 PM
To: '.net use with the System i'
Subject: Re: [SystemiDotNet] Customer Textbox MaxLength Property...
Sure...
I copied the code from the Microsoft web site and all seems to work fine (as far as I've seen) except the maxlength property which should work because the control inherits from the Textbox. In any case, here it is...
Imports System.Globalization
Imports System.Text
Public Class NumericTextBox
Inherits System.Windows.Forms.TextBox
Private m_allowDecimal As Boolean = True
Private m_allowSeparator As Boolean = False
Private skipEvent As Boolean = False
Private decimalCount As Integer = 0
Private separatorCount As Integer = 0
Private numberFormatInfo As Globalization.NumberFormatInfo
Private decimalSeparator As String
Private groupSeparator As String
Private negativeSign As String
Private groupSize As Integer()
Public Sub New()
InitializeComponent()
numberFormatInfo = System.Globalization.CultureInfo.CurrentCulture.NumberFormat
decimalSeparator = numberFormatInfo.NumberDecimalSeparator
groupSeparator = numberFormatInfo.NumberGroupSeparator
groupSize = numberFormatInfo.NumberGroupSizes
negativeSign = numberFormatInfo.NegativeSign
End Sub
' Restricts the entry of characters to digits (including hexidecimal), the negative sign,
' the decimal point, and editing keystrokes (backspace).
Protected Overloads Overrides Sub OnKeyPress(ByVal e As KeyPressEventArgs)
MyBase.OnKeyPress(e)
Dim keyInput As String = e.KeyChar.ToString()
If [Char].IsDigit(e.KeyChar) Then
UpdateText(e.KeyChar)
e.Handled = True
' Allows one decimal separator as input.
ElseIf keyInput.Equals(decimalSeparator) Then
If m_allowDecimal Then
UpdateText(e.KeyChar)
End If
e.Handled = True
' Application should support a method to temporarily
' change input modes to allow input of the decimal
' character on the widest range of keyboards, especially
' 12-key keyboards (no Sym button). InputMode will reset
' to Numeric after next key press.
' Alternative method:
' else if (Char.IsPunctuation(e.KeyChar))
ElseIf keyInput.Equals("*") Then
If (AllowDecimal _
AndAlso (decimalCount = 0)) Then
'InputModeEditor.SetInputMode(Me, InputMode.AlphaABC)
' Supports reset for devices
' on which KeyUp fires last.
skipEvent = True
End If
e.Handled = True
' Allows separator.
ElseIf keyInput.Equals(groupSeparator) Then
If m_allowSeparator Then
UpdateText(e.KeyChar)
End If
e.Handled = True
' Allows negative sign if it is the initial character.
ElseIf keyInput.Equals(negativeSign) Then
UpdateText(e.KeyChar)
e.Handled = True
' Allows Backspace key.
ElseIf e.KeyChar = ControlChars.Back Then
ElseIf e.KeyChar = ControlChars.Cr Then
' Validate input when Enter key is pressed.
' Take other action.
UpdateText(e.KeyChar)
Else
' Consume this invalid key and beep.
' MessageBeep();
e.Handled = True
End If
End Sub
Protected Overrides Sub OnKeyDown(ByVal e As KeyEventArgs)
MyBase.OnKeyDown(e)
' InputModeEditor.SetInputMode(this, InputMode.Numeric);
End Sub
Protected Overrides Sub OnKeyUp(ByVal e As KeyEventArgs)
MyBase.OnKeyUp(e)
If skipEvent Then
skipEvent = False
Return
End If
' Restores text box to Numeric mode if it was
' changed for decimal entry.
'InputModeEditor.SetInputMode(Me, InputMode.Numeric)
End Sub
Public Property AllowDecimal() As Boolean
Get
Return m_allowDecimal
End Get
Set(ByVal value As Boolean)
m_allowDecimal = value
End Set
End Property
Public Property AllowSeparator() As Boolean
Get
Return m_allowSeparator
End Get
Set(ByVal value As Boolean)
m_allowSeparator = value
End Set
End Property
Public ReadOnly Property IntValue() As Integer
Get
Try
Return Int32.Parse(Me.Text, NumberStyles.AllowThousands Or NumberStyles.AllowLeadingSign)
Catch e As FormatException
'MessageBox.Show("invalid format: " + e.Message.ToString())
Return 0
Catch e As OverflowException
'MessageBox.Show("Exceeded min/max value!")
Return 0
End Try
End Get
End Property
Public ReadOnly Property DecimalValue() As Decimal
Get
Try
Return [Decimal].Parse(Me.Text, NumberStyles.AllowDecimalPoint Or NumberStyles.AllowThousands Or NumberStyles.AllowLeadingSign)
Catch e As FormatException
'MessageBox.Show("invalid format: " + e.Message.ToString())
Return 0
End Try
End Get
End Property
' Checks current input characters
' and updates control with valid characters only.
Public Sub UpdateText(ByVal newKey As Char)
decimalCount = 0
separatorCount = 0
Dim input As String
input = (Me.Text + newKey.ToString)
Dim updatedText As String = ""
Dim cSize As Integer = 0
' char[] tokens = new char[] { decimalSeparator.ToCharArray()[0] };
' NOTE: Supports decimalSeparator with a length == 1.
Dim token As Char = decimalSeparator.ToCharArray()(0)
Dim groups As String() = input.Split(token)
' Returning input to the left of the decimal.
Dim inputChars As Char() = groups(0).ToCharArray()
' Reversing input to handle separators.
Dim rInputChars As Char() = inputChars.Reverse().ToArray()
Dim sb As New StringBuilder()
Dim validKey As Boolean = False
For x As Integer = 0 To rInputChars.Length - 1
If rInputChars(x).ToString().Equals(groupSeparator) Then
Continue For
End If
' Checking for decimalSeparator is not required in
' current implementation. Current implementation eliminates
' all digits to the right of extraneous decimal characters.
If rInputChars(x).ToString().Equals(decimalSeparator) Then
If Not m_allowDecimal Or decimalCount > 0 Then
Continue For
End If
' decimalCount += 1
' validKey = True
End If
If rInputChars(x).ToString().Equals(negativeSign) Then
' Ignore negativeSign unless processing first character.
If x < (rInputChars.Length - 1) Then
Continue For
End If
sb.Insert(0, rInputChars(x).ToString())
x += 1
Continue For
End If
If m_allowSeparator Then
' NOTE: Does not support multiple groupSeparator sizes.
If cSize > 0 AndAlso cSize Mod groupSize(0) = 0 Then
sb.Insert(0, groupSeparator)
separatorCount += 1
End If
End If
' Maintaining correct group size for digits.
If [Char].IsDigit(rInputChars(x)) Then
' Increment cSize only after processing groupSeparators.
cSize += 1
validKey = True
End If
If validKey Then
sb.Insert(0, rInputChars(x).ToString())
End If
validKey = False
Next
updatedText = sb.ToString()
If m_allowDecimal AndAlso groups.Length > 1 Then
Dim rightOfDecimals As Char() = groups(1).ToCharArray()
Dim sb2 As New StringBuilder()
For Each dec As Char In rightOfDecimals
If [Char].IsDigit(dec) Then
sb2.Append(dec)
End If
Next
updatedText += decimalSeparator + sb2.ToString()
End If
' Updates text box.
Me.Text = updatedText
' Updates cursor position.
Me.SelectionStart = Me.Text.Length
End Sub
End Class
Thank you,
Antonio Mira
Application Developer - Mid-Ohio Division
Time Warner Cable
1015 Olentangy River Road - 2nd Floor
Columbus, OH 43212
http://www.timewarnercable.com
phone: 614 827 7949
cell: 614 365 0489
-----Original Message-----
From: systemidotnet-bounces@xxxxxxxxxxxx [mailto:systemidotnet-bounces@xxxxxxxxxxxx] On Behalf Of ibm
Sent: Tuesday, January 19, 2010 3:36 PM
To: .net use with the System i
Subject: Re: [SystemiDotNet] Customer Textbox MaxLength Property...
Can you post some code?
-----Original Message-----
From: systemidotnet-bounces@xxxxxxxxxxxx
[mailto:systemidotnet-bounces@xxxxxxxxxxxx] On Behalf Of Mira, Antonio
Sent: Tuesday, January 19, 2010 2:00 PM
To: 'systemidotnet@xxxxxxxxxxxx'
Subject: [SystemiDotNet] Customer Textbox MaxLength Property...
Hello,
While I realize this is not strictly .NET and the i, I'll throw the
question because I'm not getting much in the forums.
I created a custom control that inherits from a textbox. I brought in
the custom control in the toolbox to a form and set its Maxlength
property to 5. When I run the form, the textbox is allowing more than 5
characters to be typed in. If I put a textbox control directly from the
Toolbox, the maxlength property works as expected: it limits the number
of characters entered.
Does anybody know why this is happening and what the solution is?
Thanks for any help that you can provide...
Thank you,
Antonio Mira
Application Developer - Mid-Ohio Division
Time Warner Cable
1015 Olentangy River Road - 2nd Floor
Columbus, OH 43212
http://www.timewarnercable.com
phone: 614 827 7949
cell: 614 365 0489
This E-mail and any of its attachments may contain Time Warner
Cable proprietary information, which is privileged, confidential,
or subject to copyright belonging to Time Warner Cable. This E-mail
is intended solely for the use of the individual or entity to which
it is addressed. If you are not the intended recipient of this
E-mail, you are hereby notified that any dissemination,
distribution, copying, or action taken in relation to the contents
of and attachments to this E-mail is strictly prohibited and may be
unlawful. If you have received this E-mail in error, please notify
the sender immediately and permanently delete the original and any
copy of this E-mail and any printout.
As an Amazon Associate we earn from qualifying purchases.