Delete Blank Lines

What is an efficient way to delete all blank lines in a RichTextBox?

Thanks in advance.
TAF2000Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mark2150Commented:
You'll have to iterate thru the .Text looking for vbCrLf's. Ever time you find one copy that portion to a temp string and TRIM it. If the length > 0 then append it to another string. When all done, take the appended string and replace the text box contents with it.

Raw = RTB.Text
Clean = ""
Temp = ""

do while instr(raw, vbCrLf) > 0
  marker = instr(raw, vbCrLf)
  if marker > 0 then
    temp = trim(left(raw, marker))
    if len(temp) > 0 then clean = clean & temp & vbcrlf
    raw = right( raw, len(raw) - (marker + 1)
  end if
loop
'
RTB.Text = Clean

Anyway this should be close for off the top of my head.

M

0
TAF2000Author Commented:
Thanks,
Hmm, I tried that and it didn't work..  I am not real good with VB yet, but I can understand what most of that is doing.  I made a command button so that when you click on it it does the code above.  The first time I click it, it deletes the bottom line, if I hit it again, it adds a vbCrLf symbol to every line.  I have messed with it and modified it, but I cant get it to work.  Thanks again, any additional help is much appreciated.
0
mark2150Commented:
Lemme see your code.

M
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

mark2150Commented:
Just tested it. This works:


raw = RTB.Text
Clean = ""
temp = ""

Do While InStr(raw, vbCrLf) > 0
  marker = InStr(raw, vbCrLf)
  If marker > 0 Then
    temp = Trim(Left(raw, marker - 1))
    If Len(temp) > 0 Then Clean = Clean & temp & vbCrLf
    raw = Right(raw, Len(raw) - (marker + 1))
  End If
Loop
'
RTB.Text = Clean


Sorry. Said it might just be close! :-)

M
0
TAF2000Author Commented:
Thanks, it works great!
0
mark2150Commented:
Ok, so now click on "Accept Comment as Answer"... :-)

M
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KDivadCommented:
Not trying to take mark's points, but I use a replace function to replace doubled vbCrLf's with a single one.

RTB.Text = Replace(RTB.Text, vbCrLf & vbCrLf, vbCrLf)

Or at least this is what I do with normal (not something.Text) strings.
0
KDivadCommented:
ok, I see, mark's code would also remove lines that only contain spaces, right? And mine wouldn't.
0
TAF2000Author Commented:
Ahh, didnt see the "Accept Comment as Answer"  I thought that you has to "Answer it" Thanks
0
mark2150Commented:
Yeah, when TAF2000 asked to kill "all blank lines" I assumed he meant those that were just spaces as well.

Although the code given works, it is not the most "efficient" implementation possible.

This version should be a little quicker as it only performs the INSTR once per loop instead of twice:

raw = RTB.Text
Clean = "" 
temp = "" 
marker = InStr(raw, vbCrLf)
'
Do While marker > 0
  temp = Trim(Left(raw, marker - 1))
  If Len(temp) > 0 Then Clean = Clean & temp & vbCrLf
  raw = Right(raw, Len(raw) - (marker + 1))
  marker = InStr(raw, vbCrLf)
Loop
'
RTB.Text = Clean

Additionally it eliminates a redundant conditional. It should work just the same, but be a tad faster and compile slightly smaller.

M

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.