Wie der Titel es schon sagt, eine Textbox mit Eingabefilter.
Es können bestimmte Zeichen, ausdrücklich erlaubt, oder verboten werden.
Dabei spielt es keine Rolle, ob der Text per Tastatur, oder Zwischenablage eingegeben wird.
Option Strict On
Option Explicit On
Option Infer Off
Imports System.ComponentModel
Public Class TextBoxExtended : Inherits TextBox
Private _FilterType As FilterType = FilterType.Forbidden
<Category("Filter")>
Public Property FilterType() As FilterType
Get
Return _FilterType
End Get
Set(ByVal value As FilterType)
_FilterType = value
Invalidate()
End Set
End Property
Public _Filter As String = String.Empty
<Category("Filter")>
Public Property Filter() As String
Get
Return _Filter
End Get
Set(ByVal value As String)
_Filter = value
Invalidate()
End Set
End Property
Private Sub Me_TextChanged(sender As Object, e As EventArgs) Handles Me.TextChanged
Dim caretPosition As Integer = SelectionStart
Select Case FilterType
Case FilterType.Allowed
For Each _char As Char In Text.ToCharArray
If Not Filter.Contains(_char) Then
Text = Text.Replace(_char, String.Empty)
End If
Next
Case FilterType.Forbidden
For Each _char As Char In Filter.ToCharArray
Text = Text.Replace(_char, String.Empty)
Next
End Select
SelectionStart = caretPosition
End Sub
End Class
Public Enum FilterType
Allowed
Forbidden
End Enum
Kommentare zum Snippet