捕获CTRL + V或粘贴在.NET中的文本框 [英] Capture CTRL+V or paste in a textbox in .NET

查看:169
本文介绍了捕获CTRL + V或粘贴在.NET中的文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

VB.NET 2010 - 我有一个RichTextbox,用户可以在其中手动输入数据或从其他源复制/粘贴。数据完成后,他会打开几个关键字。我的问题是,如果他从其他来源复制/粘贴格式也被复制。有时候,外面的来源有一个白色的字体,我的文本框有一个白色的背景,所以它看起来像他没有粘贴,他一次又一次地做。



我是什么寻找是一种方法来拦截粘贴到文本框中的行为,以便我可以把它作为纯粹的ASCII格式粘贴而不用格式。



使用KeyDown进行实验后编辑

  Private Sub txtRch_KeyDown(sender As Object,e As System.Windows.Forms.KeyEventArgs)处理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)'从开始到当前位置选择文本
Dim s As String = .SelectedText'将该文本复制到变量
.Select(i,.TextLength)'现在从curren选择文本t位置结束
Dim t As String = .SelectedText'将该文本复制到变量
Dim u As String = s& Clipboard.GetText(TextDataFormat.UnicodeText)& t'现在连接第一个块,新文本和最后一个块
.Clear()'清除文本框
.Text = u'将新文本粘贴回文本框
.SelectionStart = i'把光标放回缓存的位置
End With

'事件已被手动处理
e.Handled = True
End If
End Sub

这似乎起作用了,我所有的文本都被保留了,它的所有ASCII都被保留了。我想如果我想更进一步,我也可以采用我的RichTextbox的字体和前景色,选择所有文本,然后将字体和前景色分配给选择。

  Private Sub RichTextBox1_KeyDown(sender As Object,e As KeyEventArgs)_ 
处理RichTextBox1.KeyDown
如果e.Modifiers = Keys.Control AndAlso e.KeyCode = Keys。 V然后

使用box作为新的RichTextBox
box.SelectAll()
box.SelectedRtf = Clipboard.GetText(TextDataFormat.Rtf)
box.SelectAll()
box.SelectionBackColor = Color.White
box.SelectionColor = Color.Black
RichTextBox1.SelectedRtf = box.SelectedRtf
End使用

e.Handled = True
End If
End Sub

注意:缺少错误检查。


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.

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.

Edit after experimenting with 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.

解决方案

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

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.

这篇关于捕获CTRL + V或粘贴在.NET中的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆