How To Remove Blank Line From TextBox Using C# And VB.NET

Sometimes Blank Line Create Issue While Developing Programs In C# And VB.NET. To Remove Blank Lines (Including Spaces Also) , We Can Achieve It By Using TextBox's Line Property


Remove Blank Lines From TextBox Using C#

Create A Form Which Contain A TextBox (text1) & A Button (button1).

C# Form


Code For button1 On Which Upon Clicking Blank Lines Should Be Removed

C# Code To Remove Blank Lines


private void Button1_Click(object sender, EventArgs e)
        {
            txt1.Lines = txt1.Lines.Where(line => line.Trim() != string.Empty).ToArray();
        }
In This Code , txt1 Is The Name Of TextBox

Output

Remove Blank Lines From TextBox Using VB.NET

Create A Form Which Contain A TextBox (text1) & A Button (button1).

VB.NET Form

Code For button1 On Which Upon Clicking Blank Lines Should Be Removed

VB.NET Code To Remove Blank Lines


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles button1.Click
        txt1.Lines = txt1.Lines.Where(Function(line) line.Trim() <> String.Empty).ToArray()
    End Sub
In This Code , txt1 Is The Name Of TextBox


Frequently Asked Questions

How To Remove Blank Lines From TextBox ?

Using The Code Given Above , You Can Remove Blank Lines From TextBox / RichTextBox

How To Remove Blank Line With Spaces From TextBox ?

The Code Given Above Can Remove Blank Lines Which Contain Spaces