Codingo Codingo - 12 days ago 6
C# Question

Stripping lines from multiline textbox

I have a multiline textbox with text similar to:

Tester Testman
None
None
18 Test Court
Upper Test
QLD 4000


I'm trying to remove all lines that the word
None
, I've tried the following:

private void TextBoxShippingLabel_TextChanged(object sender, TextChangedEventArgs e)
{
TextBoxShippingLabel.Text = TextBoxShippingLabel.Text.Replace("None", null);
}


However instead of removing the lines this just leaves them empty, so it then looks like the following:

Tester Testman


18 Test Court
Upper Test
QLD 4000


How can I alter my code to remove the lines with that just contain the word "None" entirely? My desired output would be:

Tester Testman
18 Test Court
Upper Test
QLD 4000

Answer

Here is a simple snippet to do the job:

private void UnwantedNoneRemover(TextBox textBox) {
   var lines = textBox.Lines;
   var updatedLines = new List<string>();
   foreach (var line in lines) {
       if (line != "None") { updatedLines.Add(line); }
   }
   textBox.Lines = updatedLines.ToArray();
}
Comments