How to assign contents of List<String> to a RichTextBox?

arraylist, c#, generics, richtextbox, winforms

Solution

`Most classes in the BCL have a ToString() method.`

When it comes to a `List Of strSessionIdLines` the ToString() tells you what type of object it is.

If you are casting for example an `int` to a `string` the `int.ToString()` will return its value, but if you do it on a array of integers `int[].ToString` it's `ToString()` method wont return eg a comma/linefeed separated string of values. As it appeared you expected.

This is why assigning the .ToArray to the .Lines property of the RichTextBox or a loop (or aggregate) to concatenate the List Of String into one string to suit the .Text property of the RichTextBox works.

One more tip I live by is when I need to call a method, eg `String.Format` I hover my mouse so that I can see what the method expects - expects`being the keyword`. Then say the method wants a argument in the parameter thats of Type X, I declare type X and pass it in. Methods often have overloads, meaning that they can work with different parameters, so pressing up/down to scroll through them is also helpful in working out what is the most convenient in your situation. When you are passing in arguments to a method (in its parameter) type comma to refresh the tooltip indicating each arguments datatype.

Problem

I've tried this: ``` richTextBoxResults.Text = listStrSessionIdLines.ToString(); ``` ...but get the List's ToString() representation (I guess that's what it is: "System.Collections.Generic.List`1[System.String]"). ...and I've tried to try this: ``` listStrSessionIdLines.CopyTo(richTextBoxResults.Lines); ``` ...but I get, "Argument Exception was unhandled. Destination array was not long enough. Check destIndex and length, and the array's lower bounds." Does this mean I have to assign the RichTextBox a number of lines first, or...???

Original source