VB.NET-Tipp 0139: TextboxError
von pks
Beschreibung
Eingabeprüfung und Validierung bei Textboxen unter Verwendung des ErrorProvider.
Schwierigkeitsgrad: | Framework-Version(en): .NET Framework 1.0, .NET Framework 1.1, .NET Framework 2.0, .NET Framework 3.0, .NET Framework 3.5, .NET Framework 4 | .NET-Version(en): Visual Basic 2002, Visual Basic 2003, Visual Basic 2005, Visual Basic 2008, Visual Basic 2010 | Download: |
' Dieser Quellcode stammt von http://www.activevb.de ' und kann frei verwendet werden. Für eventuelle Schäden ' wird nicht gehaftet. ' Um Fehler oder Fragen zu klären, nutzen Sie bitte unser Forum. ' Ansonsten viel Spaß und Erfolg mit diesem Source! ' Projektversion: Visual Studio 2005 ' ' Referenzen: ' - System ' - System.Data ' - System.Deployment ' - System.Drawing ' - System.Windows.Forms ' - System.Xml ' ' Imports: ' - System ' - System.Collections ' - System.Collections.Generic ' - System.Data ' - System.Drawing ' - System.Diagnostics ' - System.Windows.Forms ' ' ############################################################################## ' ############################ clsTextboxError.vb ############################## ' ############################################################################## Public Class clsTextboxError #Region " Variables " Private m_ErrProv As New ErrorProvider Private m_TBoxes As New List(Of TextBox) Private m_BackColor As Color = _ System.Drawing.SystemColors.Window Private m_BackColorFocus As Color = _ System.Drawing.SystemColors.Info Private m_IconAlignment As ErrorIconAlignment = _ ErrorIconAlignment.MiddleRight Private m_Padding As Integer = 5 Private m_TextAlign As Integer Private m_LeftAlignActiveControl As Boolean = True Private m_ErrorSound As System.Media.SystemSound = _ Media.SystemSounds.Beep #End Region #Region " Events " Public Event Enter(ByVal sender As Object, _ ByVal e As System.EventArgs) Public Event Leave(ByVal sender As Object, _ ByVal e As System.EventArgs) Public Event KeyPress(ByVal sender As Object, _ ByRef e As System.Windows.Forms.KeyPressEventArgs) Public Event TextChanged(ByVal sender As Object, _ ByVal e As System.EventArgs) Public Event Validated(ByVal sender As Object, _ ByVal e As System.EventArgs) Public Event Validating(ByVal sender As Object, _ ByVal e As System.ComponentModel.CancelEventArgs) #End Region #Region " einstellbare Properties " ''' <summary> ''' Liefert zur Fehleranzeige einen Errorsound, Default = Hand, abschalten ''' über Nothing ''' </summary> Public Property ErrorSound() As System.Media.SystemSound Get Return m_ErrorSound End Get Set(ByVal value As System.Media.SystemSound) m_ErrorSound = value End Set End Property ''' <summary> ''' Aktives Control (Focus) immer LeftAlign ''' </summary> Public Property LeftAlignActiveControl() As Boolean Get Return m_LeftAlignActiveControl End Get Set(ByVal value As Boolean) m_LeftAlignActiveControl = True End Set End Property ''' <summary> ''' Ruft Backcolor der Textboxen ab oder legt diese fest ''' </summary> Public Property BackColor() As Color Get Return m_BackColor End Get Set(ByVal value As Color) m_BackColor = value End Set End Property ''' <summary> ''' Ruft Backcolor für Textbox ab wenn diese den Focus erhält oder legt ''' diese fest ''' </summary> Public Property BackColorFocus() As Color Get Return m_BackColorFocus End Get Set(ByVal value As Color) m_BackColorFocus = value End Set End Property ''' <summary> ''' Ruft Alignment des ErrorIcon ab oder legt diese fest ''' </summary> Public Property IconAlignment() As ErrorIconAlignment Get Return m_IconAlignment End Get Set(ByVal value As ErrorIconAlignment) m_IconAlignment = value End Set End Property ''' <summary> ''' Ruft den Abstand zwischen dem ErrorIcon und der Textbox ab oder legt ''' diesen fest ''' </summary> Public Property IconPadding() As Integer Get Return m_Padding End Get Set(ByVal value As Integer) m_Padding = value End Set End Property ''' <summary> ''' Liefert eine Textbox aus einer Auflistung ''' </summary> ''' <param name="Index">0 basierter Index</param> Public ReadOnly Property TBoxes(ByVal Index As Integer) As TextBox Get Return m_TBoxes(Index) End Get End Property ''' <summary> ''' Anzahl Textboxen in Auflistung ''' </summary> Public ReadOnly Property TBoxesCount() As Integer Get Return m_TBoxes.Count End Get End Property #End Region ''' <param name="TextboxContainer">Form oder anderer Container</param> ''' <param name="SubContainerAlso">Subcontainer einbeziehen</param> Public Sub New(ByVal TextboxContainer As ContainerControl, _ Optional ByVal SubContainerAlso As Boolean = True) 'Handles anlegen AddHandles(TextboxContainer, SubContainerAlso) End Sub ''' <summary> ''' Erstellt Handles für alle Textboxen eines Containers ''' </summary> Private Sub AddHandles(ByVal Cont As Object, _ ByVal SubContainerAlso As Boolean) If Cont IsNot Nothing Then For Each Ctl As Control In Cont.Controls If TypeOf Ctl Is TextBox Then m_TBoxes.Add(Ctl) AddHandler Ctl.Enter, AddressOf TBox_Enter AddHandler Ctl.Leave, AddressOf TBox_Leave AddHandler Ctl.KeyPress, AddressOf TBox_KeyPress AddHandler Ctl.TextChanged, AddressOf Tbox_TextChanged AddHandler Ctl.Validated, AddressOf TBox_Validated AddHandler Ctl.Validating, AddressOf TBox_Validating ElseIf Ctl.Controls.Count > 0 Then If SubContainerAlso Then AddHandles(Ctl, SubContainerAlso) End If End If Next End If End Sub #Region " Aktivität bei Eingang und Verlassen einer Textbox" ''' <summary> ''' Bei Aktivierung einer Textbox ggf. Text auf LeftAlign, ''' BackColor wechseln ''' </summary> Private Sub TBox_Enter(ByVal sender As Object, _ ByVal e As System.EventArgs) Dim Tbox As TextBox = DirectCast(sender, TextBox) Tbox.BackColor = m_BackColorFocus RaiseEvent Enter(sender, e) m_TextAlign = Tbox.TextAlign If LeftAlignActiveControl Then Tbox.TextAlign = HorizontalAlignment.Left End If End Sub ''' <summary> ''' Beim Verlassen einer Textbox ggf. Text auf RightAlign, BackColor ''' auf Default ''' </summary> Private Sub TBox_Leave(ByVal sender As Object, _ ByVal e As System.EventArgs) Dim Ctl As Control = Form1.ActiveControl Dim Tbox As TextBox = DirectCast(sender, TextBox) Tbox.BackColor = m_BackColor m_ErrProv.Clear() RaiseEvent Leave(sender, e) If Tbox.TextAlign <> m_TextAlign Then Tbox.TextAlign = m_TextAlign Ctl.Focus() End If End Sub #End Region #Region " Standartevents starten " ''' <summary> ''' Keypress Event starten ''' </summary> Private Sub TBox_KeyPress(ByVal sender As Object, _ ByVal e As System.Windows.Forms.KeyPressEventArgs) RaiseEvent KeyPress(sender, e) End Sub ''' <summary> ''' TextChanged Event starten ''' </summary> Private Sub Tbox_TextChanged(ByVal sender As System.Object, _ ByVal e As System.EventArgs) RaiseEvent TextChanged(sender, e) End Sub ''' <summary> ''' Validated Event starten ''' </summary> Private Sub TBox_Validated(ByVal sender As Object, _ ByVal e As System.EventArgs) RaiseEvent Validated(sender, e) End Sub ''' <summary> ''' Validating Event starten ''' </summary> Private Sub TBox_Validating(ByVal sender As Object, _ ByVal e As System.ComponentModel.CancelEventArgs) RaiseEvent Validating(sender, e) End Sub #End Region #Region " Eingabe- und Logikprüfungen Numeric, Date, Time " ''' <summary> ''' Eingabeprüfung einer Textbox auf Numeric mit Fehleranzeige ''' über ErrorProvider ''' </summary> ''' <param name="Sender">Textbox (Sender) als Object</param> ''' <param name="e">KeyPressEventArgs aus dem Keypress Ereignis ''' einer Textbox</param> ''' <param name="SignsAfterPoint">Anzahl zuglassener ''' Nachkommastellen</param> ''' <param name="MinusAllowed">Minuseingabe erlaubt</param> Public Sub CheckNumericKeyPress(ByVal Sender As Object, _ ByRef e As System.Windows.Forms.KeyPressEventArgs, _ Optional ByVal SignsAfterPoint As Integer = 0, _ Optional ByVal MinusAllowed As Boolean = True) If Not CharsMaximalArrived(Sender, e) Then m_ErrProv.Clear() Dim ErrTxt As String = "" Dim Tbox As TextBox = DirectCast(Sender, TextBox) Select Case e.KeyChar Case "0"c To "9"c If Tbox.SelectionLength = 0 Then 'maximale Anzahl Nachkommastellen überprüfen Dim DP As Integer = Tbox.Text.IndexOf(","c) If (DP >= 0) And (Tbox.SelectionStart > DP) Then Dim Nk As Integer = Tbox.TextLength - DP - 1 If Nk = SignsAfterPoint Then ErrTxt = "nur " & SignsAfterPoint.ToString & _ " Nachkommastellen erlaubt" End If End If End If Case ","c 'Komma auf Zulässigkeit überprüfen If SignsAfterPoint = 0 Then ErrTxt = "nur Ganzahlen erlaubt" Else If Tbox.Text.IndexOf(","c) >= 0 Then ErrTxt = "Dezimalkomma bereits vorhanden" End If End If Case "-"c 'Minuszeichen auf Zulässigkeit überprüfen If Not MinusAllowed Then ErrTxt = "keine Minuseingabe erlaubt" Else If Tbox.Text.IndexOf("-"c) >= 0 Then ErrTxt = "Minuszeichen bereits vorhanden" ElseIf Tbox.SelectionStart > 0 Then ErrTxt = "Minuszeichen nur an 1. Stelle" End If End If Case Convert.ToChar(8) Case Convert.ToChar(13) e.Handled = True SendKeys.Send(Convert.ToChar(9)) Case Else ErrTxt = "keine gültige numerische Eingabe" End Select If Not String.IsNullOrEmpty(ErrTxt) Then KeyPressErrorShow(Sender, e, ErrTxt) End If End If End Sub ''' <summary> ''' Eingabeprüfung bei Datum ''' </summary> Public Sub CheckDateKeyPress(ByVal Sender As Object, _ ByRef e As System.Windows.Forms.KeyPressEventArgs) If Not CharsMaximalArrived(Sender, e) Then Dim ErrTxt As String = "" Select Case e.KeyChar Case "0"c To "9"c Case Convert.ToChar(8) Case Convert.ToChar(13) e.Handled = True SendKeys.Send(Convert.ToChar(9)) Case "."c Case ","c e.KeyChar = "."c Case Else ErrTxt = "nur erlaubt 0123456789,." KeyPressErrorShow(Sender, e, ErrTxt) End Select End If End Sub ''' <summary> ''' Eingabeprüfung bei Uhrzeit ''' </summary> Public Sub CheckTimeKeyPress(ByVal Sender As Object, _ ByRef e As System.Windows.Forms.KeyPressEventArgs) If Not CharsMaximalArrived(Sender, e) Then Dim ErrTxt As String = "" Select Case e.KeyChar Case "0"c To "9"c Case Convert.ToChar(8) Case Convert.ToChar(13) e.Handled = True SendKeys.Send(Convert.ToChar(9)) Case ":"c Case "."c, ","c e.KeyChar = ":"c Case Else ErrTxt = "nur erlaubt 0123456789,.:" KeyPressErrorShow(Sender, e, ErrTxt) End Select End If End Sub ''' <summary> ''' Überprüft eine Datumseingabe und formatiert ''' </summary> Public Sub DateValidating(ByVal sender As Object, _ ByVal e As System.ComponentModel.CancelEventArgs, _ Optional ByVal DateFormat As String = "dd.MM.yyyy") m_ErrProv.Clear() Dim Tbox As TextBox = DirectCast(sender, TextBox) If String.IsNullOrEmpty(Tbox.Text) Then Exit Sub End If Dim ErrTxt As String = "" Dim s() As String = Tbox.Text.Split("."c) If s.GetUpperBound(0) > 2 Then ErrTxt = "ungültiges Datumsformat, nur " & DateFormat.ToUpper ErrorShow(sender, ErrTxt) e.Cancel = True Exit Sub End If ReDim Preserve s(2) If String.IsNullOrEmpty(s(1)) Then s(1) = Date.Now.Month.ToString("00") End If If String.IsNullOrEmpty(s(2)) Then s(2) = Date.Now.Year.ToString("0000") End If If Convert.ToInt32(s(0)) > 31 Then ErrTxt = "Tag maximal 31" ElseIf Convert.ToInt32(s(1)) > 12 Then ErrTxt = "Monat maximal 12" Else If s(2).Length = 2 Then s(2) = "20" & s(2) ElseIf s(2).Length <> 4 Then ErrTxt = "Jahr nur 2- oder 4-stellig" End If End If If Not String.IsNullOrEmpty(ErrTxt) Then ErrorShow(sender, ErrTxt) e.Cancel = True Exit Sub End If Try Dim d As Date = Date.Parse(s(0) & "." & s(1) & "." & s(2)) Tbox.Text = d.ToString(DateFormat) Catch ex As Exception ErrTxt = "ungültiges Datum" ErrorShow(sender, ErrTxt) e.Cancel = True End Try End Sub ''' <summary> ''' Überprüft eine Zeiteingabe und formatiert ''' </summary> Public Sub TimeValidating(ByVal sender As Object, _ ByVal e As System.ComponentModel.CancelEventArgs, _ Optional ByVal TimeFormat As String = "HH:mm:ss") m_ErrProv.Clear() Dim Tbox As TextBox = DirectCast(sender, TextBox) If String.IsNullOrEmpty(Tbox.Text) Then Exit Sub End If Dim ErrTxt As String = "" Dim s() As String = Tbox.Text.Split(":"c) If s.GetUpperBound(0) > 2 Then ErrTxt = "ungültiges Zeitformat, nur " & TimeFormat.ToUpper ErrorShow(sender, ErrTxt) e.Cancel = True Exit Sub End If ReDim Preserve s(2) If String.IsNullOrEmpty(s(1)) Then s(1) = "00" End If If String.IsNullOrEmpty(s(2)) Then s(2) = "00" End If If Convert.ToInt32(s(0)) > 23 Then ErrTxt = "Stunden maximal 23" ElseIf Convert.ToInt32(s(1)) > 59 Then ErrTxt = "Minuten maximal 59" ElseIf Convert.ToInt32(s(2)) > 59 Then ErrTxt = "Sekunden maximal 59" End If If Not String.IsNullOrEmpty(ErrTxt) Then ErrorShow(sender, ErrTxt) e.Cancel = True Exit Sub End If Try Dim d As Date = Date.Parse(s(0) & ":" & s(1) & ":" & s(2)) Tbox.Text = d.ToString(TimeFormat) Catch ex As Exception ErrTxt = "ungültige Zeit" ErrorShow(sender, ErrTxt) e.Cancel = True End Try End Sub #End Region #Region " sonstige Aktivitäten" ''' <summary> ''' Einen bei Keypress ermittelten Fehler anzeigen, e.Handled wird auf ''' True gesetzt ''' </summary> ''' <param name="Sender">Textbox als Object</param> ''' <param name="e">KeypressEventArgs</param> ''' <param name="ErrorText">Anzuzeigender Fehler</param> Public Sub KeyPressErrorShow(ByVal Sender As Object, _ ByRef e As System.Windows.Forms.KeyPressEventArgs, _ ByVal ErrorText As String) ErrorShow(Sender, ErrorText) e.Handled = True End Sub ''' <summary> ''' Liefert einen Fehler wenn MaxLength erreicht bzw. überschritten wird ''' </summary> ''' <param name="Sender">Textbox als Object</param> ''' <param name="e">KeyPressEventArgs</param> Public Function CharsMaximalArrived(ByVal Sender As Object, _ ByRef e As System.Windows.Forms.KeyPressEventArgs) _ As Boolean m_ErrProv.Clear() Dim Tbox As TextBox = DirectCast(Sender, TextBox) Dim MaxLength As Integer = Tbox.MaxLength If MaxLength = 0 Then MaxLength = 32767 End If If (Tbox.SelectionLength > 0) Or _ (Tbox.Text.Length < MaxLength) Or _ (e.KeyChar = Convert.ToChar(8)) Or _ (e.KeyChar = Convert.ToChar(13)) Then Return False End If Dim ErrTxt = "es sind maximal " & Tbox.MaxLength & " Zeichen erlaubt" ErrorShow(Sender, ErrTxt) e.Handled = True Return True End Function ''' <summary> ''' Zeigt das ErrorIcon an, e.Handled muss ggf auf True gesetzt werden ''' </summary> ''' <param name="sender">Textbox als Object</param> ''' <param name="ErrorText">Der Fehlertext</param> Public Sub ErrorShow(ByVal sender As Object, ByVal ErrorText As String) Dim Tbox As TextBox = DirectCast(sender, TextBox) m_ErrProv.SetIconAlignment(Tbox, m_IconAlignment) m_ErrProv.SetIconPadding(Tbox, m_Padding) m_ErrProv.SetError(Tbox, ErrorText) If ErrorSound IsNot Nothing Then Dim ErrSound As System.Media.SystemSound = ErrorSound ErrSound.Play() End If End Sub ''' <summary> ''' Nur Grossschrift ''' </summary> Public Sub CharToUpper(ByVal Sender As Object, _ ByRef e As System.Windows.Forms.KeyPressEventArgs) If Not SendKeyTabOnEnter(Sender, e) Then If Not CharsMaximalArrived(Sender, e) Then e.KeyChar = Char.ToUpper(e.KeyChar) End If End If End Sub ''' <summary> ''' Nur Kleinschrift ''' </summary> Public Sub CharToLower(ByVal Sender As Object, _ ByRef e As System.Windows.Forms.KeyPressEventArgs) If Not SendKeyTabOnEnter(Sender, e) Then If Not CharsMaximalArrived(Sender, e) Then e.KeyChar = Char.ToLower(e.KeyChar) End If End If End Sub ''' <summary> ''' Keine Leerstellen erlaubt ''' </summary> Public Sub CharNoSpace(ByVal Sender As Object, _ ByRef e As System.Windows.Forms.KeyPressEventArgs) If Not CharsMaximalArrived(Sender, e) Then m_ErrProv.Clear() If e.KeyChar = " "c Then Dim ErrTxt As String = "keine Leerstellen erlaubt" ErrorShow(Sender, ErrTxt) e.Handled = True End If SendKeyTabOnEnter(Sender, e) End If End Sub ''' <summary> ''' Nur bestimmte erlaubte Zeichen ''' </summary> ''' <param name="Sender">Textbox als Object</param> ''' <param name="e">KeyPressEventArgs</param> ''' <param name="AllowedChars">Erlaubte Zeichen in einem String</param> Public Sub ThisCharsAllowedOnly(ByVal Sender As Object, _ ByRef e As System.Windows.Forms.KeyPressEventArgs, _ ByVal AllowedChars As String) If Not CharsMaximalArrived(Sender, e) Then If Not SendKeyTabOnEnter(Sender, e) Then m_ErrProv.Clear() If Not AllowedChars.Contains(e.KeyChar.ToString) Then Dim ErrTxt As String = "nur erlaubt " & _ AllowedChars ErrorShow(Sender, ErrTxt) e.Handled = True End If End If End If End Sub ''' <summary> ''' Sendet bei Enter einen Tab zum Controlwechsel ''' </summary> Public Function SendKeyTabOnEnter(ByVal Sender As Object, _ ByVal e As System.Windows.Forms.KeyPressEventArgs) _ As Boolean If e.KeyChar = Convert.ToChar(13) Then e.Handled = True SendKeys.SendWait(Convert.ToChar(9)) Return True End If Return False End Function #End Region End Class ' ############################################################################## ' ################################# clsVB.vb ################################### ' ############################################################################## Public Class clsVB Public ReadOnly Property Cr() As Char Get Return Convert.ToChar(13) End Get End Property Public ReadOnly Property Lf() As Char Get Return Convert.ToChar(10) End Get End Property Public ReadOnly Property CrLf() As String Get Return Cr & Lf End Get End Property Public ReadOnly Property Chr(ByVal Index As Integer) As Char Get Return Convert.ToChar(Index) End Get End Property Public ReadOnly Property Asc(ByVal c As Char) As Integer Get Dim b As Byte = Convert.ToByte(c) Return b End Get End Property Public ReadOnly Property Asc(ByVal s As String) As Integer Get Dim c As Char = Convert.ToChar(s) Return Asc(c) End Get End Property Public Function IIf(ByVal b As Boolean, ByVal A1 As Object, _ ByVal A2 As Object) As Object If b Then Return A1 Else Return A2 End If End Function End Class ' ############################################################################## ' ################################# Form1.vb ################################### ' ############################################################################## Public Class Form1 Private WithEvents cTboxE As clsTextboxError Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load cTboxE = New clsTextboxError(Me, True) TextBox1.MaxLength = 6 TextBox2.MaxLength = 12 TextBox3.MaxLength = 12 TextBox5.MaxLength = 10 TextBox6.MaxLength = 5 Label1.Text = "numerisch, n,2" Label2.Text = "aut. Upper" Label3.Text = "No Space" Label4.Text = "Only abcdefg" Label5.Text = "Datum" Label6.Text = "Uhrzeit" End Sub ''' <summary> ''' Eingabeprüfung bei KeyPress ''' </summary> Private Sub cTboxE_KeyPress(ByVal sender As Object, _ ByRef e As System.Windows.Forms.KeyPressEventArgs) _ Handles cTboxE.KeyPress Dim Tbox As TextBox = DirectCast(sender, TextBox) Select Case Tbox.Name Case "TextBox1" cTboxE.CheckNumericKeyPress(sender, e, 2) Case "TextBox2" cTboxE.CharToUpper(sender, e) Case "TextBox3" cTboxE.CharNoSpace(sender, e) Case "TextBox4" Dim s As String = "abcdefg" cTboxE.ThisCharsAllowedOnly(sender, e, s) Case "TextBox5" cTboxE.CheckDateKeyPress(sender, e) Case "TextBox6" cTboxE.CheckTimeKeyPress(sender, e) Case Else cTboxE.SendKeyTabOnEnter(sender, e) End Select End Sub ''' <summary> ''' Formatierung bei Verlassen der Textbox ''' </summary> Private Sub cTboxE_Validated(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles cTboxE.Validated Dim Tbox As TextBox = DirectCast(sender, TextBox) Select Case Tbox.Name Case "TextBox1" If Not String.IsNullOrEmpty(Tbox.Text) Then Dim d As Decimal = Decimal.Parse(Tbox.Text) If d <> 0 Then Tbox.Text = d.ToString("0.00") End If End If End Select End Sub ''' <summary> ''' Logische Prüfung vor Verlassen der Textbox ''' </summary> Private Sub cTboxE_Validating(ByVal sender As Object, _ ByVal e As System.ComponentModel.CancelEventArgs) _ Handles cTboxE.Validating Dim Tbox As TextBox = DirectCast(sender, TextBox) Select Case Tbox.Name Case "TextBox5" cTboxE.DateValidating(sender, e) Case "TextBox6" cTboxE.TimeValidating(sender, e, "HH:mm") End Select End Sub Private Sub TBox_KeyDown(ByVal sender As Object, _ ByVal e As System.Windows.Forms.KeyEventArgs) ' nur zur Demonstration für zusätzliche AddHandler ' Debug.Print(sender.Name) End Sub End Class
Ihre Meinung
Falls Sie Fragen zu diesem Artikel haben oder Ihre Erfahrung mit anderen Nutzern austauschen möchten, dann teilen Sie uns diese bitte in einem der unten vorhandenen Themen oder über einen neuen Beitrag mit. Hierzu können sie einfach einen Beitrag in einem zum Thema passenden Forum anlegen, welcher automatisch mit dieser Seite verknüpft wird.