cRuLo Posted June 22, 2003 Posted June 22, 2003 (edited) How do I use this message in VB.NET. I have sendmessage declared and it works with other messages, just not this one. Public Function GetWordUnderCursor(ByVal x As Integer, ByVal y As Integer) As String Dim pt As PointAPI Dim As Integer Dim ch As String Dim txt As String Dim txtlen As Integer Dim _start As Integer Dim _end As Integer pt.x = x pt.y = y ' position of character under cursor = SendMessage(rtbData.Handle, EM_CHARFROMPOS, 0&, pt) 'This shyt wont ing work! If <= 0 Then Exit Function End If txt = rtbData.Text ' get start position of word under cursor For _start = To 1 Step -1 ch = Mid$(txt, _start, 1) If ch = Chr(32) Or ch = vbCr Or ch = vbLf Or ch = vbNewLine Then Exit For Next _start _start = _start + 1 ' get end position of word under cursor txtlen = Len(txt) For _end = To txtlen ch = Mid$(txt, _end, 1) If ch = Chr(32) Or ch = vbCr Then Exit For Next _end _end = _end - 1 End Function the x and y values are grabbed from the MouseMove function and passed over to this code. When it goes to run the " = SendMessage(rtbData.Handle, EM_CHARFROMPOS, 0&, pt)" part, I get an Invalid parameter error or something... what's up? Everything is declared. This is how you did it in VB6...exactly how. For some reason all the "p o s" (without the spaces) were replaced with three whitespaces. Edited June 22, 2003 by cRuLo Quote
*Gurus* divil Posted June 22, 2003 *Gurus* Posted June 22, 2003 You do know the .NET richtextbox has a GetCharFromPosition method, right? Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Leaders dynamic_sysop Posted June 23, 2003 Leaders Posted June 23, 2003 when ever i tried GetCharFromPosition it always returned the first item in the richtextbox , so here's a way i put together which may help. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim x As Integer x = RichTextBox1.SelectionStart() '/// where the cursor is in the rtf box's text RichTextBox1.SelectionLength = 1 '/// get the character at that position by making selectionlength 1. MessageBox.Show(RichTextBox1.SelectedText) End Sub Quote
Leaders dynamic_sysop Posted June 23, 2003 Leaders Posted June 23, 2003 here ya go , not sure if this is what you want , but when you move the cursor over the richtextbox it will return the character under the cursor : Private Sub RichTextBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseMove Me.Text = GetWordUnderCursor(e.X, e.Y) '/// get the current letter by cursor position. End Sub Public Function GetWordUnderCursor(ByVal X As Integer, ByVal Y As Integer) As String Dim point As System.Drawing.Point = New System.Drawing.Point() point.X = X point.Y = Y Dim c As Char = RichTextBox1.GetCharFromPosition(point) Return c End Function Quote
Leaders dynamic_sysop Posted June 24, 2003 Leaders Posted June 24, 2003 i have put together yet another way of getting the char at the position the cursor stopped at in the richtextbox : Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click With RichTextBox1 .ScrollToCaret()'/// get the cursor ( selection start ) .SelectionLength = 1'/// get the char ( s ) you want, 1 retrieves the first char. MessageBox.Show(.SelectedText) End With End Sub Quote
Recommended Posts