what is the code for searching for and removing a blank line in a multi-line text box? im using VB Express '08 and can not figure it out.

my problem is im loading a sample.text file and using txtFile.AppendText(ControlChars.NewLine & activetext & ",") to add new text from user input and get a blank line that causes problems when converting the text into my array. it looks like this:
info,info
info,info
info,info
info,info
info,info
info,info

info,info
info,info
please HELP! i am out of ideas and everything else is working except this. all i need is that blank line to go away. lol

any ideas or suggestions would be most appreciated.
can post more of my code if needed.

Dani AI

Generated

A quick way to stop the extra blank line is to change how you append. You are prefixing each write with a newline, so if the textbox already ends with a newline you create an empty row. Append the newline after each entry, and only add a newline before appending if the last char is not already one. This keeps exactly one newline between rows.

' Append one line safely (no leading or blank lines)
If TextBox1.TextLength > 0 AndAlso Not TextBox1.Text.EndsWith(Environment.NewLine) Then
    TextBox1.AppendText(Environment.NewLine)
End If
TextBox1.AppendText(activetext & "," & Environment.NewLine)

To clean what is already there, avoid splitting on a single CR/LF char (as in ’s sample) and use the textbox’s Lines property. Filter out empty or whitespace-only lines, then assign back. This works for CRLF/LF differences and multiple consecutive blanks.

' Remove blank or whitespace-only lines already in the textbox
Dim lines() As String = TextBox1.Lines
Dim cleaned() As String = Array.FindAll(lines, Function(l) l.Trim().Length > 0)
TextBox1.Lines = cleaned

Notes:

  • Prefer Environment.NewLine over ControlChars.NewLine for clarity. Both map to CRLF on Windows, but using Environment.NewLine pairs well with .Lines.
  • ’s Replace(vbCrLf & vbCrLf, vbCrLf) will collapse double blanks, but it will not catch whitespace-only lines or mixed line endings; you would need to loop or use a regex. If you want a single pass that collapses any run of blank/whitespace-only lines to one newline: TextBox1.Text = System.Text.RegularExpressions.Regex.Replace(TextBox1.Text, "(\r?\n)\s*(\r?\n)+", "$1").

Recommended Answers

All 2 Replies

to remove the blank lines on reading it ou could do:

Dim tmp() As String = My.Computer.FileSystem.ReadAllText("C:\test.txt").Split(vbNewLine)
        For Each newStr As String In tmp
            If newStr.Length > 2 Then TextBox1.AppendText(newStr & vbNewLine)
        Next

if you want to update the textbox you could do:

Dim tmp() As String = TextBox1.Text.Split(CChar(vbNewLine))
        TextBox1.Clear()
        For Each line As String In tmp
            If line.Length > 1 Then
                TextBox1.AppendText(line & vbNewLine)
            End If
        Next

i've posted both possibility coz it wasnt really clear to me what exactlly u wanted (at what time) ... guess need more coffee

commented: N.A +13

You can try this (not tested deeply)

Dim SearchIn = Me.TextBox1.Text
Dim sb As StringBuilder = New StringBuilder(SearchIn)
Me.TextBox1.Text = sb.Replace(vbCrLf + vbCrLf, vbCrLf).ToString
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.