zoukankan      html  css  js  c++  java
  • 自写的简单屏蔽特定字符的TextBox和数字TextBox

    1.屏蔽特定字符的TextBox

    Public Class SpTextBox
        Inherits TextBox
        Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
            Dim selStart As Integer = Me.SelectionStart ’保存初始光标位置,方便之后固定光标位置
            Dim errorWordFlag As Integer = 0 ’标记是否出现过特定字符
            For i As Integer = 0 To Me.SelectionStart - 1

           ’防Access注入,屏蔽各种类型单引号,如果有其他需要可自行添加。
                If Me.Text(i) = "'" Or Me.Text(i) = "'" Or Me.Text(i) = "’" Then
                    Me.Text = Me.Text.Substring(0, i) + Me.Text.Substring(i + 1)
                    errorWordFlag = 1
                    Exit For
                End If
            Next

         ’出现过特定字符,调整光标位置
            If errorWordFlag = 1 Then
                Me.SelectionStart = selStart - 1
                Me.Focus()
                Exit Sub
            End If

            MyBase.OnTextChanged(e)
        End Sub
    End Class

     

    2.数字TextBox

    Public Class NumTextBox
        Inherits EESTextBox

        Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)

         Dim selStart As Integer = Me.SelectionStart ’保存初始光标位置,方便之后固定光标位置
            Dim wordFlag = 0 ’标记是否出现过非数字非"."字符

         ’"."开头的Pass掉

            If Me.Text.StartsWith(".") Then
                Me.Text = Me.Text.Substring(1, Me.Text.Length - 1)
                wordFlag = 1

            ’"."结尾的判断之前是否还有"."
            ElseIf Me.Text.EndsWith(".") Then
                If Me.Text.Length > 0 Then
                    For i As Integer = 1 To Me.SelectionStart - 1
                        If Me.Text(i) = "." And Me.Text.Substring(0, i).Contains(".") Then
                            Me.Text = Me.Text.Substring(0, i) + Me.Text.Substring(i + 1)
                            wordFlag = 1
                            Exit For
                        End If
                    Next
                End If
            Else

           ’循环扫描是否有非数字和"."的字符
                For i As Integer = 0 To Me.SelectionStart - 1
                    If Me.Text(i) <> "1" And Me.Text(i) <> "2" And Me.Text(i) <> "3" And Me.Text(i) <> "4" And Me.Text(i) <> "5" And Me.Text(i) <> "6" And Me.Text(i) <> "7" And Me.Text(i) <> "8" And Me.Text(i) <> "9" And Me.Text(i) <> "0" And Me.Text(i) <> "." Then
                        Me.Text = Me.Text.Substring(0, i) + Me.Text.Substring(i + 1)
                        wordFlag = 1
                        Exit For
                    End If
                Next
            End If

         ’如果有过非数字非"."字符,调整光标位置
            If wordFlag = 1 Then
                Me.SelectionStart = selStart - 1
                Me.Focus()
                Exit Sub
            End If

            MyBase.OnTextChanged(e)
        End Sub
    End Class

      目前两种TextBox还不免疫Ctrl+V的攻击(就是用Ctrl+V把非法字符贴过了还有问题),之后再完善。

      P.S. :之前发的没注释,不专业了。怕是过一阵自己都看不懂了,补上。

  • 相关阅读:
    [硬件驱动_蓝牙]蓝牙Bluez的编程实现
    二分查找
    LeetCode-35.Search Insert Position
    LeetCode-34.Find First and Last Position of Element in Sorted Array
    LeetCode-704.Binary Search
    剑指offer-最小的k个数
    树的各种遍历
    LeetCode-912.Sort an Array
    排序
    LeetCode-209.Minimum Size Subarray Sum
  • 原文地址:https://www.cnblogs.com/zhangnn2fs/p/2517559.html
Copyright © 2011-2022 走看看