Projektdateien hinzufügen.
This commit is contained in:
162
UID/MyTextBox.vb
Normal file
162
UID/MyTextBox.vb
Normal file
@@ -0,0 +1,162 @@
|
||||
Imports System.ComponentModel
|
||||
|
||||
Public Class MyTextBox
|
||||
Inherits System.Windows.Forms.TextBox
|
||||
Implements INotifyPropertyChanged
|
||||
Property _numbersOnly As Boolean = False
|
||||
Property _ShortDateOnly As Boolean = False
|
||||
Property _Waehrung As Boolean = False
|
||||
Property _Prozent As Boolean = False
|
||||
Property _useAsBindingText As Boolean = False
|
||||
|
||||
|
||||
Public Sub New()
|
||||
|
||||
If _Waehrung Then MyBase.TextAlign = HorizontalAlignment.Right
|
||||
' Me.Focus()
|
||||
End Sub
|
||||
|
||||
Private Sub MyTextBox_BindingContextChanged(sender As Object, e As EventArgs) Handles Me.BindingContextChanged
|
||||
' If _ShortDateOnly Then
|
||||
'If IsDate(sender.text) Then sender.text = CDate(sender.text).ToShortDateString
|
||||
' End If
|
||||
|
||||
' If _Waehrung Then
|
||||
'If IsNumeric(sender.text) Then sender.text = String.Format("{0:N2}", CDbl(sender.text))
|
||||
'End If
|
||||
End Sub
|
||||
|
||||
|
||||
' Sub fillWithSQL(sql As String, Optional conn As String = "SDL")
|
||||
' Me._value = (New SQL).getValueTxtBySql(sql, conn)
|
||||
' End Sub
|
||||
|
||||
Private Sub MyTextBox_KeyDown(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
|
||||
If _numbersOnly Then
|
||||
If (Microsoft.VisualBasic.Asc(e.KeyChar) < 48) _
|
||||
Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 57) Then
|
||||
e.Handled = True
|
||||
End If
|
||||
If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then
|
||||
e.Handled = False
|
||||
End If
|
||||
End If
|
||||
End Sub
|
||||
|
||||
Private Sub MyTextBox_Layout(sender As Object, e As LayoutEventArgs) Handles Me.Layout
|
||||
MyBase.CausesValidation = True
|
||||
If _ShortDateOnly Then MyBase.MaxLength = 10
|
||||
End Sub
|
||||
|
||||
Private Sub MyTextBox_Leave(sender As Object, e As EventArgs) Handles Me.Leave
|
||||
' If _DateOnly AndAlso Not isShortDate(sender.text) Then sender.focus()
|
||||
If _Waehrung Then
|
||||
Try
|
||||
sender.text = String.Format("{0:N2}", CDbl(sender.text))
|
||||
sender.ForeColor = System.Drawing.Color.Black
|
||||
Catch ex As Exception
|
||||
sender.ForeColor = System.Drawing.Color.Red
|
||||
End Try
|
||||
|
||||
End If
|
||||
|
||||
If _Prozent Then
|
||||
Try
|
||||
_value = CDbl(sender.text.ToString.Replace("%", "")) / 100
|
||||
Catch ex As Exception
|
||||
sender.ForeColor = System.Drawing.Color.Red
|
||||
_value = Propvalue
|
||||
End Try
|
||||
End If
|
||||
If _useAsBindingText Then
|
||||
_value = sender.text.ToString
|
||||
End If
|
||||
End Sub
|
||||
|
||||
|
||||
Private Sub MyTextBox_Textchanged(sender As Object, e As EventArgs) Handles Me.TextChanged
|
||||
sender.ForeColor = System.Drawing.Color.Black
|
||||
If _numbersOnly Then
|
||||
If Not IsNumeric(sender.text) Then sender.ForeColor = System.Drawing.Color.Red
|
||||
End If
|
||||
If _ShortDateOnly Then
|
||||
If Not isShortDate(sender.text) Then sender.ForeColor = System.Drawing.Color.Red
|
||||
End If
|
||||
If _Waehrung Then
|
||||
Try
|
||||
sender.text = String.Format("{0:N2}", CDbl(sender.text))
|
||||
Catch ex As Exception
|
||||
sender.ForeColor = System.Drawing.Color.Red
|
||||
End Try
|
||||
End If
|
||||
End Sub
|
||||
Function isShortDate(d) As Boolean
|
||||
Return CBool(IsDate(d) And d.length = 10)
|
||||
End Function
|
||||
|
||||
|
||||
Public Propvalue As String
|
||||
Public Property _value As String
|
||||
Get
|
||||
Return Propvalue
|
||||
End Get
|
||||
Set(v As String)
|
||||
If _Prozent Then
|
||||
Propvalue = v
|
||||
MyBase.Text = String.Format("{0:P2}", CDbl(Propvalue))
|
||||
OnPropertyChanged("_value")
|
||||
End If
|
||||
If _useAsBindingText Then
|
||||
Propvalue = v
|
||||
MyBase.Text = Propvalue
|
||||
OnPropertyChanged("_value")
|
||||
End If
|
||||
End Set
|
||||
End Property
|
||||
|
||||
|
||||
Public Event PropertyChanged As PropertyChangedEventHandler _
|
||||
Implements INotifyPropertyChanged.PropertyChanged
|
||||
|
||||
Protected Sub OnPropertyChanged(ByVal name As String)
|
||||
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name))
|
||||
End Sub
|
||||
|
||||
Private Sub MyTextBox_Validating(sender As Object, e As CancelEventArgs) Handles Me.Validating
|
||||
If sender.text = "" Then e.Cancel = False : Exit Sub 'wenn der Test leer ist, stimmt die Eingabe
|
||||
|
||||
Dim errMessage As String = "Es ist ein Validierungsfehler aufgetreten:" & vbNewLine
|
||||
Dim errDescription As String = "Überprüfen Sie die Eingabe!"
|
||||
|
||||
If _Waehrung Then
|
||||
Try
|
||||
String.Format("{0:N2}", CDbl(sender.text))
|
||||
e.Cancel = False
|
||||
Catch ex As Exception
|
||||
errDescription = "Die Eingabe muss eine Zahl sein! (z.B. 123,45)"
|
||||
e.Cancel = True
|
||||
End Try
|
||||
End If
|
||||
|
||||
If _Prozent Then
|
||||
e.Cancel = False
|
||||
End If
|
||||
|
||||
If _ShortDateOnly Then
|
||||
If isShortDate(sender.text) Then
|
||||
e.Cancel = False
|
||||
Else
|
||||
e.Cancel = True
|
||||
End If
|
||||
End If
|
||||
|
||||
If e.Cancel Then
|
||||
MsgBox(errMessage & errDescription)
|
||||
End If
|
||||
End Sub
|
||||
|
||||
Private Sub MyTextBox_Validated(sender As Object, e As EventArgs) Handles Me.Validated
|
||||
|
||||
'Wenns stimmt
|
||||
End Sub
|
||||
End Class
|
||||
Reference in New Issue
Block a user