Hi everyone, I am new to RichTextBox control in VB6
I wanted to select the entire line/row in the RichTextBox, where currently the cursor is.
How should I do it ?
The code should be paste in command button 'cmdSelect'
Thanks in advance :)

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!

The RichTextBox control doesn't have a simple way to do this. Also, it's not really standard...

Hey thanks BitBlt, It worked fine.:cool:

But I want more help with that.. Actually my assumption about CURSOR POSITION was wrong. I have a command button 'cmdSelect' which adds a string into RichTextBox as follows:
(Where 'i' is String)

Private Sub cmdSelect_Click()
RichTextBox1.Text = RichTextBox1.Text + vbNewLine + "String No." + i
i = i + 1
End Sub

I wanted to select the line where that 'String No.' is placed. I thought cursor would be at that line when the string is added, but it remains at the first line.
So how to select that line ? Or how to move cursor at that line ?

Hey thanks BitBlt, It worked fine.:cool:

But I want more help with that..

Okay I got following code for that:

Private Sub cmdSelect_Click()
StartChar = Len(RichTextBox1.Text)

RichTextBox1.Text = RichTextBox1.Text + vbNewLine + "String No." + i
i = i + 1

StopChar = Len(RichTextBox1.Text)

RichTextBox.SelStart = StartChar
RichTextBox.SelLength = StopChar - StartChar

End Sub

Though it doesn't select the entrie line, and only selects the entered string it is okay for me.
But problem occurs when I try to manipulate the selected text (like making it bold) using
code:

Private Sub cmdSelect_Click()
StartChar = Len(RichTextBox1.Text)

RichTextBox1.Text = RichTextBox1.Text + vbNewLine + "String No." + i
i = i + 1

StopChar = Len(RichTextBox1.Text)

RichTextBox.SelStart = StartChar
RichTextBox.SelLength = StopChar - StartChar

RichTextBox.SelBold = True

End Sub

It works good, but the effect (bold) dosen't remain. When I click 'cmdSelect' again, the previously BOLD string turns to normal and only newly entered string get bold.
How to solve this ?

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!

Hey that perfectly worked !! thanks a lot... :) :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.