在 C# 中的文本框中粘贴限制

发布于 2024-10-21 21:10:43 字数 163 浏览 1 评论 0原文

我已经完成了 textbox.shortcut= false,因此我限制在 textbox 中复制和粘贴。但我只想粘贴数值(这里我还限制了 key_press 只能粘贴数值),但完全粘贴不起作用,我只想粘贴数值。我怎样才能做到这一点?

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_press 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

猫瑾少女 2024-10-28 21:10:43

我会在 中处理该问题TextChanged 事件。
这将捕获粘贴、拖放和所有其他可能的情况。

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

倒数 2024-10-28 21:10:43

如果您要捕获按键,您可以检查 Ctrl-V 并使用类似的内容检查剪贴板内容,

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

然后检查 (this.Text + data) 看看它是否可以接受。

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.

东京女 2024-10-28 21:10:43

如果您基于文本框构建自己的控件,则可以覆盖文本框的 WndProc 事件:

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

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
难以启齿的温柔 2024-10-28 21:10:43

在 KeyDown 事件中尝试一下

 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

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
天生の放荡 2024-10-28 21:10:43

也许使用 onchange 事件,每次更改文本时,尝试解析它,排除每个非数字字符?

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

悸初 2024-10-28 21:10:43

例如,如果您使用 MaskedTextBox 控件并将掩码设置为 #####,那么您可以复制 a123b 这样的值并将其粘贴到掩码编辑控件中,它只会粘贴 123。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文