0

さて、このコードを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 と同じイベントがないことがわかりました...どうすればそれらのイベントを追加できますか?

4

1 に答える 1

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

読み込み中イベント

 Dim RichTextBox1 As New WindowsFormsApplication1.MyRichTextBox
    Me.Controls.Add(RichTextBox1)
于 2013-04-09T05:13:27.430 に答える