I need to display the entire Text of my ListViewItems (not truncated)

Go To StackoverFlow.com

1

The ListViewItems I am displaying, categorized within ListView Groups in a ListView, are showing just their Text property, and truncated at that (even if they are the only item in a particular Group, and there are acres and hectares of screen real estate available).

I would like them to indeed display their Text property, but in its entirety, not truncated.

I am assigning the ListViewItem text property this way:

public LegacyApplication(String AAppName, String ATitle, String ADesc, String AIconFileName, String APathOfExe) {
  . . .
  base.Text = ATitle;
  . . .
}

What am I doing wrong or failing to do right? Is there some property I'm unaware of that will control the appearance of the Text on the ListViewItem?

2012-04-03 23:10
by B. Clay Shannon


2

Windows Explorer also sets a Tooltip for trimmed text and it also uses a ListView control. You can imitate its behavior by handling the MouseMove event and using the ListView.GetItemAt(...) to find out which item the mouse is moving on. Alternatively you can use the ItemMouseHover event , however I found MouseMove to be more user friendly as the user does not have to actually hover and wait for the tooltip to popup ....

I used the following logic to imitate Windows explorer behaviour (this sample is only a draft and might contain errors):

     //get the item at the current mouse location.
     ListViewItem item = myListView.GetItemAt(e.X, e.Y);
     if (item != null)
     {
        using (Graphics graphics = this.myListView.CreateGraphics())
        {
           var itemTextWidth = graphics.MeasureString(item.Text, item.Font).Width;
           if (itemTextWidth > myListView.Width)
           {
              if (!item.Equals(itemsToolTip.Tag) && !itemsToolTip.Active)
              {
                 //Set the tooltip , tag it with the item and set it as active ... 
              }
              // Adjacent ' text trimmed' list view items.  
              else if (!item.Equals(itemsToolTip.Tag))
              {
                 //set the tooltip as none active.
              }
           }
           else//None trimmed list view items.
           {
              //clear the tooltip and set it as none active
           }
        }
     }
     else
     {
        //clear the tooltip and set it as none active
     }
  }
2015-03-05 09:01
by Ahmad


2

Not sure if this will work for your situation, but perhaps you could try using the AutoResizeColumn method; set to ColumnContent.

2012-04-04 02:17
by Dr. Wily's Apprentice
I've tried that, but can't get the syntax right, I guess. I tried: if (listApplications.Groups.Count > 0) { listApplications.Groups[0].Items[0].AutoResizeColumn(0,
ColumnHeaderAutoResizeStyle.HeaderSize); } - but that won't compile.. - B. Clay Shannon 2012-04-05 16:26


2

I'm aware this is an old question, but I was convinced Dr.Wilys answer to do with AutoResizeColumn should be on the the right path, so explored it a little...

After writing output to the listview (mine was the output of a bulk sql server operation) I updated the listview (here, lstResults) thus:

lstResults.AutoResizeColumn(0, ColumnHeaderAutoResizeStyle.HeaderSize);
lstResults.AutoResizeColumn(1, ColumnHeaderAutoResizeStyle.ColumnContent);

This worked for me... NB My View type was Details, I haven't tried the others.

2016-02-04 10:04
by Fetchez la vache


1

If you want multi-line text in a ListView, have a look at ObjectListView (an open source wrapper around .NET WinForms ListView). This takes care of many of the problems involved with owner drawing (and other ListView annoyances).

I'd put this as a comment but it is the answer and has a lovely screenshot: Multi-line list items on WinForms ListView control?

Then if you have any concerns please see the follow up: How to wordWrap the text in a column using ObjectListView

2012-04-04 02:04
by Jeremy Thompson
Thanks, but we are trying to avoid 3rd-party control - B. Clay Shannon 2012-04-04 18:32
@ClayShannon I would normally agree 100%, since your up the creek if you get a bug with a 3rd party component and you dont have the source or they go out of business. But this ObjectListView and associated source code is licensed under The GNU General Public License, so its more open source - Jeremy Thompson 2012-04-05 00:14
OK, I will czech it out and see if "the powers that be" might go for it - B. Clay Shannon 2012-04-05 04:38
Actually, my ListViewItems hosted on a standard ListView control are showing multi-line text, such as:

Customer Contact List

...the problem is where the text does not fit into the area allocated the ListViewItem, such as:

Data Collecti...

There must be some way to add padding around the items to give them some breathing/elbow room so that their entire text can display.

The "PadLeft" and "PadRight" and "IndentCount" properties for those ListViewItems don't seem to do anything, though.. - B. Clay Shannon 2012-04-05 16:37

What I've ended up doing, at least for now (until I find a good way, if one exists, of showing all the text of the ListViewItems), I'm concatenating the full text value and assigning it to the Tooltip property, so that users can, if the Text is truncated, hover and find that the full text has hove into sight - B. Clay Shannon 2012-04-05 17:24


1

I found that the best solution to this was to use View = View.Tile.

This displays MOST of the text; my workaround is to assign the text to the tooltip for each item so that in those cases where the full text is still not being displayed, the user can hover and it hoves into sight.

2012-04-07 15:58
by B. Clay Shannon


1

A fleshed-out version of @Ahmad's answer (using a tooltip for items that don't fit):

    void SetTooltipForNonFittingItems(ListView listView)
    {
        var tooltip = new ToolTip();
        listView.MouseMove += (_1, e) =>
        {
            ListViewItem item = listView.GetItemAt(e.X, e.Y);
            if (item != null)
            {
                using (Graphics graphics = listView.CreateGraphics())
                {
                    var itemTextWidth = graphics.MeasureString(item.Text, item.Font).Width;
                    if (itemTextWidth > listView.Width)
                    {
                        if (!item.Equals(tooltip.Tag))
                        {
                            tooltip.Active = true;
                            tooltip.Tag = item;
                            // as written, the tooltip covers the item
                            // if you prefer to show it at the location of the cursor,
                            // use listView.PointToClient(Cursor.Position)
                            tooltip.Show(item.Text, listView, item.Position);
                        }
                    }
                    else
                    {
                        tooltip.Tag = null;
                        tooltip.Active = false;
                    }
                }
            }
            else
            {
                tooltip.Tag = null;
                tooltip.Active = false;
            }
        };
    }
2018-06-25 10:26
by staafl
Ads