?
Solved

Detect NewLine/vbCr/vbLf at end of RichTextBox text

Posted on 2005-02-28
27
Medium Priority
?
2,041 Views
Last Modified: 2008-01-09
Winforms app in VB.Net...

I have a richtextbox, RTB1.  I need to check to see if the user has pressed Enter at the very end of the RTB1 contents, and if not, I need to add a new line at the end, using...

RTB1.AppendText(vbCr) or RTB1.AppendText(environment.NewLine)

I have tried the following:

 >> Attempt1
If Not RTB1.Rtf.EndsWith(ControlChars.Cr) Then
txtFilenotes.AppendText(ControlChars.Cr)
End If

 >>Attempt2
If Not RTB1.Text.EndsWith(ControlChars.Cr) Then
txtFilenotes.AppendText(ControlChars.Cr)
End If

 >>Attempt3
If Not RTB1.Text.EndsWith(Environment.NewLine) Then
txtFilenotes.AppendText(Environment.NewLine)
End If

.....without success.

Please help

TIA
0
Comment
Question by:Sigh_Man
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 14
  • 11
  • 2
27 Comments
 
LVL 1

Author Comment

by:Sigh_Man
ID: 13426654
NB - 'txtFilenotes' and RTB1 should be the same.  For example, the code in Attempt 3 should read...

If Not RTB1.Text.EndsWith(Environment.NewLine) Then
RTB1.AppendText(Environment.NewLine)
End If

0
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 13426860
       If RTB1.Text.EndsWith(vbLf) Then
            MsgBox("Yes")
        Else
            RTB1.Text = RTB1.Text & vbCrLf
        End If
0
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 13426899
Hi Sigh_Man
It might appear to be a bit strange but if you search the Text for VBCrLf or even VBCr you'd see that it won't find any

RTB1.Text.Indexof(vbcr) will always be -1

Actually its like, the combination of VBCr and VBLf which signifies a proper New Line (equivalent to VBCrLf) is translated implicitly and the VBCr is lost only VBLf is preserved so you can only look for it. However for Appending you must use both i.e.
RTB1.AppendText(VBLf) will not work you need to use RTB1.AppendText(VBCrLf)

Look what the documentation has to say... (remember \r=VBCr and \nVBLf)

In Windows you have to pass multi-line strings with \r\n, otherwise the system functions don't recognize them correctly as multi-line. This is true e.g. for setting the text of Edit controls, Labels, Windows etc. Also, when you read multi-line text from a file that initially contains "\r\n" in translated mode, the string in memory will contain only a single "\n".

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Author Comment

by:Sigh_Man
ID: 13426927
Arif

If RTB1.Text.EndsWith(vbLf) Then....... does not work.  I made sure that I hit enter on saving my RTB, but when I ran this code it did not say Yes, despite the content of the RTB ending with 'Enter'.

Also, the second part of the code simply removes all text and shows only the vbCrLf.  I can sort something out with this second part but please re-think the first bit.

Thanks   :D
0
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 13426953
strange
I have a richTextBox I enter som etext then hit ENTER key

Then I click on a button which runs this code
       If RTB1.Text.EndsWith(vbLf) Then
            MsgBox("Yes")
        Else
            RTB1.Text = RTB1.Text & vbCrLf
        End If

And it certainly shows the messagebox
Also in case I do not hit Enter first time it will not show the msgbox but a click again will show "YES"

Anyway I'd just try some other way of doing it....
0
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 13426969
Your ultimate aim is that if there is no newLine at the End we should programatically Add one isn't it ??

Even this will do it

RTB1.Text = RTB1.Text.TrimEnd(vbCr, vbLf)
RTB1.AppendText(vbCrLf)

But I still wonder why the earlier thing did not work try checking the Indexof VBLF and see if you get it...
I can not do it as I am not having this problem here
0
 
LVL 1

Author Comment

by:Sigh_Man
ID: 13426991
Thanks - I'll try that right now...
0
 
LVL 1

Author Comment

by:Sigh_Man
ID: 13427032
Just to let you know, my RTB contained the following:

Line 1 - testing
Line 2 - testing
Line 3 - further testing

Even when I delete everything after "further testing", the answer ...IndexOf(vbLf) says "Yes".  Presumably this is due to each line being seen as a vbLf.  Actually, even if I have one line of text, ...IndexOf(vbLf) says "Yes"

Any ideas?

0
 
LVL 1

Author Comment

by:Sigh_Man
ID: 13427044
...Actually, even if the RTB is empty, the ...IndexOf(vbLf) says "Yes"     :-/
0
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 13427063
Well Indexof returns an Integer value
it should return -1 if not found
so check your code

if RTB1.Text>IndexOf(vbLf) > -1 Then
   Msgbox "Found"
Else
   Msgbox "Not Found"
End If

try this...
0
 
LVL 1

Author Comment

by:Sigh_Man
ID: 13427119
It is not finding vbLf even though the RTB contains several instances of them.
Nor does it find vbCrLf or vbCr.

(it says "Not found")
0
 
LVL 1

Author Comment

by:Sigh_Man
ID: 13427126
(There was a mistake in my earlier code, which I have now corrected)
0
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 13427342
So is it working now...
0
 
LVL 1

Author Comment

by:Sigh_Man
ID: 13427406
No, I am looking to see if I can make this work by reference to the \par in the RTB's .rtf property.

I had a similar problem to this with RTBs -- I could easily do AppendText(vbLf) but AppendText(vbBack) would not work (instead of deleting it would add a small black line to the RTB.  I don't know if these issues are connected, but suffice it to say that I don't like RTB's behaviour!

I'll post something as soon as I've worked out some code.
0
 
LVL 1

Author Comment

by:Sigh_Man
ID: 13427798
No, this is horrible -- I cannot get this to work.

Please help!!    :D
0
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 13427820
Could you post some of your code
0
 
LVL 1

Author Comment

by:Sigh_Man
ID: 13427898
OK, you I have posted a stack of code below.  You will note that it is all commented out.  This is because I have been trying all afternoon to get this working, and rather than deleting it, I can see what my previous attempts were.

I thought some of my efforts appeared perfectly logical, but ..... evidently not!!

I hope you can fix this.

Thanks again.

PS - I'm logging off shortly - but will respond to any comments asap!


Here's the code...

'If RTB1.Text.EndsWith(Environment.NewLine) And CType(RTB1, RichTextBox).Lines.GetUpperBound(0) > 1 Then
        'MsgBox("Environment.NewLine detected successfully")
        'RTB1.AppendText(Environment.NewLine)
        'End If


        'RTB1.AppendText(Environment.NewLine)

        'Dim FNLinesArray As ArrayList
        'FNLinesArray.Add()

        'Dim originalText As String
        'originalText = RTB1.Text
        '

        'If RTB1.Text.IndexOf(vbCrLf) Then MsgBox("Yes")
        'Else
        'RTB1.Text = originalText & vbCrLf
        'End If

        'RTB1.Text = RTB1.Text.TrimEnd(vbCr, vbLf)

        'If RTB1.Text.IndexOf(vbLf) > -1 Then
        'If RTB1.Text.IndexOf(vbCrLf) > -1 Then
        'If RTB1.Text.IndexOf(vbCr) > -1 Then
        'If RTB1.Text.IndexOf(v) > -1 Then

        'If RTB1.Rtf.EndsWith("\par") Then MsgBox("Found \par")
        'If RTB1.Rtf.EndsWith("\par\par") Then MsgBox("Found \par\par")
        'If RTB1.Rtf.EndsWith("\par \par") Then MsgBox("Found \par \par")

        'Dim IndexOfLastPar As Integer = RTB1.Rtf.LastIndexOf("\par")
        'MsgBox(IndexOfLastPar)

        'Dim lengthOfFilenote As Integer = RTB1.Rtf.Length
        'MsgBox(lengthOfFilenote)

        'Dim paraPenultimateSubstringStart As Integer = lengthOfFilenote - (lengthOfFilenote - indexoflastpara)
        'MsgBox(RTB1.Rtf.Substring(IndexOfLastPar - 6, 4))

        'If indexoflastpara - paraPenultimateSubstringStart < 5 Then
        'MsgBox("Yes, there is a blank line at the end")
        'Else
        'MsgBox("No, there is no blank line at the end")
        'End If


       
        'Dim lengthOfFilenote As Integer = RTB1.Rtf.Length
        'MsgBox(lengthOfFilenote)
        'MsgBox(RTB1.Rtf.Substring(lengthOfFilenote - 6, 4))
        'MsgBox(RTB1.Rtf.Substring(lengthOfFilenote - 10, 4))


        'if RTB1.Rtf.Substring(IndexOfLastPar - 6, 4) = "\par" and

        'MsgBox(RTB1.Rtf.LastIndexOf("\par"))
        'MsgBox(RTB1.Rtf.Length)

        'MsgBox(RTB1.Rtf.ToString)



        ' NEXT LINE IS OK
        'RTB1.AppendText(vbCrLf)




        'MsgBox(RTB1.Lines.GetUpperBound(0) & " lines")


        'If Not RTB1.Rtf.EndsWith("\par") Then
        'RTB1.AppendText(Environment.NewLine)
        'End If

        'MsgBox(RTB1.Rtf.ToString.LastIndexOf("\par"))
        'MsgBox(RTB1.Rtf.LastIndexOf(vbCr)) ' 230
        'MsgBox(RTB1.Rtf.LastIndexOf(Environment.NewLine)) ' 230
        'MsgBox(RTB1.Text.LastIndexOf(vbCr)) ' -1
        'MsgBox(RTB1.Text.LastIndexOf(Environment.NewLine)) ' -1
        'MsgBox(RTB1.Rtf.Length()) ' 233

        'If Not RTB1.Rtf.ToString.LastIndexOf("\par") = (RTB1.Rtf.Length() - 10) Then
        'RTB1.AppendText(ControlChars.NewLine)
        'RTB1.Text += vbCr
        'End If



        'LastLine = RTB1.Text.LastIndexOf(

        'If RTB1.Text Then

        'End If


        'LastLineNumber = RTB1.Lines. 'GetLength(((1) '.GetUpperBound(0)
        'MsgBox(LastLineNumber)

        'LastLine = CType(RTB1, RichTextBox).Lines(LastLineNumber)
        'MsgBox("!" & LastLine & "!")

        'If LastLine.Length > 0 Then ' Is Nothing Then '= "" Then 'environment.NewLine Then
        'RTB1.AppendText(Environment.NewLine)
        'End If

        '    If Not RTB1.Rtf.EndsWith(ControlChars.Cr) Then
        ' if rtbcontent ends with blank line then do nothing
        ' else put a blank line at the end...
        'RTB1.AppendText(ControlChars.Cr)
        'End If
0
 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 800 total points
ID: 13427907
Hi sigh_man,

try this

        If not RichTextBox1.Lines((RichTextBox1.Lines.Length - 1)) = "" Then
            RichTextBox1.AppendText(Environment.NewLine)
        End If
0
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 13427994
Hi Sigh_Man
Is your RTB empty in the beginning, did you test either of the code RonaldBiemans or mine on a fresh form.
Because both of them work fine for me, there must be something there...
0
 
LVL 1

Author Comment

by:Sigh_Man
ID: 13433340
Thanks guys....

OK, I'm sorry to give the experts the runaround on this, but I think I have made a stupid mistake.  Very embarrassing.  I was trying the execute this code on the Form's Deactivate event, which (I assume) is messing up the index values etc.  How can I catch the user's clicking on the form_close 'x' button and execute this code prior to closing the form?
0
 
LVL 19

Assisted Solution

by:arif_eqbal
arif_eqbal earned 1200 total points
ID: 13436125
Use Form_Closing event

    Private Sub Frm_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        If RTB1.Text.EndsWith(vbLf) Then
            MsgBox("Exists")
        Else
            RTB1.AppendText(vbCrLf)
            MsgBox("Added")
        End If
    End Sub
0
 
LVL 1

Author Comment

by:Sigh_Man
ID: 13436466
Excellent.  Thanks both!    :D
0
 
LVL 1

Author Comment

by:Sigh_Man
ID: 13436512
I hope you're both OK with the points split.  The accepted answer above is the only one that stood up to loads of testing.

Arif, thanks for your continued help - perhaps in a different scenario (ie if my program wasn't so crazy in its design!) I'm sure your solution(s) would have been best.  I have given you most points (in fact, the same amount as was first allocated to this question) despite RB's answer being the accepted one.  (I think the idea of EE is to accept the code that solves my problem - for the benefit of other EE members - I hope you're OK with this - if not, I'll have to remember you for some extra points next time!!).  The points is what matters most.

Phew!!

Thanks again   :D
0
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 13436514
Hi sigh Man
In case you are using RonaldBiemans solution
just add one line before it
If RTB1.Lines.Length > 0 Then
    .....

Or make it like this

      If RTB1.Lines.Length > 0 AndAlso Not RTB1.Lines((RTB1.Lines.Length - 1)) = "" Then
            RTB1.AppendText(Environment.NewLine)
      End If

This is in case your RTB is empty the compiler should not give IndexOutofRange Error as we are using "Length-1" here

Thanks
0
 
LVL 1

Author Comment

by:Sigh_Man
ID: 13436521
RB - I hope you're OK with this - since you've helped me out in about two thirds of my postings!!   :D
0
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 13436523
hi Sigh_Man
Its perfectly fine, RonaldBiemans of course deserved it
we were only looking at the problem from one angle (VbCr, VbCrLf ....)
He gave a better alternative....
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 13436891
Ok by me, as long as your problem was solved :-)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses
Course of the Month8 days, 12 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question