Results 1 to 5 of 5

Thread: NumericTextBox only accepts greater than 0

  1. #1

    Thread Starter
    Fanatic Member Flashbond's Avatar
    Join Date
    Jan 2013
    Location
    Istanbul
    Posts
    646

    NumericTextBox only accepts greater than 0

    Hi!

    I am currently using this custom numerictextbox class: http://msdn.microsoft.com/en-us/libr...(v=vs.90).aspx

    I modified like this to make it not to accept negatives:
    Code:
    Public Class NumericTextBox
        Inherits TextBox
        Private SpaceOK As Boolean = False
        Protected Overrides Sub OnKeyPress(ByVal e As KeyPressEventArgs)
            MyBase.OnKeyPress(e)
            Dim numberFormatInfo As NumberFormatInfo = System.Globalization.CultureInfo.CurrentCulture.NumberFormat
            Dim decimalSeparator As String = numberFormatInfo.NumberDecimalSeparator
            Dim groupSeparator As String = numberFormatInfo.NumberGroupSeparator
            Dim negativeSign As String = numberFormatInfo.NegativeSign
            If groupSeparator = CChar(ChrW(160)).ToString() Then
                groupSeparator = " "
            End If
            Dim keyInput As String = e.KeyChar.ToString()
            If [Char].IsDigit(e.KeyChar) Then
            ElseIf keyInput.Equals(decimalSeparator) OrElse keyInput.Equals(groupSeparator) Then           
            ElseIf e.KeyChar = vbBack Then
            ElseIf Me.SpaceOK AndAlso e.KeyChar = " "c Then
            Else
                e.Handled = True
            End If
        End Sub
        Public ReadOnly Property IntValue() As Integer
            Get
                Return Int32.Parse(Me.Text)
            End Get
        End Property
        Public Property AllowSpace() As Boolean
            Get
                Return Me.SpaceOK
            End Get
            Set(ByVal value As Boolean)
                Me.SpaceOK = value
            End Set
        End Property
    End Class
    What should I do to make it ignore the first '0' inorder to have only positives?

    I have another question. It is revelant to this but I think I have to open another thread according to forum rules.
    EDIT: http://www.vbforums.com/showthread.p...GridView-class

    Thanks a lot!
    Last edited by Flashbond; Oct 7th, 2013 at 08:51 AM.
    God, are you punishing me because my hair is better than yours? -Jack Donaghy

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    Re: NumericTextBox only excepting greater than 0

    Why not just use a NumericUpDown? BTW - Can the user paste into this? If so KeyPress won't be fired.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3

    Thread Starter
    Fanatic Member Flashbond's Avatar
    Join Date
    Jan 2013
    Location
    Istanbul
    Posts
    646

    Re: NumericTextBox only excepting greater than 0

    Aaah! I forgot about the 'paste' totally!
    Yeah, I've tested it just right now. It doesn't accept Ctrl+V but right click.
    How about disabling context menu?

    EDIT: I set ShortcutsEnabled poperty to False.
    Last edited by Flashbond; Oct 7th, 2013 at 08:23 AM.
    God, are you punishing me because my hair is better than yours? -Jack Donaghy

  4. #4
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: NumericTextBox only excepting greater than 0

    Quote Originally Posted by Flashbond View Post
    Aaah! I forgot about the 'paste' totally!
    Yeah, I've tested it just right now. It doesn't accept Ctrl+V but right click.
    How about disabling context menu?

    EDIT: I set ShortcutsEnabled poperty to False.
    You're trying to hack a control into doing what it's simply not meant to do. If you really want to continue to try and force a textbox into a locked down, numbers only thing, you'll need to become very familiar with the WndProc event and become familiar with all of the messages that the control will receive (which includes cut, copy, paste, etc..)
    --or--
    You could use a .Net provided control that gives you exactly what you're wanting to do. I'll give you a hint, dbasnett has already mentioned it.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: NumericTextBox only accepts greater than 0

    I'd go with the NUD, myself. There are too many alternative issues with textboxes.
    My usual boring signature: Nothing

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