Results 1 to 10 of 10

Thread: VB 2008 - ListView tool tips

  1. #1

    Thread Starter
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    VB 2008 - ListView tool tips

    This code will display the text of whatever item or sub items the cursor hovers over a listview. The length of the tool tip can be set via the ToolTipLengthPerLine variable. It was pieced together from various postings on the web and cleaned up for various conditions discovered in other posts like just displaying the item (not sub items) and the position being lost when the list view scrolling is in effect and the grey bar is slid to the right.

    1. Start a new Windows application.
    2. Drop a tool tip on the form named ToolTip1.
    3. Paint a listview on the form called ListView1 ( I used about 4 x 4 inches).
    4. Open un the code portion of the form and delete everything.
    5. Paste in the code and run the solution.


    Code:
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            ListView1.Clear()
            ListView1.View = View.Details
            ListView1.GridLines = True
            ListView1.FullRowSelect = True
            ListView1.HideSelection = False
            ListView1.MultiSelect = False
    
            'Headings
            ListView1.Columns.Add("Primary Column")
            ListView1.Columns.Add("Sub One")
            ListView1.Columns.Add("Sub Two")
            ListView1.Columns.Add("Sub Three")
    
    
            Dim lvi As New ListViewItem
            'First row
            lvi.Text = "Primary Column"
            lvi.SubItems.Add("Sub Item One is a very long entry for demonstation")
            lvi.SubItems.Add("Sub Item Two")
            lvi.SubItems.Add("Sub Item three")
            ListView1.Items.Add(lvi)
    
            'Second row
            lvi = New ListViewItem
            lvi.Text = "Primary Column"
            lvi.SubItems.Add("Sub Item One")
            lvi.SubItems.Add("Sub Item Two is a very long entry for demonstation")
            lvi.SubItems.Add("Sub Item three")
            ListView1.Items.Add(lvi)
    
            For i As Integer = 0 To ListView1.Columns.Count - 1
                ListView1.Columns(i).Width = -2
            Next
    
        End Sub
        Private Sub ListView1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseMove
            Dim LVHit As ListViewHitTestInfo
            Dim ToolTipText As String = ""
            Dim CurrentPosition As Integer = 0
            Dim LastBreakpoint As Integer = 0
            'Set ToolTipLengthPerLine to the size the tool tip should display.
            'It will break the text at that length, at the next space, and add linefeeds
            Dim ToolTipLengthPerLine As Integer = 40
    
            Try
                LVHit = ListView1.HitTest(e.X, e.Y)
    
                If LVHit IsNot Nothing Then
                    If LVHit.SubItem IsNot Nothing Then
                        ToolTipText = LVHit.SubItem.Text
                        While CurrentPosition + ToolTipLengthPerLine < ToolTipText.Length
                            LastBreakpoint = ToolTipText.Substring(CurrentPosition, ToolTipLengthPerLine).LastIndexOf(" ")
                            ToolTipText = ToolTipText.Insert(CurrentPosition + LastBreakpoint + 1, vbCrLf)
                            CurrentPosition += LastBreakpoint + 3
                        End While
                    End If
                End If
                ToolTip1.SetToolTip(ListView1, ToolTipText)
            Catch
                ToolTip1.SetToolTip(ListView1, "")
            End Try
        End Sub
    
    End Class

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: VB 2008 - ListView tool tips

    Moved To The Codebank

  3. #3
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: VB 2008 - ListView tool tips

    Unless the text for the ListViewItem is cutoff because the text is longer than the column, then this really isn't useful.

    And if it is cutoff, then just make the ShowItemToolTips True.

    ie:



    Thanks for the contribution though.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  4. #4

    Thread Starter
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: VB 2008 - ListView tool tips

    Quote Originally Posted by weirddemon View Post
    Unless the text for the ListViewItem is cutoff because the text is longer than the column, then this really isn't useful.

    And if it is cutoff, then just make the ShowItemToolTips True.

    ie:



    Thanks for the contribution though.
    I thought ShowItemToolTips only worked on the first item. Even if I'm wrong about that we have cramped listviews that don't show the entire columns because of space on the form. Also we use the same listview in all our programs as a user control and we consider it a nice feature that is consistant in it's behavior. We don't consider it useless and perhaps others won't either.

    One man's trash is another man's treasure

  5. #5
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: VB 2008 - ListView tool tips

    Quote Originally Posted by TysonLPrice View Post
    I thought ShowItemToolTips only worked on the first item. Even if I'm wrong about that we have cramped listviews that don't show the entire columns because of space on the form. Also we use the same listview in all our programs as a user control and we consider it a nice feature that is consistant in it's behavior. We don't consider it useless and perhaps others won't either.

    One man's trash is another man's treasure
    No. That property applies to every ListViewItem.

    I'm not saying that your code is trash, but what I'm saying is that it's better just to turn on a property with two clicks, than write a bunch of code for no reason. And if you have a custom control that has other changes you've made, then all you need to do is turn on that property and delete your ToolTip code. It just clutters things up.

    But either way, it's important that I mention this. If someone else comes across this thread and all they see is your method, they may get the idea that no other method exists.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  6. #6

    Thread Starter
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: VB 2008 - ListView tool tips

    Quote Originally Posted by weirddemon View Post
    No. That property applies to every ListViewItem.

    I'm not saying that your code is trash, but what I'm saying is that it's better just to turn on a property with two clicks, than write a bunch of code for no reason. And if you have a custom control that has other changes you've made, then all you need to do is turn on that property and delete your ToolTip code. It just clutters things up.

    But either way, it's important that I mention this. If someone else comes across this thread and all they see is your method, they may get the idea that no other method exists.
    OK...fair enough concerning someone else seeing this. I painted a listview, set ShowItemToolTips to true, loaded three rows and three columns and the behavior is not consistant. Additionally, I went down this path because of that and I wanted them on all the sub items. I googled that and found the same question many times including a post here with a method similar to what I did. Please take the code I posted and comment out the routine. Set ShowItemToolTips to true and see if it works on all the items and sub items consistantly.

    If it does I will ask for this post to be deleted.
    Last edited by TysonLPrice; May 19th, 2011 at 10:53 AM.

  7. #7
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: VB 2008 - ListView tool tips

    Quote Originally Posted by TysonLPrice View Post
    OK...fair enough concerning someone else seeing this. I painted a listview, set ShowItemToolTips to true, loaded three rows and three columns and the behavior is not consistant. Additionally, I went down this path because of that and I wanted them on all the sub items. I googled that and found the same question many times including a post here with a method similar to what I did. Please take the code I posted and comment out the routine. Set ShowItemToolTips to true and see if it works one all the items and sub items consistantly.

    If it does I will ask for this post to be deleted.
    What do you mean it's not consistent? If the text is outside of the columns bounds and you hover over it, it will show every time. Now, if you're expecting it to show even if the text is within the bounds of the columns width, then it won't. And it shouldn't. It can all be seen, so there's no point.

    But regardless, this post should be deleted. It's always good to see two solutions to the same problem. Even though you can just set one property, the user might need a more fine tune control over it and so this code would help them achieve that.

    Not only that, but this thread will help any user who is having the same problem resolve it by either method.

    I'm glad you've posted your code, though. We just need to present all options so people can pick and choose.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  8. #8

    Thread Starter
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: VB 2008 - ListView tool tips

    Digging in a little deeper I came across this which may help explain why we differ on this. Mainly on the mode you are in:

    "I don't say much about my .NET work here, sometimes because the nature of that work is proprietary, but usually because there's so much about .NET already on the web that it'd just be redundant. I had a hard time finding out exactly how .NET 2.0 supported tooltips for ListView subitems, though, so I thought I'd put a small blurb on the web for the benefit of others...


    The ListView control in .NET 2.0 offers a number of improvements, one of which is a new property called ShowItemTooltips. When true, this property allows a tooltip to be displayed when the user mouses over an item in the ListView. The text displayed for the ListViewItem can be set by its ToolTipText property.

    All very nice, but I had an application where I was displaying a table of data (a ListView in "details" mode) where some of the data could quite naturally appear longer than the column width allowed. A tooltip containing the subitem text seemed just the ticket, and according to the documentation for ShowItemTooltips, "When FullRowSelect is set to true, ToolTips for a ListViewItem.ListViewSubItem will not be shown; only the ToolTip for the parent ListViewItem will display."

    Tantalizing: Microsoft implies that tooltips for subitems are supported somehow. Browsing ListViewSubItem members, however, shows no ToolTipText property similar to that for ListViewItem. So what is Microsoft's documentation really saying?

    A little experimentation turned up the answer. If you set ShowItemTooltips for the ListView control in "details" mode and do nothing else, the ListView control will automatically provide tooltips for items and subitems that exceed their column's widths. This turns out to work even if the FullRowSelect property is set to true. If ToolTipText has been set for a ListViewItem and FullRowSelect is true, then the tooltip will appear for the whole row; that's the case where tooltips won't be displayed for subitems.

    Note that, for subitems, the tooltip text will be the same as the subitem text. In my case, that's exactly what I wanted, and I think it's a sensible default behavior. I can imagine applications, though, where it might be handy to have a ToolTipText property on ListViewSubItems, allowing the tooltip to differ from the subitem text. If that's what you need, you might have to look elsewhere until the next iteration of the .NET framework."

  9. #9

    Thread Starter
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: VB 2008 - ListView tool tips

    Quote Originally Posted by weirddemon View Post
    What do you mean it's not consistent? If the text is outside of the columns bounds and you hover over it, it will show every time. Now, if you're expecting it to show even if the text is within the bounds of the columns width, then it won't. And it shouldn't. It can all be seen, so there's no point.

    But regardless, this post should be deleted. It's always good to see two solutions to the same problem. Even though you can just set one property, the user might need a more fine tune control over it and so this code would help them achieve that.

    Not only that, but this thread will help any user who is having the same problem resolve it by either method.

    I'm glad you've posted your code, though. We just need to present all options so people can pick and choose.
    I see what you mean if all the text is visible it doesn't show. The article I posted also sheads some light on why I went down this path.

    By the way...when you said "But regardless, this post should be deleted." is that what you meant? The context around that seemed to indicate to me to leave it in place.

    Finally, if you Google showing tooltips on sub items you will get lots of hits and solutions similar to what I posted. I'm not the only one that got confused.

  10. #10
    New Member
    Join Date
    Jun 2011
    Posts
    6

    Re: VB 2008 - ListView tool tips

    Sorry to hark on about this but I've found setting
    the showitem tool tips property to be rather useless
    if you happen to have the form topmost...the tips
    fall behind the form,haven't tried the coded,
    maybe the same thing?...just an observation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width