none
Restrict User From Pasting Irrelevant String RRS feed

  • Question

  • Hi,

    If we want a user to past only Decimal String, we can use This method. Solution By Karen Payne.

    Public Class CustomTextbox
        Inherits TextBox
        Protected Overrides Sub WndProc(ByRef m As Message)
            If m.Msg = WM_PASTE Then
                Dim value As String = Text
                value = value.Remove(SelectionStart, SelectionLength)
                value = value.Insert(SelectionStart, Clipboard.GetText)
                Dim result As Decimal = 0
                If Not Decimal.TryParse(value, result) Then
                    Return
                End If
            End If
            MyBase.WndProc(m)
        End Sub
    End Class

    I made small change for a user to paste integer string only which is

    Dim result As Integer = 0
                If Not Integer.TryParse(value, result) Then
                    Return
                End If

    But this code is not working as I paste "Character space", it even stops responding.

    Now I want to know how will we do the followings.

    1)Allow a user to past Alphabets only. If there is no numbers or special characters in the Clipboard.

    2)Allow a user to past Integers only. If there is no Alphabets or special characters in the Clipboard.

    3)Allow a user to Past string like "Non-standard". If there is no other special characters or numbers in the Clipboard.

    4)Allow a user to Past numbers like "092-000-000-0000". If there is no other special characters or Alphabets in the Clipboard.

    Hope this might be simple enough to understand.

    Note: Separate class for all. 

    Thanks in advance.


    Habib Ur Rehman

    Thursday, January 17, 2019 4:47 PM

Answers

  • Here is a roadmap, using an enum as per below, write logic to handle your various cases.

    Public Class NumericTextbox
        Inherits TextBox
    
        Const WM_PASTE As Integer = &H302
        Public Enum DecimalDigits
            ZeroDigits
            OneDigit
            TwoDigits
            ThreeDigits
        End Enum
        Public Property DecimalPlaces As DecimalDigits
        <ComponentModel.Browsable(False)>
        Public ReadOnly Property DoubleValue As Double
            Get
                If String.IsNullOrWhiteSpace(Text) Then
                    Return 0
                Else
                    Return CDbl(Text)
                End If
            End Get
        End Property
        <ComponentModel.Browsable(False)>
        Public ReadOnly Property DecimalValue As Decimal
            Get
                If String.IsNullOrWhiteSpace(Text) Then
                    Return 0
                Else
                    Return CDec(Text)
                End If
            End Get
        End Property
        <ComponentModel.Browsable(False)>
        Public ReadOnly Property IntegerValue As Integer
            Get
                If String.IsNullOrWhiteSpace(Text) Then
                    Return 0
                Else
                    Return CInt(Text)
                End If
            End Get
        End Property
        <ComponentModel.Browsable(False)>
        Public ReadOnly Property FormattedValue As String
            Get
                Select Case DecimalPlaces.ToString
                    Case "ZeroDigits"
                        Return DoubleValue.ToString("F0")
                    Case "OneDigit"
                        Return DoubleValue.ToString("F1")
                    Case "TwoDigits"
                        Return DoubleValue.ToString("F2")
                    Case "ThreeDigits"
                        Return DoubleValue.ToString("F3")
                    Case Else
                        Return ""
                End Select
            End Get
        End Property
        Protected Overrides Sub OnKeyPress(e As KeyPressEventArgs)
            Dim value As String = Text
    
            value = value.Remove(SelectionStart, Me.SelectionLength)
            value = value.Insert(SelectionStart, e.KeyChar)
    
            e.Handled = value.LastIndexOf("-", StringComparison.Ordinal) > 0 Or
                Not (Char.IsControl(e.KeyChar) OrElse Char.IsDigit(e.KeyChar) OrElse
                     (e.KeyChar = "."c And
                      Not Text.Contains(".") Or e.KeyChar = "."c And
                      SelectedText.Contains(".")) OrElse (e.KeyChar = "-"c And
                                                             Me.SelectionStart = 0))
    
            MyBase.OnKeyPress(e)
    
        End Sub
        Public Enum PasteOptions
            OnlyAllowCharacters
            OnlyAllowInt
            OnlyAllowPastNumbers
        End Enum
        Public Property PastingOptions() As PasteOptions
        Private Function OnlyAllowCharacters() As Boolean
            Throw New NotImplementedException()
        End Function
        Public Function OnlyAllowInt() As Boolean
            Throw New NotImplementedException()
        End Function
        Public Function OnlyAllowPastNumbers() As Boolean
            Throw New NotImplementedException()
        End Function
        Protected Overrides Sub WndProc(ByRef m As Message)
            If m.Msg = WM_PASTE Then
                Select Case PastingOptions
                    Case PasteOptions.OnlyAllowCharacters
                        ' write logic here
                    Case PasteOptions.OnlyAllowInt
                        ' write logic here
                    Case PasteOptions.OnlyAllowPastNumbers
                        ' write logic here
                End Select
            End If
            MyBase.WndProc(m)
        End Sub
    End Class

    Highlighted areas would be the start of changes. From here where there is NotImplementedException you would write logic to handle you requirements.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, January 17, 2019 7:32 PM
    Moderator

All replies

  • Hi

    Well, I for one will not try to assist considering your odd sometimes readable sometimes unreadable posts!

    Regards Les, Livingston, Scotland

    Thursday, January 17, 2019 5:50 PM
  • Here is a roadmap, using an enum as per below, write logic to handle your various cases.

    Public Class NumericTextbox
        Inherits TextBox
    
        Const WM_PASTE As Integer = &H302
        Public Enum DecimalDigits
            ZeroDigits
            OneDigit
            TwoDigits
            ThreeDigits
        End Enum
        Public Property DecimalPlaces As DecimalDigits
        <ComponentModel.Browsable(False)>
        Public ReadOnly Property DoubleValue As Double
            Get
                If String.IsNullOrWhiteSpace(Text) Then
                    Return 0
                Else
                    Return CDbl(Text)
                End If
            End Get
        End Property
        <ComponentModel.Browsable(False)>
        Public ReadOnly Property DecimalValue As Decimal
            Get
                If String.IsNullOrWhiteSpace(Text) Then
                    Return 0
                Else
                    Return CDec(Text)
                End If
            End Get
        End Property
        <ComponentModel.Browsable(False)>
        Public ReadOnly Property IntegerValue As Integer
            Get
                If String.IsNullOrWhiteSpace(Text) Then
                    Return 0
                Else
                    Return CInt(Text)
                End If
            End Get
        End Property
        <ComponentModel.Browsable(False)>
        Public ReadOnly Property FormattedValue As String
            Get
                Select Case DecimalPlaces.ToString
                    Case "ZeroDigits"
                        Return DoubleValue.ToString("F0")
                    Case "OneDigit"
                        Return DoubleValue.ToString("F1")
                    Case "TwoDigits"
                        Return DoubleValue.ToString("F2")
                    Case "ThreeDigits"
                        Return DoubleValue.ToString("F3")
                    Case Else
                        Return ""
                End Select
            End Get
        End Property
        Protected Overrides Sub OnKeyPress(e As KeyPressEventArgs)
            Dim value As String = Text
    
            value = value.Remove(SelectionStart, Me.SelectionLength)
            value = value.Insert(SelectionStart, e.KeyChar)
    
            e.Handled = value.LastIndexOf("-", StringComparison.Ordinal) > 0 Or
                Not (Char.IsControl(e.KeyChar) OrElse Char.IsDigit(e.KeyChar) OrElse
                     (e.KeyChar = "."c And
                      Not Text.Contains(".") Or e.KeyChar = "."c And
                      SelectedText.Contains(".")) OrElse (e.KeyChar = "-"c And
                                                             Me.SelectionStart = 0))
    
            MyBase.OnKeyPress(e)
    
        End Sub
        Public Enum PasteOptions
            OnlyAllowCharacters
            OnlyAllowInt
            OnlyAllowPastNumbers
        End Enum
        Public Property PastingOptions() As PasteOptions
        Private Function OnlyAllowCharacters() As Boolean
            Throw New NotImplementedException()
        End Function
        Public Function OnlyAllowInt() As Boolean
            Throw New NotImplementedException()
        End Function
        Public Function OnlyAllowPastNumbers() As Boolean
            Throw New NotImplementedException()
        End Function
        Protected Overrides Sub WndProc(ByRef m As Message)
            If m.Msg = WM_PASTE Then
                Select Case PastingOptions
                    Case PasteOptions.OnlyAllowCharacters
                        ' write logic here
                    Case PasteOptions.OnlyAllowInt
                        ' write logic here
                    Case PasteOptions.OnlyAllowPastNumbers
                        ' write logic here
                End Select
            End If
            MyBase.WndProc(m)
        End Sub
    End Class

    Highlighted areas would be the start of changes. From here where there is NotImplementedException you would write logic to handle you requirements.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, January 17, 2019 7:32 PM
    Moderator
  • Here is a roadmap, using an enum as per below, write logic to handle your various cases.

        Protected Overrides Sub WndProc(ByRef m As Message)
            If m.Msg = WM_PASTE Then
                Select Case PastingOptions
                    Case PasteOptions.OnlyAllowCharacters
                        ' write logic here
                    Case PasteOptions.OnlyAllowInt
                        ' write logic here
                    Case PasteOptions.OnlyAllowPastNumbers
                        ' write logic here
                End Select
            End If
            MyBase.WndProc(m)
        End Sub

    First of all Thank you for your time Karen.

    I'm confuse with that logic.

    Select Case PastingOptions

    Case PasteOptions.OnlyAllowCharacters

    Return

    End Select

    This blocks everything I mean characters also. and if I change this as

    Case Not PasteOptions.OnlyAllowCharacters

    Then It allows everything including numbers and special characters.

    Please explain this logic part.


    Habib Ur Rehman


    Friday, January 18, 2019 10:04 AM
  • There is no logic, all I was doing was providing direction to how you might approach writing logic. It's up to you to figure it out. It will not be easy by any means and personally never had a need to have all the logic you want, instead I would avoid writing that logic and instead handle it when using whatever has been entered into the TextBox.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, January 18, 2019 10:15 AM
    Moderator
  • Thanks Kareninstructor

    Your Help led me to what I needed as below

        Protected Overrides Sub WndProc(ByRef m As Message)
            If m.Msg = WM_PASTE Then
    
                Dim value As String = Text
                value = value.Remove(SelectionStart, SelectionLength)
                value = value.Insert(SelectionStart, Clipboard.GetText)
                If Not System.Text.RegularExpressions.Regex.IsMatch(value, "^[A-Z- a-z]+$") Then
                    Return
                End If
            End If
            MyBase.WndProc(m)
        End Sub


    Habib Ur Rehman

    Thursday, March 26, 2020 4:07 PM