开发者

Paste Restriction in Textbox in C#

I have done the textbox.shortcut= false so I have restricted to copy and paste in the textbox. But I want to paste only numeric value (here I have also restricted in key_p开发者_StackOverflow社区ress that only numeric value will be put) but totally paste are not functioning, I want to paste only numeric value. How can I do that?


I would handle that in the TextChanged event.
That would catch paste, drag'n drop and all other possible scenarios.


If you are trapping the keypress, you could check for Ctrl-V and check the clipboard contents using something like

   IDataObject clipData = Clipboard.GetDataObject();
   string data = (string)clipData.GetData(System.Windows.Forms.DataFormats.Text);

You can then check (this.Text + data) to see if it is acceptable.


If you build your own control based on the textbox you can override the WndProc event of the textbox:

    #region -- WndProc(ref Message m) Event Handler --
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_PASTE)
        {
            //your code here
        }
    }
    #endregion


Try This in the KeyDown Event

 Public Sub TextBoxNumeric_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
    Try
        ' Allow navigation keyboard arrows
        If fAllowNavigationArrows(e) Then

            ' Handle pasted Text
            If e.Control AndAlso e.KeyCode = Keys.V Then
                Dim strInput As String = Clipboard.GetText()
                Dim strOutput As String = ""
                Dim intStart As Integer
                Dim strNewTxt As String

                e.SuppressKeyPress = True

                ' Preview paste data (removing non-number characters)
                For intI As Integer = 0 To strInput.Length - 1
                    If Char.IsDigit(strInput(intI)) Or (strInput(intI) = "." And strOutput.Length > 0 And strOutput.IndexOf(".") < 0) Then
                        strOutput += strInput(intI).ToString()
                    End If
                Next

                ' Select TextBox Object
                With DirectCast(sender, TextBox)

                    .SelectAll()
                    intStart = .SelectionStart
                    strNewTxt = .Text
                    strNewTxt = strNewTxt.Remove(.SelectionStart, .SelectionLength)

                    ' Remove selected text
                    strNewTxt = strNewTxt.Insert(.SelectionStart, strOutput)

                    ' Paste
                    .Text = strNewTxt
                    .SelectionStart = intStart + strOutput.Length

                End With

            End If

        End If

    Catch ex As Exception
        Throw ex
    End Try


Maybe use onchange event and every time when text is changed, try to parse it excluding every character that is not a number?


If you use the MaskedTextBox control and set the mask to #####, for example, then you can copy a value like a123b and paste it into the masked edit control and it will only paste 123.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜