Ralf Meier
Exampel для настраиваемого текстового поля-элемента управления, который допускает только числовые входы - определяется заданной маской.
(Поскольку изначально он использовался только мной, некоторые переменные и описания находятся на немецком языке ... но я надеюсь, что контроль понятен в любом случае)
Private Class NumericTextBox
Inherits TextBox
Property CheckLimits As Boolean
Get
Return myCheckLimits
End Get
Set(ByVal value As Boolean)
myCheckLimits = value
End Set
End Property
Private myCheckLimits As Boolean = False
Property Minimum As Double
Get
Return myMinimum
End Get
Set(ByVal value As Double)
myMinimum = value
End Set
End Property
Private myMinimum As Double = 0
Property Maximum As Double
Get
Return myMaximum
End Get
Set(ByVal value As Double)
myMaximum = value
End Set
End Property
Private myMaximum As Double = 100000
Property Maske As String
Get
Return myMaske
End Get
Set(ByVal value As String)
myMaske = value.Replace(".", ",")
Check_TextNumeric()
NumericMaske_zerlegen()
formatiere_Ausgabe()
End Set
End Property
Private myMaske As String = "#,000"
Property NegativeAllowed As Boolean
Get
Return myNegativeAllowed
End Get
Set(ByVal value As Boolean)
myNegativeAllowed = value
formatiere_Ausgabe()
End Set
End Property
Private myNegativeAllowed As Boolean = True
Event InputComplete(ByVal e As System.Windows.Forms.KeyEventArgs)
Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
Dim Taste As Keys = e.KeyCode
Select Case Taste
Case Keys.Enter, Keys.Escape
'Input Complete
formatiere_Ausgabe()
If myCheckLimits Then Grenzwerte_überprüfen(Text)
'e.Handled = True
MyBase.OnKeyDown(e)
RaiseEvent InputComplete(e)
Case Keys.Back
If SelectionStart > 0 Then SelectionStart -= 1
SelectedText = ""
If Text = "" Then Text = "0"
If SelectionLength = 0 Then SelectionLength = 1
e.Handled = True
Case Keys.Tab
Text += ","
SelectionStart += 1
e.Handled = True
Case Keys.Delete
SelectedText = ""
If Text = "" Then Text = "0"
If SelectionLength = 0 Then SelectionLength = 1
e.Handled = True
Case Else
Beep()
e.Handled = True
End Select
End Sub
Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
Dim hText As String() = Text.Replace("-", "").Split(",")
Dim xAnzVorKomma As Integer = hText(0).Length
Dim xAnzNachKomma As Integer = 0
If hText.Length > 1 Then xAnzNachKomma = hText(1).Length
Dim posKomma As Integer = Text.IndexOf(",")
Dim isEditingVorkomma As Boolean = SelectionStart <= posKomma
Dim Taste As Char = e.KeyChar
Select Case Taste
Case "0" To "9"
If isEditingVorkomma Then
If (myAnzVorKomma < 0) Or (xAnzVorKomma < myAnzVorKomma) Or (SelectionLength > 1) Then SelectedText = Taste
Else
If (myAnzNachKomma < 0) Or (xAnzNachKomma < myAnzNachKomma) Or (SelectionLength > 1) Then SelectedText = Taste
End If
Case "+"
If myNegativeAllowed Then
Dim p As Integer = SelectionStart
If Text.Substring(0, 1) = "-" Then
Text = Text.Replace("-", "")
If p > 0 Then SelectionStart = p - 1
End If
End If
Case "-"
If myNegativeAllowed Then
Dim p As Integer = SelectionStart
If Text.Substring(0, 1) <> "-" Then
Text = "-" + Text
SelectionStart = p + 1
Else
Text = Text.Replace("-", "")
If p > 0 Then SelectionStart = p - 1
End If
End If
Case ".", ","
If (Not Text.Contains(",") Or SelectedText.Contains(",")) And (myAnzNachKomma > 0) Then
SelectedText = ","
isNachkomma = True
End If
Case Is < Chr(32)
MyBase.OnKeyPress(e)
End Select
e.Handled = True
End Sub
Private isNachkomma As Boolean = False
Protected Overrides Function IsInputKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean
If keyData = Keys.Enter Then '???
Return True
ElseIf keyData = Keys.Back Then
Return True
Else
Return MyBase.IsInputKey(keyData)
End If
End Function
Private Sub Me_Leave(sender As Object, e As System.EventArgs) Handles Me.Leave, Me.LostFocus
formatiere_Ausgabe()
If myCheckLimits Then Grenzwerte_überprüfen(Text)
End Sub
Private Sub Grenzwerte_überprüfen(xValue As String)
Dim myValue As Double = Val(xValue)
If myValue < myMinimum Then
MessageBox.Show("Der Eingabewert darf den vorgegebenen" + vbCrLf + _
"Minimalwert von " + myMinimum.ToString + vbCrLf + _
"nicht unterschreiten !" _
, "fehlerhafte Eingabe")
Me.Select()
ElseIf myValue > myMaximum Then
MessageBox.Show("Der Eingabewert darf den vorgegebenen" + vbCrLf + _
"Maximalwert von " + myMaximum.ToString + vbCrLf + _
"nicht überschreiten !" _
, "fehlerhafte Eingabe")
Me.Select()
End If
End Sub
Private Sub formatiere_Ausgabe()
If myAnzNachKomma > 0 Then
Dim myVar As Double
Try
myVar = CSng(MyBase.Text)
Catch ex As Exception
myVar = Val(MyBase.Text)
End Try
Dim myFormat As String = FillString(myAnzNachKomma, "0")
MyBase.Text = myVar.ToString("0." + myFormat)
End If
If myAnzVorKomma > 0 Then
Dim myTextParts As String() = MyBase.Text.Split(",")
Dim myTextVorkomma As Integer = myTextParts(0).Length
Dim myZeichen As Integer = myAnzVorKomma - myTextVorkomma
If myZeichen > 0 Then
MyBase.Text = FillString(myZeichen, "0") + MyBase.Text
End If
End If
End Sub
Private Sub Check_TextNumeric()
If Val(MyBase.Text) = 0 Then
Dim sa As String() = myMaske.Split(",")
Dim myText As String
If sa.Length > 1 Then
myText = "0," + FillString(sa(1).Length, "0")
MyBase.Text = myText
Else
myText = "0"
MyBase.Text = myText
End If
End If
End Sub
Private myAnzVorKomma As Integer = -1
Private myAnzNachKomma As Integer = 3
Private myMaxTextLength As Integer = 5
Private Sub NumericMaske_zerlegen()
Dim myMaskeParts As String() = myMaske.Split(",")
If myMaskeParts.Length < 1 Then
myAnzVorKomma = -1
ElseIf myMaskeParts(0).Contains("#") And myMaskeParts(0).Contains("0") Then
myAnzVorKomma = -1
ElseIf myMaskeParts(0).Contains("#") Then
myAnzVorKomma = -1
ElseIf myMaskeParts(0).Contains("0") Then
myAnzVorKomma = myMaskeParts(0).Length
ElseIf Not myMaskeParts(0).Contains("#") And Not myMaskeParts(0).Contains("0") Then
myAnzVorKomma = 0
Text = "0,"
isNachkomma = True
End If
If myMaskeParts.Length < 2 Then
myAnzNachKomma = -1
ElseIf myMaskeParts(1).Contains("#") And myMaskeParts(0).Contains("0") Then
myAnzNachKomma = -1
ElseIf myMaskeParts(1).Contains("#") Then
myAnzNachKomma = -1
ElseIf myMaskeParts(1).Contains("0") Then
myAnzNachKomma = myMaskeParts(1).Length
ElseIf Not myMaskeParts(1).Contains("#") And Not myMaskeParts(1).Contains("0") Then
myAnzNachKomma = 0
End If
If (myAnzVorKomma > 0) And (myAnzNachKomma > 0) Then
myMaxTextLength = myAnzVorKomma + 1 + myAnzNachKomma
ElseIf (myAnzVorKomma = 0) And (myAnzNachKomma > 0) Then
myMaxTextLength = 1 + 1 + myAnzNachKomma
ElseIf (myAnzVorKomma > 0) And (myAnzNachKomma = 0) Then
myMaxTextLength = myAnzVorKomma
ElseIf (myAnzVorKomma < 0) Or (myAnzNachKomma < 0) Then
myMaxTextLength = -1
ElseIf (myAnzVorKomma = 0) And (myAnzNachKomma = 0) Then
myMaxTextLength = -1
End If
End Sub
End Class