繁体   English   中英

为什么我的RichTextBox无法激活其事件vb.net

[英]Why does my RichTextBox not activate its events vb.net

好的,所以我在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没有相同的事件……我将如何添加这些事件?

试试这个代码。

在类旁边添加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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM