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




Reply With Quote