`
hej748
  • 浏览: 28302 次
  • 性别: Icon_minigender_2
  • 来自: 珠海
社区版块
存档分类
最新评论

两段代码 老师给的

    博客分类:
  • SSD
阅读更多

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

 

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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics