日期:2014-05-20  浏览次数:21072 次

RichTextBox怎样获取鼠标所在位置
目的:用 GetcharIndexformPosition(point)到获取鼠标位置的字符长度
但是不知道 point怎么去获取

------解决方案--------------------
不好意思,上面那个只能显示选定的字符串的最后一个字符是框内全部字符的第几个


下面这个能显示出选择了多少个字符



VB.NET code

Public Class Form1

    Dim pt As Point
    Dim ptB As Point
    Dim b As Boolean

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        b = False
    End Sub

    Private Sub RichTextBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseDown
        If b <> True Then
            ptB.X = e.X
            ptB.Y = e.Y
        End If
        b = True
    End Sub

    Private Sub RichTextBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseMove
        Dim i As Integer
        Dim j As Integer
        If b Then
            pt.X = e.X
            pt.Y = e.Y
            j = RichTextBox1.GetCharIndexFromPosition(ptB)
            i = RichTextBox1.GetCharIndexFromPosition(pt)
            Textbox1.Text = Math.Abs(i - j) '文本框内现实的数值就是个数
        End If
    End Sub

    Private Sub RichTextBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseUp
        b = False
    End Sub
End Class