Search code examples
c#stringlisttooltip

C# Multiple Lines in Tool Tip


I am doing this (value is a list of strings):

protected override bool _setValue(object value)
{
    ToolTip toolTip = new ToolTip();
    toolTip.Content = string.Join("\r\n", value);
    return true;
}

When I hover over the item that has the tool tip, it displays "System.Generic.List'1[System.String]"

So apparently the string.Join() is returning a list object, not a string.

How do I make the tool tip display multiple lines of text?


Solution

  • Return type of String.Join method is string not List. You need to call your object type to List to get the right answer. Other wise its compiler is just using value.ToString() and value is an object not List. Just tried it

    public static void Main(string[] args)
            {
                var items = new List<string>
                {
                    "Test 1",
                    "test 2"
                };
                WillPrintCorrect(items);
                WillPrintWrong(items);
                BestWay(items);
                Console.ReadLine();
    
            }
    
            public static void WillPrintCorrect(object value)
            {
                 Console.WriteLine(string.Join(Environment.NewLine,(List<string>)value));
            }
    
            public static void WillPrintWrong(object value)
            {
                Console.WriteLine(string.Join(Environment.NewLine, value));
            }
    
            public static void BestWay(List<string> value)
            {
                Console.WriteLine(string.Join(Environment.NewLine, value));
        }