Rich Text box masks Line Feed

J

Justin Roberts

Hi

I have noticed that the RTB control hides all Linefeed characters in it's
text properties. Unfortunately these LF characters are masked in the Length
properties also.

I need the LF characters to be returned in the RTB.Text / RTB.SelectedText
and the LF characters to be included correctly in the RTB.TextLength /
RTB.SelectedLength properties. Does anyone know how I can achieve this, or
must I abandon the notion of using the standard MS RTB control.

Much help appreciated since I have completely floundered without these
correct text properties.

Regards
Justin.
 
A

AMercer

I have noticed that the RTB control hides all Linefeed characters in it's
text properties. Unfortunately these LF characters are masked in the Length
properties also.

I need the LF characters to be returned in the RTB.Text / RTB.SelectedText
and the LF characters to be included correctly in the RTB.TextLength /
RTB.SelectedLength properties. Does anyone know how I can achieve this, or
must I abandon the notion of using the standard MS RTB control.

Much help appreciated since I have completely floundered without these
correct text properties.

When you say "RTB control", I assume you mean something like
rtb As System.Windows.Forms.RichTextBox
I don't get the behavior you describe. rtb.SelectedText contains line feeds
for me, and they are included in the length. If rtb contains linefeeds, then
InStr(rtb.Text, vbLf) > 0.
What's the problem?
 
J

Justin Roberts

Yes, I did mean the standard Forms.RichTextBox
Apologies for not being clear on that one.

Also, you're right about the linefeed. I actually meant the Carriage Return
(vbCR) character. Sorry for the bad post.
InStr(rtb.Text, vbCR) always = 0

I need all the CR characters to be included as part of in my Text,
SelectionStart, SelectionLength, SelectedText, etc. properties.

Regards
Justin.
 
A

AMercer

I need all the CR characters to be included as part of in my Text,
SelectionStart, SelectionLength, SelectedText, etc. properties.

I think that rtb's always discard CR. Maybe someone else knows a way to
retain them - I don't know of a way. vbNewLine on my pc is CRLF, yet all CRs
go away in rtbs.

So... howcome do you need to retain all the CRs? It would seem to me that
you could do all your internal stuff with LF as the line separator, and in
those (presumably few) cases where CR is absolutely necessary, you could
replace LF with CR or CRLF. I use rtb's all the time, and that is the way I
do it. I need to put back CRs in a few situations, eg, notepad wants CRLF,
so to launch it with contents from an rtb, I replace LF with CRLF like:
Dim s as String = Replace(rtb.Text, vbLf, vbCrLf)
 
J

Justin Roberts

Thank you for your response.

Unfortunately I need to interface with another third party application that
specifies exactly what to do with what text. The interaction is dynamic and
frequent. The third party application bases all it's text calculations with
the standard Windows newline character (CRLF). (E.g. Characters at pos
x,length y = bold). I can't keep copying everything to a string because I
need the user input too, and have to send that back to the third party app.

Well, if CR's are not retained in RTB's, does anyone know how I can
calculate which logical 'line' a specified character position is in? (Not the
'physical' UI line, but the actual line in the string array)?

Regards
Justin
 
A

AMercer

Well, if CR's are not retained in RTB's, does anyone know how I can
calculate which logical 'line' a specified character position is in? (Not the
'physical' UI line, but the actual line in the string array)?

Have a look at rtb.GetLineFromCharIndex().
Line and character numbers are zero based, so if rtb is:

abc
def

with an LF after c (total length 7 chars), then char indices 0..3 are in
line 0 (a,b,c,LF), and char indices 4..6 are in line 1 (d,e,f).
 
J

Justin Roberts

I tried that, unfortunately GetLineFromCharIndex() returns the UI line, not
the string array line. So, as the text wraps down the page, the
GetLineFromCharIndex() increments even though there has been no CR.

I can calculate this using the Lines().Length property, but surely MS must
have provided a faster and better way to know which line a character is in!
 
A

AMercer

I tried that, unfortunately GetLineFromCharIndex() returns the UI line, not
the string array line. So, as the text wraps down the page, the
GetLineFromCharIndex() increments even though there has been no CR.

I can calculate this using the Lines().Length property, but surely MS must
have provided a faster and better way to know which line a character is in!

I assume you can't set WordWrap to false - if you can, problem solved.

This is getting ugly, but you could have two rtbs, one with word wrap and
one without. Show the one with wordwrap, calculate from the one without word
wrap, and keep them in sync.

Alternatively, there is the windows api. Sorry, no warranties for what
follows.

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
ByVal hWnd As IntPtr, _
ByVal wMsg As Integer, _
ByVal wParam As Integer, _
ByVal lParam As Integer) _
As Integer

Const WM_USER As Integer = &H400
Const EM_EXLINEFROMCHAR As Integer = WM_USER + 54
dim l,c as integer ' zero based line number and character number
c = some zero based character index
l = SendMessage(RichTextBox1.Handle, EM_EXLINEFROMCHAR, 0, c)
 
J

Justin Roberts

Thanks for your input on this one. It's a bit of a frustrating issue, so I
appreciate the answers and suggestions.

Regards
Justin
 
A

Alan Gillott

You scare me!
I have a complex VB6 Script editing application that started life as a text
box before I reimplemented it with a rich text box: the thoery was, and
according to my .net text books (sorry about the pun) that a RTB control
should substitute for a text control WITHOUT ANY CHANGES: what you state
suggests that this isn't true. it also means that I need to review the
effort of migrating this app to VB200x as it has HUNDREDS of references to
CR throughout the application: I was anticipating hours of pain and
suffering as it is (hence it is still VB6) fixing the hundreds of dim as
objects that are going to spring up when I convert without actually having
to review functionality too. Lots of Mid statements with explicit lengths
looking for keywords and scans of partial lines attempting to parse vbscript
for syntax errors. The problem is, bits of VB6 keep breaking on Vista (for
example Help is broken now!) AAARRRGGGGHHHH! My head hurts!
A
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top