Request Code for Integer Textbox menu

User Tag List

Results 1 to 5 of 5
  1. #1
    Roger Fang's Avatar Banned
    Reputation
    17
    Join Date
    Aug 2008
    Posts
    113
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Request Code for Integer Textbox

    I need a program that will have a textbox which will only accept integer entry.

    I need this box so that a number of gold can be entered. So I do not want letters in this box?

    If the box is called gold4.Text how can this be done?

    Request Code for Integer Textbox
  2. #2
    bezuca's Avatar Active Member
    Reputation
    16
    Join Date
    Jun 2007
    Posts
    131
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    With a "try catch" in the OK, Save or w/e name button, in your form.

    Code:
    Example
    
    C# code ( vb is similar, just change the syntax )
    
    try
    {
    
    int a = Convert.toInt32(gold4.Text);
    
    }
    catch(Exception a)
    {
       Messagebox.Show("Error!" Please insert numbers!");
       //or
       Messagebox.Show(a); // to show the error.
    }
    Last edited by bezuca; 11-06-2008 at 11:30 AM.
    :yuck:

  3. #3
    Murdok's Avatar Member
    Reputation
    34
    Join Date
    Oct 2007
    Posts
    69
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you doing it in visual basic you can do

    Code:
    if isnumeric(Gold4.text) then  'checks textbox for text 
    msgbox("Number") 'if only numbers
    Else 
    msgbox("Text")   'if theres text
    Gold4.text = ""
    End if

  4. #4
    Apoc's Avatar Angry Penguin
    Reputation
    1387
    Join Date
    Jan 2008
    Posts
    2,750
    Thanks G/R
    0/12
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    VB.NET (Just add this class to your project somewhere, build it, and choose the NumericTextBox from the Tool Box when in designer mode):

    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
    Credits: How to: Create a Numeric Text Box

    Learn to search folks.

  5. #5
    Bakami's Avatar Banned
    Reputation
    7
    Join Date
    Jan 2008
    Posts
    122
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just use a simple Val(gold4.text) statement.

Similar Threads

  1. [REQUEST] Ally code for Rift
    By Link3s in forum Rift
    Replies: 0
    Last Post: 04-07-2011, 07:24 PM
All times are GMT -5. The time now is 12:09 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search