【问题标题】:Why does my RichTextBox not activate its events vb.net为什么我的 RichTextBox 没有激活它的事件 vb.net
【发布时间】:2013-04-09 04:11:14
【问题描述】:

好的,所以我在 stackoverflow 上找到了这段代码,并在我的项目中将它实现到了一个新的类文件中。

    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 相同的事件...如何添加这些事件?

【问题讨论】:

    标签: .net vb.net events richtextbox


    【解决方案1】:

    试试这个代码。

    在类中添加 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
    

    在加载事件中

     Dim RichTextBox1 As New WindowsFormsApplication1.MyRichTextBox
        Me.Controls.Add(RichTextBox1)
    

    【讨论】:

      猜你喜欢
      • 2011-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-27
      • 1970-01-01
      相关资源
      最近更新 更多