2011-08-17 76 views
1

我是新來的VB應用程序。我的基本背景是C.我剛剛安裝了VB,並從谷歌和微軟幫助中心學習東西。並且我正在爲我正在做的事情而感到樂趣,但是我在某個時候遇到了困難,而且在富文本框中出現了這種情況。是否有任何方法可以跟蹤在VB中對Rich textbox文本的跟蹤?這樣當用戶點擊新行時(即輸入),我可以附加一些文本,並在退格時執行一些任務。我如何跟蹤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 

我只想當用戶點擊新的生產線,但我無法弄清楚什麼條件可以made.and任何好的鏈接,做一些任務VB和文件在VB或其Windows應用程序將不勝感激太。預先感謝您

+0

** 1。**您是在談論VB還是VB.NET? ** 2。** VB.NET本身沒有'RichtTextBox'。你使用哪種GUI框架?的WinForms? WPF?或者是其他東西? - 請相應地標記您的問題。 ** 3。**該代碼應該是VB代碼嗎?看起來不像它。 – stakx

+0

它的視覺基本2010表達我使用和抱歉,老實說,我不知道什麼是vb和vb.net – niko

+0

@niko VB.Net是visual basic的現代版本。 VB也可以引用VB 6.0,它是1998年發佈的一個不使用.NET框架的舊版本。大多數關於VB的問題都可以認爲是關於.NET的版本,因爲6.0的答案有很大的不同,所以請求的人一定要提及它。 – briddums

回答

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 
0

所有你需要做的是檢查輸入或退格鍵被按下,這樣的:

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 

注意,選擇案例是一樣的作爲C中的開關。