• VB.net中使文本框只能输入数字


    编制计算类程序时文本框一般都用于输入数字数据,此时可以通过以下步骤来确保用户输入数据的正确性:

    首先添加好文本框控件TextBox后把属性IMEMode的值改成Disable,这样在该文本框中就不能使用中文输入法了。然后对文本框的KeyPress事件添加以下代码

      • 文本框用来输入整数
    Private Sub TextBox1_KeyPress(ByVal sender As ObjectByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Textbox1.KeyPress
        
    If Char.IsDigit(e.KeyChar) Or e.KeyChar = Chr(8Then
            e.Handled 
    = False
        
    Else
            e.Handled 
    = True
        
    End If
    End Sub

    其中e.KeyChar是键盘输入的键对应的字符,IsDigit函数可以判断是否为0到9的数字,Chr(8)为退格键,当e.Handled为True时,程序认为该KeyPress事件已经被处理过了,文本框的内容也就不会发生变化了。从以上程序中不难看出,此时文本框只能接受数字和退格键,也就是说这个文本框是用来输入整数的。

    • 文本需要输入小数

    如果文本需要输入小数的话,就要能够输入小数点".",而且小数点只能输入一次,此时可以将上面的函数改为下面的格式:

    Private Sub TextBox1_KeyPress(ByVal sender As ObjectByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Textbox1.KeyPress
        
    If Char.IsDigit(e.KeyChar) or e.KeyChar = Chr(8or e.KeyChar = "." Then
            
    If e.KeyChar = "." And InStr(TextBox1.Text, "."> 0 Then
                e.Handled 
    = True
            
    Else
                e.Handled 
    = False
            
    End If
        
    Else
            e.Handled 
    = True
        
    End If
    End Sub

    其中函数InStr返回指定字符在字符串中的位置,当字符串中不含此字符时,返回一个负数。这时文本框可以用来输入正整数及小数。

    • 文本需要输入负数

    如果文本需要输入负数的话,就要能够输入负号"-",而且只能是文本框输入的第一个字符,此时又可以将上面的函数改为下面的格式:

    Private Sub TextBox1_KeyPress(ByVal sender As ObjectByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Textbox1.KeyPress
        
    If Char.IsDigit(e.KeyChar) Or e.KeyChar = "." Or e.KeyChar = Chr(8Then
            
    If e.KeyChar = "." And InStr(TextBox1.Text, "."> 0 Then
                e.Handled 
    = True
             
    Else
                 e.Handled 
    = False
             
    End If
         
    ElseIf e.KeyChar = "-" And TextBox1.Text = "" Then
             e.Handled 
    = False
         
    Else
             e.Handled 
    = True
         
    End If
    End Sub

    这时文本框可以用来输入正的或负的整数及小数。

    当有多个文本框需要做输入限制时,可以在以上代码Handles后面加上多个文本框的KeyPress事件,各事件之间用逗号隔开,例如

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Textbox1.KeyPress,Textbox2.KeyPress,Textbox3.KeyPress

    对于要判断文本框内容的函数,就要把代码进行修改,使其能够同时处理多个文本框,也就是把上面后两段程序中的TextBox1.Text改为CType(sender, TextBox).Text,这样一来就能够对号入座了,比如上面能够输入负号的代码就可以改成:

    Private Sub TextBox1_KeyPress(ByVal sender As ObjectByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Textbox1.KeyPress,Textbox2.KeyPress,Textbox3.KeyPress
         
    If Char.IsDigit(e.KeyChar) Or e.KeyChar = "." Or e.KeyChar = Chr(8Then
              
    If e.KeyChar = "." And InStr(CType(sender, TextBox).Text, "."> 0 Then
                   e.Handled 
    = True
              
    Else
                   e.Handled 
    = False
              
    End If
         
    ElseIf e.KeyChar = "-" And CType(sender, TextBox).Text = "" Then
              e.Handled 
    = False
         
    Else
              e.Handled 
    = True
         
    End If
    End Sub

    这样一来四个文本框就都只能输入正的或负的整数及小数了。

    文本框可以用来输入正整数及小数,限定只允许输入两位小数

    '函数InStr返回指定字符在字符串中的位置,当字符串中不含此字符时,返回一个负数。
    If Char.IsDigit(e.KeyChar) Or e.KeyChar = Chr(8) Or e.KeyChar = "." Then
    If e.KeyChar = "." And InStr(TextBox1.Text, ".") > 0 Then
    '防止用户输入多个“.”
    e.Handled = True
    Else
    '用户输入的是数字,如果是小数点后第三位数字,就不允许输入,否则允许。
    If Char.IsDigit(e.KeyChar) And (InStr(TextBox1.Text, ".") + 1 < _
    TextBox1.SelectionStart
    And InStr(TextBox1.Text, ".") > 0) Then
    '当用户输入大于两位小数的数时,阻止键盘输入。
    e.Handled = True
    Else
    '符合程序设定要求,允许键盘输入
    e.Handled = False
    End If
    End If
    Else
    '不接受用户输入的非数字、非“.”等字符。
    e.Handled = True
    End If


    下面是只允许输入数字的一个类:

    Public Class AllowEnterDigit

    '#######类的作用:只允许输入数字,且只允许指定位数小数的类。该类具有两个方法。
    '#######类的调用:在文本框Textbox的KeyPress事件中,加入下列代码
    'dim NewExmple as AllowEnterDigit
    'NewExmple.IsAllowDecimal(sender,e,2)'2代表只允许两位小数

    '只允许输入数字的方法
    Public Sub IsAllowDigit(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
    If Char.IsDigit(e.KeyChar) Or e.KeyChar = Chr(8) Then
    e.Handled = False
    Else
    e.Handled = True
    End If

    End Sub


    '允许输入指定数量位数的小数
    Public Sub IsAllowDecimal(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs, ByVal DecimalNumber As Int16)
    '函数InStr返回指定字符在字符串中的位置,当字符串中不含此字符时,返回一个负数。
    If Char.IsDigit(e.KeyChar) Or e.KeyChar = Chr(8) Or e.KeyChar = "." Then
    If e.KeyChar = "." And InStr(sender.Text, ".") > 0 Then
    '防止用户输入多个“.”
    e.Handled = True
    Else
    '用户输入的是数字,如果是小数点后第三位数字,就不允许输入,否则允许。
    If Char.IsDigit(e.KeyChar) And (InStr(sender.Text, ".") + (DecimalNumber - 1) < _
    sender.SelectionStart And InStr(sender.Text, ".") > 0) Then
    '当用户输入大于两位小数的数时,阻止键盘输入。
    e.Handled = True
    Else
    '符合程序设定要求,允许键盘输入
    e.Handled = False
    End If
    End If
    Else
    '不接受用户输入的非数字、非“.”等字符。
    e.Handled = True
    End If
    End Sub
    End Class















  • 相关阅读:
    Open_basedir 开启后项目变慢
    PHP导入百万级excel数据方案
    使用Python统计项目代码行数(Python3.X)
    AttributeError: '_io.TextIOWrapper' object has no attribute 'xreadlines'
    startTime = time.clock()AttributeError: module 'time' has no attribute 'clock
    SyntaxError: Missing parentheses in call to 'print'. Did you mean print
    误删 Win10 应用商店应该如何恢复?
    win10无法开启Windows Defender Firewall服务,错误1058
    设备管理器里面的AAP Server是什么?
    layui——Cannot create property 'LAY_TABLE_INDEX' on number '2'
  • 原文地址:https://www.cnblogs.com/shuiguang/p/2092244.html
Copyright © 2020-2023  润新知