Top / JUNK / HexadecimalTextBox

16進数入力用テキストボックス

Public Class HexadecimalTextBox
    Inherits TextBox

    Private Const WM_PASTE As Integer = &H302

    Private Shared CTRL_BASE As Integer = AscW("A"c) - 1
    Private Shared CTRL_A As Char = ChrW(AscW("A"c) - CTRL_BASE)
    Private Shared CTRL_C As Char = ChrW(AscW("C"c) - CTRL_BASE)
    Private Shared CTRL_V As Char = ChrW(AscW("V"c) - CTRL_BASE)
    Private Shared CTRL_X As Char = ChrW(AscW("X"c) - CTRL_BASE)
    Private Shared CTRL_Z As Char = ChrW(AscW("Z"c) - CTRL_BASE)

    Protected Overrides Sub OnKeyPress(e As KeyPressEventArgs)
        Select Case e.KeyChar
            Case CTRL_A
                Me.SelectAll()
                e.Handled = True
            Case CTRL_C, CTRL_V, CTRL_X, CTRL_Z
            Case ControlChars.Back
            Case Else
                e.KeyChar = RestrictChar(e.KeyChar)
                e.Handled = (e.KeyChar = ChrW(0))
        End Select
        MyBase.OnKeyPress(e)
    End Sub

    Protected Overridable Function RestrictChar(c As Char) As Char
        Select Case c
            Case "0"c To "9"c, "a"c To "f"c
                Return c
            Case "A"c To "F"c
                Return Char.ToLower(c)
            Case Else
                Return ChrW(0)
        End Select
    End Function

    Protected Overrides Sub WndProc(ByRef m As Message)
        Select Case m.Msg
            Case WM_PASTE
                If Clipboard.ContainsText() Then
                    Dim sb As New Text.StringBuilder()
                    Dim pasteMaxLength As Integer
                    If MaxLength > 0 Then
                        pasteMaxLength = MaxLength - TextLength + SelectionLength
                    Else
                        pasteMaxLength = Integer.MaxValue - TextLength + SelectionLength
                    End If
                    For Each c As Char In Clipboard.GetText()
                        If sb.Length >= pasteMaxLength Then
                            Exit For
                        End If
                        Dim rc As Char = RestrictChar(c)
                        If rc <> ChrW(0) Then
                            sb.Append(rc)
                        End If
                    Next
                    MyBase.Paste(sb.ToString())
                End If
            Case Else
                MyBase.WndProc(m)
        End Select
    End Sub

End Class



トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   最終更新のRSS
Last-modified: 2020-02-24 (月) 20:50:35 (40d)