两段代码 老师给的

给一个文本框,只能输入数字、小数点和负号,其他的按键输入将不显示。

 

1.普通程序代码控制

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        Dim c As Char
        c = e.KeyChar
        If c = "."c Then
            If InStr(TextBox1.Text, e.KeyChar) > 0 Or (InStr(TextBox1.Text, "-") > 0) And (TextBox1.SelectionStart = 0) Then
                e.KeyChar = ""
            End If
        ElseIf
c = "-"c Then
            If InStr(TextBox1.Text, c) > 0 Or TextBox1.SelectionStart > 0 Then
                e.KeyChar = ""
            End If
        ElseIf
c = "0"c Then
            If
(TextBox1.SelectionStart = 0 And Not TextBox1.Text.StartsWith("0") And TextBox1.Text.Length <> 0) Or (TextBox1.SelectionStart <= 1 And TextBox1.Text.StartsWith("0")) Then
                e.KeyChar = ""
            End If
        ElseIf c = ControlChars.Back Then
        ElseIf
InStr("123456789", e.KeyChar) <= 0 Then
            e.KeyChar = ""
        End If
    End Sub

 

2.正则表达式控制//正则表达式不对

    Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
        '利用正则表达式验证
        'System.Text.RegularExpressions.Regex.IsMatch(TextBox2.Text, "^(-?\\d+)(\\.\\d+)?$")    ''''该表达式来源与网上,未经证实

    End Sub

//  ^[+-]?(?:\d+\.?\d*|\d*\.?\d+)[\r\n]*$  据说这个是对的。未验证。

 

3.使用MaskedTextBox//不能输入-和。

Mask属性设置的是自定义掩码为999999999999999

 

===============================================

 

将窗体中所有TextBox的text全部清空,其他的CheckBox啊RadioButton啊这些照旧

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim tp As TabPage
        Dim
text As Control

        For Each tp In TabControl1.TabPages
            For Each text In tp.Controls
                If TypeOf text Is TextBox Then
                    text.Text = ""
                End If
            Next
        Next

    End Sub

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值