Results 1 to 4 of 4

Thread: Numerical value on TextBox

  1. #1
    Join Date
    Aug 2009
    Posts
    76

    Numerical value on TextBox

    As far as VB6 is concerned, it is easy to force a user to grasp the figure on a TextBox with KeyAscii. My concern is with Vb.net, my textbox should accept that number and not letters or other symbols. I put this code:
    Code:
      If Not IsNumeric (textbox1.text) then 
      MessageBox.Show ("blablabla") 
      End if
    This is perfect but this is not what I want to do. Even if the user clicks on a letter, the latter will not be written and no message should appear.

  2. #2
    Join Date
    May 2008
    Posts
    685

    Re: Numerical value on TextBox

    No direct solution via a property of the TextBox. To create a Numeric Text Box, here is the example code:

    Code:
    Public Class NumericTextBox
        Inherits TextBox
        Private SpaceOK As Boolean = False
    
        ' Restricts the entry of characters to digits (including hex),
        ' the negative sign, the e decimal point, and editing keystrokes (backspace).
        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
    
            Dim keyInput As String = e.KeyChar.ToString()
    
            If [Char].IsDigit(e.KeyChar) Then
                ' Digits are OK
            ElseIf keyInput.Equals(decimalSeparator) OrElse keyInput.Equals(groupSeparator) OrElse keyInput.Equals(negativeSign) Then
                ' Decimal separator is OK
            ElseIf e.KeyChar = vbBack Then
                ' Backspace key is OK
                '    else if ((ModifierKeys & (Keys.Control | Keys.Alt)) != 0)
                '    {
                '     // Let the edit control handle control and alt key combinations
                '    }
            ElseIf Me.SpaceOK AndAlso e.KeyChar = " "c Then
    
            Else
                ' Consume this invalid key and beep.
                e.Handled = True
            End If
    
        End Sub
    
    
        Public ReadOnly Property IntValue() As Integer
            Get
                Return Int32.Parse(Me.Text)
            End Get
        End Property
    
    
        Public ReadOnly Property DecimalValue() As Decimal
            Get
                Return [Decimal].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

  3. #3
    Join Date
    Nov 2008
    Posts
    1,054

    Re: Numerical value on TextBox

    Put this in the keypress event of your textbox:

    Code:
    If Asc(0) > Asc(e.KeyChar) Or Asc(9) < Asc(e.KeyChar) Then e.Handled = True
    Or you can also put this for simplicity:

    Code:
    If Char.IsDigit(e.KeyChar) Then
                e.Handled = False
    End If

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Numerical value on TextBox

    Use a mix of char.IsDigit and char.IsControl like this:

    Code:
    If Not Char.IsDigit(e.KeyChar) and Not Char.IsControl(e.KeyChar) Then
        e.Handled = True
    End If

Similar Threads

  1. Banking Exams Tips and tricks for Reasoning and Numerical Aptitude Questions.
    By Suryavansham in forum Education Career and Job Discussions
    Replies: 9
    Last Post: 08-04-2012, 11:57 AM
  2. Replies: 2
    Last Post: 20-08-2010, 01:23 AM
  3. ProgressBar In TextBox
    By Level8 in forum Software Development
    Replies: 5
    Last Post: 06-02-2010, 08:55 PM
  4. Textbox validation in ASP.NET
    By Kasper in forum Software Development
    Replies: 4
    Last Post: 19-01-2010, 08:28 PM
  5. Moving from one TextBox to another with TAB
    By GeforceUser in forum Software Development
    Replies: 3
    Last Post: 05-11-2009, 06:31 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,138,832.68925 seconds with 16 queries