vb.net 在 .NET 中捕获 CTRL+V 或粘贴到文本框中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15958250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:13:49  来源:igfitidea点击:

Capture CTRL+V or paste in a textbox in .NET

vb.netformattinghookrichtextboxpaste

提问by sinDizzy

VB.NET 2010 - I have a RichTextbox in which the user can manually enter data or copy/paste from another source. After the data is complete he hits go and a few key words are highlighted. My issue is that if he copy/pastes from another source the formatting also gets copied. Well sometimes the outside source has a white font and my textbox has a white background so it appears like he pasted nothing and he does it again and again.

VB.NET 2010 - 我有一个 RichTextbox,用户可以在其中手动输入数据或从其他来源复制/粘贴。数据完成后,他点击 go 并突出显示几个关键词。我的问题是,如果他从另一个来源复制/粘贴,格式也会被复制。好吧,有时外部来源有白色字体,而我的文本框有白色背景,所以看起来他什么也没粘贴,而且他一次又一次地粘贴。

What I'm looking for is a way to intercept the paste action into the textbox so that I can take that text and paste it as pure ASCII without formatting.

我正在寻找的是一种将粘贴操作拦截到文本框中的方法,以便我可以将该文本粘贴为纯 ASCII 而不进行格式化。

Edit after experimenting with KeyDown

尝试使用 KeyDown 后进行编辑

Private Sub txtRch_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtRch.KeyDown
    If e.Modifiers = Keys.Control AndAlso e.KeyCode = Keys.V Then
        With txtRch
            Dim i As Integer = .SelectionStart          'cache the current position
            .Select(0, i)                               'select text from start to current position
            Dim s As String = .SelectedText             'copy that text to a variable
            .Select(i, .TextLength)                     'now select text from current position to end
            Dim t As String = .SelectedText             'copy that text to a variable
            Dim u As String = s & Clipboard.GetText(TextDataFormat.UnicodeText) & t 'now concatenate the first chunk, the new text, and the last chunk
            .Clear()                                    'clear the textbox
            .Text = u                                   'paste the new text back into textbox
            .SelectionStart = i                         'put cursor back to cached position
        End With

        'the event has been handled manually
        e.Handled = True
    End If
End Sub

This seems to work and all my text gets retained and its all ASCII. I think if I wanted to take a step further I could also take the font and forecolor of my RichTextbox, select all text, and then assign the font and forecolor to the selection.

这似乎有效,并且我的所有文本都被保留并且全部为 ASCII。我想如果我想更进一步,我还可以采用 RichTextbox 的字体和前景色,选择所有文本,然后将字体和前景色分配给选择。

回答by LarsTech

In most cases, examining the KeyDown event should be good enough along with using a temporary RichTextBox to modify the incoming text:

在大多数情况下,检查 KeyDown 事件并使用临时 RichTextBox 来修改传入的文本应该足够了:

Private Sub RichTextBox1_KeyDown(sender As Object, e As KeyEventArgs) _
                                 Handles RichTextBox1.KeyDown
  If e.Modifiers = Keys.Control AndAlso e.KeyCode = Keys.V Then

    Using box As New RichTextBox
      box.SelectAll()
      box.SelectedRtf = Clipboard.GetText(TextDataFormat.Rtf)
      box.SelectAll()
      box.SelectionBackColor = Color.White
      box.SelectionColor = Color.Black
      RichTextBox1.SelectedRtf = box.SelectedRtf
   End Using

   e.Handled = True
  End If
End Sub

Note: Missing any error checking.

注意:缺少任何错误检查。