我是VB应用程序的新手。我的基本背景是C.我刚刚安装了VB并从谷歌和微软帮助中心学习东西。我正在玩我正在做的事情但是我被困在一个点,那就是在富文本框中。有没有办法在VB中跟踪Rich文本框文本?因此,当用户点击新行(即输入)时,我可以附加一些文本,并在他按下退格键时执行一些任务。我如何跟踪richtextbox。?
我找到了
stringer = RichTextBox1.Lines(0) to read lines
& vbNewLine for new line
我如何阅读该用户在vb富文本框中点击新行字符或退格键?因为在C我以前做过这样的事情
if a = 13; \\ ascii for new line and 8 for backspace
我只是想在用户点击新行时执行某项任务,但我无法弄清楚要做什么条件。在VB或其Windows应用程序上的vb和文档的任何良好链接都会受到赞赏。提前谢谢
答案 0 :(得分:0)
您需要链接到RichTextBox的KeyDown事件。在此事件中,您可以修改当前行的文本。用于向行添加文本的示例代码为:
Private Sub RichTextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyDown
Dim textToAdd As String = " ** text to add **"
'Fire when the Enter key is pressed
If e.KeyCode = Keys.Enter Then
'Get the current cursor position
Dim cursorPos As Integer = RichTextBox1.SelectionStart
'Get the current line index
Dim index As Integer = RichTextBox1.GetLineFromCharIndex(cursorPos)
'Load all the lines into a string array
'This has to be done since editing via RichTextBox1.Lines(index) = "" doesn't always work
Dim lines() As String = RichTextBox1.Lines
'Add the text to the correct line
lines(index) &= textToAdd
'Assign the text back to the RichTextBox
RichTextBox1.Lines = lines
'Move the cursor to the correct position
RichTextBox1.SelectionStart = cursorPos + textToAdd.Length
End If
End Sub
答案 1 :(得分:0)
您需要做的就是检查是否按下了Enter或BackSpace键,如下所示:
Private Sub RichTextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyDown
Select Case e.KeyCode
Case Keys.Enter
'Do stuff when Enter was pressed
Case Keys.Back
'Do stuff when BackSpace was pressed
End Select
End Sub
请注意,Select Case与C中的开关相同。