The RichTextBox control doesn't have a simple way to do this. Also, it's not really standard CUA/GUI behavior. Usually if there's a text box, selecting text is done via mouse drag or via keyboard (e.g. <SHIFT><END>).
That being said, of course it is possible to do. Much depends on how your RichTextBox control properties are set. If you can isolate carriage return/line feed characters, you can use the UpTo and Span methods to select portions of your text. Here's an example that will move the cursor to the immediate prior CRLF, and select text to the immediate subsequent CRLF:
Me.RichTextBox1.UpTo vbCrLf, False, False
Me.RichTextBox1.Span vbCrLf, True, True
MsgBox "Selected: " & Me.RichTextBox1.SelText
The drawback with this method is that you can't always depend on there being a handy CRLF at the end of the line. For example, if you are relying on automatic line (such as when you have Multiline=TRUE and no Horizontal ScrollBar), it becomes more tricky. Then you have to go into Win32 messages, with all the attendant interestingness.
Many of the messages you can send to a standard text box apply, so you can use the EM_LINEINDEX message to get the starting position of the line (using the RichTextBox's GetLineFromChar method, which in turn needs the offset using the RichTextBox's SelStart property). It's not trivial.
Anyway, I hope this gives you something to work with. Good luck!
BitBlt
Practically a Posting Shark
894 posts since Feb 2011
Reputation Points: 482
Solved Threads: 148
Skill Endorsements: 14
This behavior is caused by setting the RichTextBox1.Text value. When you do that, it resets the RichTextBox.SelStart value to zero, effectively nullifying any prior RichTextBox.SelBold settings. You should actually be using the RichTextBox1.SelText property instead of the .Text property.
Here's a little piece of "science experiment" code to demonstrate how you should be doing your task:
Private Sub cmdSelect_Click()
' variable just used as a counter
Static i As Integer
'set the starting point to the end of the existing text
RichTextBox1.SelStart = Len(RichTextBox1.Text)
' identify every "other" line to determine whether or not to bold
If i Mod 2 = 0 Then
RichTextBox1.SelBold = True
Else
RichTextBox1.SelBold = False
End If
' insert text at the SelStart point, using the SelBold property as set
RichTextBox1.SelText = vbNewLine + "String No." + CStr(i)
' increment your counter
i = i + 1
End Sub
Notice that by using .SelText, you are actually inserting text at the starting point. You have to be careful that .SelLength is zero, otherwise you'll be REPLACING any highlighted text.
Hope this helps! Good luck!
BitBlt
Practically a Posting Shark
894 posts since Feb 2011
Reputation Points: 482
Solved Threads: 148
Skill Endorsements: 14
Question Answered as of 1 Year Ago by
BitBlt