65.9K
CodeProject 正在变化。 阅读更多。
Home

FlexMaskEditBox - 一个掩码编辑控件

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.64/5 (36投票s)

2003年4月29日

2分钟阅读

viewsIcon

337788

downloadIcon

987

一个非常灵活的 MaskTextbox 控件

引言

为什么你需要使用 MaskEditBox?当只需要数字输入时,允许用户输入字母数字字符是不专业的。

验证输入和/或向输入错误的“打字员”提供反馈轻而易举。

使用代码

有三种类型的输入:字母、数字和日期。这三种类型的行为在键入时有所不同。

当将数字类型与小数一起使用(例如 mask:= "9999D##")并且用户键入小数表示形式(一个句点 (.) 或逗号 (,) 取决于国际设置时),光标会自动移动到小数点。此外,输入字符从右向左绘制。这是一个使用 FlexMaskEditBox 进行验证的示例,并通过 ErrorTxt 向用户提供反馈。

 
Private Sub FlexMaskEditBox1_Validating(ByVal sender As Object, 
    ByVal e As System.ComponentModel.CancelEventArgs) 
    Handles FlexMaskEditBox1.Validating, FlexMaskEditBox2.Validating, 
    FlexMaskEditBox3.Validating, FlexMaskEditBox5.Validating
    
    Dim fmb As FlxMaskBox.FlexMaskEditBox = CType(sender, 
        FlxMaskBox.FlexMaskEditBox)
    
    If fmb Is FlexMaskEditBox1 Then
        Try
            Dim Value As Decimal = Decimal.Parse(fmb.Text)
            If Value < 201.13 OrElse Value > 4027.55 Then
                fmb.ErrorTxt = "Value MUST between 201.13 
                                    and 4027.55"
                If CheckBox1.Checked Then e.Cancel = True
            Else
                fmb.ErrorTxt = ""
            End If
        Catch 'normal, this will never been evaluate

            fmb.ErrorTxt = "There is no Valid Value given"
            If CheckBox1.Checked Then e.Cancel = True
        End Try
    ElseIf fmb Is FlexMaskEditBox2 Then
        Try
            Dim dt As DateTime = DateTime.Parse(fmb.Text)
            If dt.Year < 1980 OrElse dt.Year > 2010 Then
                fmb.ErrorTxt = "The Range of valid Years 
                                    must be between 1980 an 2010"
                If CheckBox1.Checked Then e.Cancel = True
            Else
                fmb.ErrorTxt = ""
            End If
        Catch
            fmb.ErrorTxt = "Not A Valid Date, Try Again!"
            If CheckBox1.Checked Then e.Cancel = True
        End Try
    ElseIf fmb Is FlexMaskEditBox3 Then
        With fmb
            If .Text.Replace(" ", "").Length < 6 Then
                .ErrorTxt = "Not All Characters Are Given"
                If CheckBox1.Checked Then e.Cancel = True
            Else
                .ErrorTxt = ""
            End If
        End With
    ElseIf fmb Is FlexMaskEditBox5 Then
        With fmb
            If Not Regex.IsMatch(.Text, "^([\w-]+\.)*?
                    [\w-]+@[\w-]+\.([\w-]+\.)*?[\w]+$") Then
                    
                .ErrorTxt = "No valid Email address"
                If CheckBox1.Checked Then e.Cancel = True
            Else
                .ErrorTxt = ""
            End If
        End With
    End If
End Sub

使用掩码

(#) - (chr 48 - 57)

(9) - (chr 48 - 57) (-) minus sign

(?) - (chr 65 - 90) (chr 97 - 121)

(A)(C)  - (chr 65 - 90) (chr 97 - 121) (chr 48 - 57)

(a)(c)  - (chr 65 - 90) (chr 97 - 121) (chr 48 - 57) (chr 32 )

(&) - (chr 32 - 126) (chr 128 - 255)

(\)  Next char will be no mask char

(>) All chars next to the > sign will transformed in upper-case
    until the next mask char is an (<) or (\) or (|)

(}) Next 1 Char will transformed in upper-case
    
(<) All chars next to the < sign will transformed in lower-case
    until the next mask char is an (>) or (\) or (|)
    
(|) Stops next chars from Upper or Lower casing

({) Next 1 Char will transformed in lower-case

(d)(D) Decimal Point (#####d99) = 12345.67 or 12345,67  
       (depending on country settings)

(g)(G) Group Separator (##g###g###d##) = 12.345.678,90 or 12,345,678.90
       (depending on country settings)
  
(s)(S) Date Separator (##s##s####) = 12-12-2000 or 12/12/2000
       (depending on country settings)

因此,当你想在掩码中包含也作为掩码符号的字符时,请在前面使用 \。例如,##s##s1\9##(9 也是掩码符号,但 1 不是)。

将 FlexMaskEditBox 包含到你的项目中

在新项目中使用的 FlexMaskEditControl 控件的说明

  1. 启动 Microsoft Visual Studio .NET。
  2. 打开 FlexMaskEdit.vbproj 项目。
  3. 在“生成”菜单上,单击“生成解决方案”以生成 FlexMaskEdit.dll
  4. 关闭项目。
  5. 使用 Visual Basic .NET 创建一个新的 Windows 应用程序。默认情况下会创建 Form1。
  6. 在“工具”菜单上,单击“自定义工具箱”。
  7. 选择“.NET Framework 组件”选项卡。
  8. 单击“浏览”按钮。
  9. 导航到构建 FlexMaskEdit.dll 的文件夹(它应该是项目解压位置下的 bin 文件夹)。
  10. 选择 FlexMaskEdit.dll 并单击“打开”。它会自动将其添加到所选组件列表中并为你选中该项目。
  11. 单击“确定”关闭“自定义工具箱”对话框。请注意,FlexMaskEdit 控件现在出现在你的工具箱中。就是这样,现在你可以像使用 TextBox 一样绘制 FlexMaskEditBox

历史

  • 最初的源代码是用 VB6 编写的(几年前),但为了提高性能而进行了重写。

致谢

  • 非常感谢 Hans Scholten (电子邮件:wonen@wonen.com) 提供的意见。
© . All rights reserved.