Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2002
    Location
    Illinois
    Posts
    133

    Unanswered: Still can't clear Masked Edit Box - VB.Net

    I am posting this again because I didn't mention before that I am using VB.Net.

    I have an unbound masked edit control with a mask for a social security number. I had code in the click event of a button to clear the SSN but it would only work the first time the button was clicked.

    This is the line of code I used:

    Me.MaskedEdit1.Text = " / / " (3 spaces, 2 spaces, 4 spaces)

    Also tried:
    Me.MaskedEdit1.Text = " / / " (same spaces)
    Me.MaskedEdit1.Mask = "###/##/####"

    And:
    Me.MaskedEdit1.Mask = "###/##/####"
    Me.MaskedEdit1.Text = " / / " (same spaces)

    And:
    Me.MaskedEdit1.Text = ""
    Me.MaskedEdit1.Mask = "###/##/####"

    Any ideas?


    Thanks
    SteveH

  2. #2
    Join Date
    Jan 2003
    Posts
    23

    Re: Still can't clear Masked Edit Box - VB.Net

    There is no "mask" property in vb.net.
    here is a class to aid in performing the same operation as the mask property

    Initialize this instead of the standard textbox class.

    If you need help let me know.

    '*********Begin Code**********
    Public Class MaskedBox
    Inherits System.Windows.Forms.TextBox

    Private aMskMask() As Char
    Private aMask() As Char
    Private tmpMask As String
    Private a As Integer

    #Region " Windows Form Designer generated code "

    Public Sub New()
    MyBase.New()
    InitializeComponent()
    End Sub

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    If disposing Then
    If Not (components Is Nothing) Then
    components.Dispose()
    End If
    End If
    MyBase.Dispose(disposing)
    End Sub

    Private components As System.ComponentModel.IContainer

    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    '
    'MaskedBox
    '
    Me.Name = "MaskedBox"

    End Sub

    #End Region

    Public Property Mask() As String

    Get
    Return tmpMask
    End Get

    Set(ByVal Value As String)
    tmpMask = Value
    SetMask()
    End Set

    End Property

    Private Sub SetMask()

    On Error Resume Next

    Me.Text = tmpMask
    Me.Text = Me.Text.Replace("#", "_")
    Me.Text = Me.Text.Replace("&", "_")

    ReDim aMask(Me.Text.Length - 1)
    ReDim aMskMask(Me.Text.Length - 1)

    For a = 0 To tmpMask.Length - 1
    If tmpMask.Substring(a, 1) = "#" Or tmpMask.Substring(a, 1) = "&" Then
    aMask.SetValue(CType("_", Char), a)
    Else
    aMask.SetValue(CType(tmpMask.Substring(a, 1), Char), a)
    End If
    Next

    For a = 0 To tmpMask.Length - 1
    aMskMask.SetValue(CType(tmpMask.Substring(a, 1), Char), a)
    Next

    End Sub

    Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
    Dim tmpset = Me.SelectionStart

    On Error Resume Next

    Select Case e.KeyCode
    Case Keys.Delete
    Me.Text = ""
    For a = tmpset To aMask.Length - 1
    Select Case aMskMask.GetValue(a + 1)
    Case ".", "-", "\", "/", ","
    aMask.SetValue(aMask.GetValue(a + 2), a)
    a = a + 1
    Case Else
    aMask.SetValue(aMask.GetValue(a + 1), a)
    End Select
    Next

    aMask.SetValue(CType("_", Char), aMask.Length - 1)

    e.Handled = True

    Me.Text = ""
    For a = 0 To aMask.Length - 1
    Me.Text += aMask.GetValue(a)
    Next
    Me.SelectionStart = tmpset

    End Select

    End Sub

    Protected Overrides Sub OnKeyUp(ByVal e As System.Windows.Forms.KeyEventArgs)
    e.Handled = True
    End Sub

    Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
    Dim tmpset = Me.SelectionStart
    Dim tmpCH As Char

    On Error Resume Next

    If Asc(e.KeyChar) = 8 Then
    Select Case CType(aMskMask.GetValue(tmpset - 1), String)
    Case ".", "-", "\", "/", ","
    tmpset = tmpset - 2
    aMask.SetValue(CType("_", Char), tmpset)
    tmpset = tmpset - 1
    Case Else
    tmpset = tmpset - 1
    aMask.SetValue(CType("_", Char), tmpset)
    tmpset = tmpset - 1
    End Select

    e.Handled = True

    Me.Text = ""
    For a = 0 To aMask.Length - 1
    Me.Text += aMask.GetValue(a)
    Next
    Me.SelectionStart = tmpset + 1

    ElseIf Char.IsControl(e.KeyChar) Then

    Else
    Select Case aMskMask.GetValue(tmpset)
    Case ".", "-", "\", "/", ","
    tmpset = tmpset + 1
    If aMskMask.GetValue(tmpset) = "#" Then
    If Char.IsDigit(e.KeyChar) Then
    aMask.SetValue(e.KeyChar, tmpset)
    Else
    tmpset = tmpset - 1
    End If
    ElseIf aMskMask.GetValue(tmpset) = "&" Then
    aMask.SetValue(e.KeyChar, tmpset)
    End If

    Case Else
    If aMskMask.GetValue(tmpset) = "#" Then
    If Char.IsDigit(e.KeyChar) Then
    aMask.SetValue(e.KeyChar, tmpset)
    Else
    tmpset = tmpset - 1
    End If
    ElseIf aMskMask.GetValue(tmpset) = "&" Then
    aMask.SetValue(e.KeyChar, tmpset)
    End If

    End Select

    e.Handled = True

    Me.Text = ""
    For a = 0 To aMask.Length - 1
    Me.Text += aMask.GetValue(a)
    Next
    Me.SelectionStart = tmpset + 1

    End If
    End Sub

    Public Sub SetText(ByVal txt As String)

    On Error Resume Next

    If txt = "" Then
    For a = 0 To tmpMask.Length - 1
    If tmpMask.Substring(a, 1) = "#" Or tmpMask.Substring(a, 1) = "&" Then
    aMask.SetValue(CType("_", Char), a)
    Else
    aMask.SetValue(CType(tmpMask.Substring(a, 1), Char), a)
    End If
    Next
    Else
    For a = 0 To txt.Length - 1
    If tmpMask.Substring(a, 1) = "#" Or tmpMask.Substring(a, 1) = "&" Then
    aMask.SetValue(CType(txt.Substring(a, 1), Char), a)
    Else
    aMask.SetValue(CType(tmpMask.Substring(a, 1), Char), a)
    End If
    Next

    End If

    Me.Text = ""
    For a = 0 To aMask.Length - 1
    Me.Text += aMask.GetValue(a)
    Next

    End Sub

    End Class

  3. #3
    Join Date
    Oct 2008
    Posts
    1
    Ummmmm...........if you're indeed using VB.NET, just use the .CtlText property to clear the text. .Text does nothing. And yes, .Mask still exists. I'm currently using this in a VB.NET app that I just finished.

    Here's my Sub Procedure that I call from a Module to clear an AxMaskEdBox.

    Using this line to call from wherever:
    txtStartDate = ClearAxMaskEdBox(txtStartDate)

    Sub Procedure from the module:
    Sub ClearAxMaskEdBox(ByRef box As AxMSMask.AxMaskEdBox)
    Dim tmpMask As String

    With box
    tmpMask = .Mask
    .Mask = ""
    .CtlText = ""
    .Mask = tmpMask
    End With
    End Sub


    Have fun with that.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •