0

I have the two following code pieces:

    private void AddTextToLog(string textToAdd)
    {
        //Console.WriteLine(@"Raw Text: " + textToAdd);
        textToAdd = RemoveInvalidCharacters(textToAdd);
        var array = ParseLogText(textToAdd);
        foreach (var entry in array)
        {
            _log.Add(entry);
            MainForm.Get.UISynchContext.Send(o => MainForm.Get.AddToChatLog(entry), null);
        }
    }

And:

    public void AddToChatLog(string text)
    {
        tbChatLog.AppendText("\r\n" + text);
        //tbChatLog.AppendText(Environment.NewLine);

    }

What happens here is that when AddTextLog(text) is called, some new text is appended to tbChatLog, which is a textBox.

The weird thing is that, if in do not include \r\n or Environment.NewLine in AddToChatLog method, then all gets added directly after each other, without any line breaks or whitespace. If I add one of those two line break codes, then not only is it dropped to the next line of the textbox, but a whole empty line is added too. So it seems it does two line breaks.

Any idea?

Thanks!


UPDATE:

This is the text without any line-break added:

A710E000A:Simon:1A7110000A:Simon:2A7118000A:Simon:3

If I add a line break of some sort to AddToChatLog, I get this:

A721F000A:Simon:1

A7222000A:Simon:2

A7223000A:Simon:3

UPDATE 2:

Hmmmm, I added a Console.WriteLine("ENTRY: " + entry) to the AddTextToLog method, and turns out every time it adds something in the for loop, there is also an empty entry. That is really bizarre. I got the array from a string.Split method, there should be no empty entries in the array...


SOLUTION:

I was Split'ing on a certain set of characters, which as it turns out the master-string could start with. If you split at the start of a string you actually get a string array with one empty string at top. Two fixes that both work: One was stripping empty array entries with Linq. The other, which is easier, is just to create a Substring from the master string, that starts a few characters in, so split does not happen at the start of the master-string.

Thanks for all the help!

Anders
  • 580
  • 8
  • 17
  • 2
    You're telling it to add a carriage return and a newline `\r\n`, which is why you get a 'double line break'. – DGibbs Jan 29 '15 at 17:30
  • 1
    `Environment.NewLine` does the same thing though – Anders Jan 29 '15 at 17:31
  • `Environment.NewLine` returns `\r\n` on windows... – DGibbs Jan 29 '15 at 17:32
  • What should I use instead? I tried just `\n` and just `\r`, but that did not work. It actually made the program crash, for some reason... – Anders Jan 29 '15 at 17:34
  • Have you tried to check the contents of the TextBox after the `AppendText` call? Is it `previous \r\n current` or is it something else? What are the contents if you manually make a line break from the keyboard? Have you tried to append only the linebreak? – Eugene Podskal Jan 29 '15 at 17:36
  • It may be the case of adding breaks when not having any text yet. Have a look at the extension method in the accepted answer of a similar question here: http://stackoverflow.com/questions/8536958/how-to-add-a-line-to-a-multiline-textbox – musicfuel Jan 29 '15 at 17:37
  • @musicfuel Just tried the extension method, it did not work for me unfortunately. – Anders Jan 29 '15 at 17:41
  • @Anders Can you try to create a [MCVE](http://stackoverflow.com/help/mcve)? Because `NewLine ` works perfectly on a clean project. Maybe the issue is caused by `UISynchContext` use or by something else. – Eugene Podskal Jan 29 '15 at 17:48
  • What does a typical `textToAdd` string look like and what does the body of `ParseLogText` do? – musicfuel Jan 29 '15 at 18:04
  • 1
    The comments helped me think outside of the box, resulting in me figuring out what was wrong. I was Split'ing on a certain set of characters that the master-string could start with. If you split at the first character you actually get a string array with one empty string at the beginning. So I made two fixes, one was stripping empty array entries with Linq. The other which is easier is just to create a substring from the master string, that starts a few characters in, so split does not happen at the start of the master-string. – Anders Jan 29 '15 at 18:11

0 Answers0