I know this post is old, but I wanted to share what I implemented to turn a TextBox into what I call an IntBox.
First you need to do the extension with:
<Runtime.CompilerServices.Extension()> _ Public Function HandledStringtoInteger(s As String) As Integer Try If s = String.Empty Then Return 0 Else Return Integer.Parse(s) End If Catch Dim result As String = String.Empty Dim ReturnInt As Integer Dim Parsed As Integer For Each Character In s.ToCharArray If Character = "-" Then If s.Substring(0, 1).ToString <> "-" Then result = Character + result End If End If If Character = "." Then Exit For End If If Integer.TryParse(Character, Parsed) Then result = result + Parsed.ToString End If Next If result <> String.Empty Then If Integer.TryParse(result, ReturnInt) Then Return Integer.Parse(ReturnInt) Else If Double.Parse(result) > Double.Parse(Integer.MaxValue.ToString) Then Return Integer.MaxValue ElseIf Double.Parse(result) < Double.Parse(Integer.MinValue.ToString) Then Return Integer.MinValue Else Return Integer.Parse(ReturnInt) End If End If Else Return 0 End If End Try End Function
Then create a TextChanged subtitle:
Private Sub TextBox_to_IntBox(sender As Object, e As TextChangedEventArgs) Handles YourTextBox.TextChanged If DirectCast(sender, TextBox).IsKeyboardFocused Then DirectCast(sender, TextBox).Text = DirectCast(sender, TextBox).Text.HandledStringtoInteger DirectCast(sender, TextBox).CaretIndex = DirectCast(sender, TextBox).Text.Length End If End Sub
Then, whenever a user enters text, he evaluates the string and returns only numeric values ββthat are within the standard integer. With the β-β symbol, you can change the integer from positive to negative and vice versa.
If anyone sees anything that might improve this code, let me know, but my tests show that it works fantastically to make IntBox.
EDIT: I found another method that might work if you use properties in your code. (Note that a separate property is required for TextBox)
First create a property:
Public Class Properties Implement INotifyPropertyChanged Private _Variable as Integer Public Property YourProperty as Object get Return _Variable end get set(value as Object) _Variable = value.ToString.ToInteger 'I will give the ToInteger extension code later end set end property Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged Public Sub OnPropertyChange(ByVal e As PropertyChangedEventArgs) If Not PropertyChangedEvent Is Nothing Then RaiseEvent PropertyChanged(Me, e) End If End Sub End Class
Then bind in the main class of your window:
Public WithEvents _YourVariable as New Properties Public Sub New() InitializeComponent() With YourTextBox .SetBinding(Textbox.TextProperty, New Binding("YourProperty")) .DataContext = _YourVariable End With End Sub
Finally, here is the ToInteger extension code I installed:
''' <summary> ''' Handles conversion of variable to Integer. ''' </summary> ''' <param name="X"></param> ''' <param name="I">Returned if conversion fails.</param> ''' <returns>Signed 32bit Integer</returns> ''' <remarks></remarks> <Runtime.CompilerServices.Extension()> _ Public Function toInteger(Of T)(ByRef X As T, Optional I As Integer = 0) As Integer Dim S As String = X.ToString Try If S = String.Empty Then Return I Else Return Integer.Parse(S) End If Catch Dim result As String = String.Empty Dim ReturnInt As Integer Dim Parsed As Byte For Each Character In S.ToCharArray If Character = "-" Then If S.Substring(0, 1).ToString <> "-" Then result = Character + result End If End If If Character = "." Then Exit For End If If Byte.TryParse(Character, Parsed) Then result = result + Parsed.ToString End If Next If result <> String.Empty Then If Integer.TryParse(result, ReturnInt) Then Return Integer.Parse(ReturnInt) Else If Double.Parse(result) > Double.Parse(Integer.MaxValue.ToString) Then Return Integer.MaxValue ElseIf Double.Parse(result) < Double.Parse(Integer.MinValue.ToString) Then Return Integer.MinValue Else Return Integer.Parse(ReturnInt) End If End If Else Return I End If End Try End Function
When they are all combined, when they enter something into the field, it will act as if it were a text field, but when they change focus, the ToInteger extension sets the value as an integer to the property and returns it to the text field.
Considering that if the operator entered "-1w3" after changing the focus, he will automatically return to "-13".