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
在用键盘进行测试后进行编辑。
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的字体和前景色,选择所有的文本,然后分配字体和前景色。
1 个解决方案
#1
6
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.
注意:缺少任何错误检查。
#1
6
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.
注意:缺少任何错误检查。