2013-04-09 63 views
0

好吧,所以我在stackoverflow上找到了這個代碼,並且在我的項目中將它實現爲一個新的類文件。爲什麼我的RichTextBox沒有激活它的事件vb.net

Imports System 
Imports System.Collections.Generic 
Imports System.Text 
Imports System.Runtime.InteropServices 
Imports System.Windows.Forms 

Namespace WindowsFormsApplication1 

    Public Class MyRichTextBox 
     Inherits RichTextBox 
     <DllImport("user32.dll", CharSet:=CharSet.Auto)> _ 
     Public Shared Function GetScrollPos(ByVal hWnd As IntPtr, ByVal nBar As Integer) As Integer 
     End Function 

     <DllImport("user32.dll")> _ 
     Private Shared Function SetScrollPos(ByVal hWnd As IntPtr, ByVal nBar As Integer, ByVal nPos As Integer, ByVal bRedraw As Boolean) As Integer 
     End Function 

     Private Const SB_HORZ As Integer = &H0 
     Private Const SB_VERT As Integer = &H1 

     ''' <summary> 
     ''' Gets and Sets the Horizontal Scroll position of the control. 
     ''' </summary> 
     Public Property HScrollPos() As Integer 
      Get 
       Return GetScrollPos(DirectCast(Me.Handle, IntPtr), SB_HORZ) 
      End Get 
      Set(ByVal value As Integer) 
       SetScrollPos(DirectCast(Me.Handle, IntPtr), SB_HORZ, value, True) 
      End Set 
     End Property 

     ''' <summary> 
     ''' Gets and Sets the Vertical Scroll position of the control. 
     ''' </summary> 
     Public Property VScrollPos() As Integer 
      Get 
       Return GetScrollPos(DirectCast(Me.Handle, IntPtr), SB_VERT) 
      End Get 
      Set(ByVal value As Integer) 
       SetScrollPos(DirectCast(Me.Handle, IntPtr), SB_VERT, value, True) 
      End Set 
     End Property 
    End Class 
End Namespace 

當我將代碼實現到我的項目中後,我意識到要替換我的RichTextBox,我將不得不更改大部分代碼。尋求更快的方式來做到這一點,我把下面的代碼放在我的form1_Load事件中。

RichTextBox1 = New MyRichTextBox 

所以現在RichTextBox1是MyRichTextBox

因爲MyRichTextBox實現RichTextBox中應該具有相同的事件。

但我的RichTextbox.TextChanged事件不起作用。現在,如果我從form1_load中刪除上面的那一行,它可以正常工作。怎麼了?

編輯

所以我發現MyRichTextBox不具有相同的事件作爲一個RichTextBox ......我怎麼會添加這些事件?

回答

0

試試這個代碼。

添加TextChanged事件中端類

例如

Imports System 
    Imports System.Collections.Generic 
    Imports System.Text 
    Imports System.Runtime.InteropServices 
    Imports System.Windows.Forms 
    Namespace WindowsFormsApplication1 

     Public Class MyRichTextBox 
      Inherits RichTextBox 
      <DllImport("user32.dll", CharSet:=CharSet.Auto)> _ 
      Public Shared Function GetScrollPos(ByVal hWnd As IntPtr, ByVal nBar As Integer) As Integer 
      End Function 

      <DllImport("user32.dll")> _ 
      Private Shared Function SetScrollPos(ByVal hWnd As IntPtr, ByVal nBar As Integer, ByVal nPos As Integer, ByVal bRedraw As Boolean) As Integer 
      End Function 

      Private Const SB_HORZ As Integer = &H0 
      Private Const SB_VERT As Integer = &H1 

...... 
......... 
.......... 
........ 
...... 


''' Add the Event 



      Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs) 

       MsgBox(Me.Text) 
       MyBase.OnTextChanged(e) 
      End Sub 

     End Class 

    End Namespace 

在Load事件

Dim RichTextBox1 As New WindowsFormsApplication1.MyRichTextBox 
    Me.Controls.Add(RichTextBox1) 
相關問題