Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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...???

like image 335
B. Clay Shannon-B. Crow Raven Avatar asked Dec 09 '22 23:12

B. Clay Shannon-B. Crow Raven


2 Answers

This works for me:

myRichTextBox.Lines = myList.ToArray();
like image 63
MAV Avatar answered Dec 11 '22 12:12

MAV


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 - expectsbeing 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.

like image 44
Jeremy Thompson Avatar answered Dec 11 '22 12:12

Jeremy Thompson