Delete Blank Lines

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

Thanks in advance.
TAF2000Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
mark2150Connect With a Mentor Commented:
Ok, so now click on "Accept Comment as Answer"... :-)

M
0
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
mark2150Commented:
Lemme see your code.

M
0
 
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
 
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
All Courses

From novice to tech pro — start learning today.