ABL .NET FAQ - Working with Office 2007 documents
 

Home
Mission
News
Blog
Tools
FAQ
Publications
Downloads
Contact
Contents

 

Frequently asked questions

.NET FAQ Visual FoxPro FAQ

 


The MaskedTextBox and MaskedTextProvider types


One of the nice new additions in the .NET 2.0 framework is the MaskedTextBox. This MaskedTextBox allows for a input to be formatted according to a specific input mask. Great but text input isn't the only place values need to be formatted, the same problem shows up in output only places to. Wouldn't it be nice if we could use the same functionality in other places?

Well it turns out we can because the MaskedTextBox was also build around the provider model. In fact it uses the provide pattern and all the functionality used to implement the masking is actually implemented in the MaskedTextProvider type.

Now there is good news and bad news :-)

The good news is that the MaskedTextProvider type is public and you can use on its own :-)

The bad news is that you cannot make the MaskedTextBox use a different MaskedTextProvider :-( And there are some good reasons to do so. The MaskedTextProvider is very useful but it has one flaw and that is the fact that it handles all input as left to right. So it works great for a text kind of data but not for numerical data. Now this isn’t hard to do by adding a little code to wrap the behavior of the MaskedTextProvider but if we cannot get the MaskedTextBox to use our enhanced MaskedTextProvider the functionality is limited to output only.

The code below shows how you can get the MaskedTextProvider to work with numeric data. Its isn't perfect as it doesn’t do a good job when only fractions are involved but it isn’t hard to add, an exercise for the reader as they say :-)

Imports System.ComponentModel
 
Module Module1
 
    Sub Main()
        Dim number As New MaskedNumberProvider("999,999,999,999.99")
 
        Console.WriteLine("Adding")
        For i As Integer = 1 To 12
            Dim temp AsChar()
            temp = i.ToString.ToCharArray()
            number.Add(temp(temp.Length - 1))
            Console.WriteLine(number)
        Next
 
        Console.WriteLine("Removing")
        For i As Integer = 1 To 11
            number.Remove()
            Console.WriteLine(number)
        Next
    EndSub
EndModule
 
 
Class MaskedNumberProvider
    Inherits MaskedTextProvider
 
    PublicSubNew(ByVal mask As String)
        MyBase.New(mask)
    EndSub
 
    Public Overloads Function Add(ByVal input As Char) AsBoolean
        Dim current AsString
        current = Me.ToString(False, False)
        current = current.PadLeft(Me.EditPositionCount)
        current = current.Substring(1, Me.EditPositionCount - 1)
        current = current + input
 
        Return Me.Set(current)
    End Function
 
    Public Overloads Function Remove() As Boolean
        Dim current As String
        current = Me.ToString(False, False)
        current = current.Substring(0, Me.EditPositionCount - 1)
        current = current.PadLeft(Me.EditPositionCount)
 
        Return Me.Set(current)
    End Function
 
    Public Overrides Function ToString() As String
        Dim result As String
        result = MyBase.ToString()
        result = result.Replace(" " + _
           Me
.Culture.NumberFormat.NumberGroupSeparator, "  ")
        Return result
    End Function
End Class

 


 

Send mail to webmaster@TheProblemSolver.nl with questions or comments about this web site.
Copyright © 1995 - 2007 ABL - The Problem Solver
Last modified: 10/21/06
WF
RSS 2.0