2

I'm encountering a strange ListView problem and will do my best to explain it. I have a ListView with 4 columns, the last one being a message string of varying length. I have some functionality that will change the ListView item red if it contains certain keywords (fail, exception, ect).

I first noticed this issue when one item was red and I didn’t see any word in the column that would trigger the red coloring code. So, I had the Length of the incoming string prepending to the item and added a textbox that would display that column's text when selected. What I found was that the prepending length (actual length of incoming string) would be like 953, the extracted ListViewItem's Text length would be 960 (str length + prepended length info), but the text that would be in the text box's length was 253...

What's going on here? Its like all the text made it into the ListViewItem but it can't/won't show it all (and no, its not column width, I had it set to over 1000 in the above case).

Adding the ListViewItem and checking for error strings:

ListViewItem listItem = new ListViewItem(msg.Date);

// Add sub-items for Details view.
listItem.SubItems.Add(msg.Time);
listItem.SubItems.Add(msg.Thread);
listItem.SubItems.Add("L: " + msg.Message.Length + " " + msg.Message);                        

if (!msg.Message.Contains("FA_FAILCNT"))
{
    if (msg.Message.Contains("fail", StringComparison.OrdinalIgnoreCase) ||
        msg.Message.Contains("exception", StringComparison.OrdinalIgnoreCase) ||
        msg.Message.Contains("db q", StringComparison.OrdinalIgnoreCase))
    {
        listItem.Font = new Font(listItem.Font, FontStyle.Bold);
        listItem.ForeColor = Color.Red;
    }
    else
        listItem.ForeColor = Color.Black;
}

Obviously its the last subitem thats giving me the issues (the one that gets msg.Message)

EDIT: Well crap, this explains it.... any ways around this?

Hershizer33
  • 1,206
  • 2
  • 23
  • 46
  • You will need to post some code and the formatted HTML – Kane Jul 14 '11 at 15:27
  • 1
    From the linked article: `The ListBox control in" _ & " Visual Basic .NET offers an alternative in that" _ & " it now supports a horizontal scroll bar so that" _ & " the user can view all of the string when necessary."` Does that not work for you? – Cody Gray - on strike Jul 14 '11 at 15:49
  • To be completely honest, I just read the headline and got disapointed haha, good catch – Hershizer33 Jul 14 '11 at 15:57
  • possible duplicate of [.NET ListView, max number of characters, or maximum column width? Possible to override/expand?](http://stackoverflow.com/questions/5559704/net-listview-max-number-of-characters-or-maximum-column-width-possible-to-ove) – Cody Gray - on strike Jul 14 '11 at 16:19

1 Answers1

3

You have already found the reason why not all of the text is displayed.

The best solution that I've found so far is to put the information in the tooltip so that the entire string is visible when the user hovers their mouse over the column - see Listview subitem text is not shown fully in the UI eventhough the length of the text is correct.

Another way I've seen this work is allowing users to copy the value of a cell. Although the displayed text is truncated, copying and pasting the cell value into another application allows you to view the full text.

I imagine that the only other "workaround" would involve writing your own control - alternatively I don't think the ListView control in WPF has this same limitation.

Justin
  • 84,773
  • 49
  • 224
  • 367
  • Thanks! Any pointers on setting up the tooltip? Never played with one before – Hershizer33 Jul 14 '11 at 16:00
  • @Hershizer33 Sorry, I've not used them extensively either but I expect that you should be able to find a fair amount of information on Google. If you are struggling then just ask another question - there will be plenty of people on StackOverflow who will be able to help with this. – Justin Jul 14 '11 at 16:02
  • Are you sure that the native Win32 ListView control has this limitation? I've never run into it before, so it seems unlikely. Then again, it's probably also unlikely that I've tried to show over ~270 characters in a single subitem. That's the opposite of user-friendly. – Cody Gray - on strike Jul 14 '11 at 16:08
  • @Cody [ListView control can only display 259 characters per column](http://support.microsoft.com/kb/321104) says that it is, also I've seen this before in one of our internal tools. – Justin Jul 14 '11 at 16:09
  • Yeah, but it also says that there's a horizontal scroll bar so that the user can view all of the string. What do they mean by that? Not to mention that 259 just seems like a weird number – Cody Gray - on strike Jul 14 '11 at 16:11
  • Ah, turns out you're right. See [here](http://stackoverflow.com/questions/5559704/net-listview-max-number-of-characters-or-maximum-column-width-possible-to-ove). It's the first 260 (actually 259) characters that are displayed, documented [here](http://msdn.microsoft.com/en-us/library/bb774760.aspx) in the SDK docs. The number more than likely comes from `MAX_PATH`. A possible solution is owner drawing. This looks like a duplicate. – Cody Gray - on strike Jul 14 '11 at 16:19