How to set tooltips in ListView subelements in .Net

I am trying to set tooltip text for some of my subitems in my listview control. I cannot get a tool tip.

Anyone have any suggestions?

Private _timer As Timer
Private Sub Timer()
    If _timer Is Nothing Then
        _timer = New Timer
        _timer.Interval = 500
        AddHandler _timer.Tick, AddressOf TimerTick
        _timer.Start()
    End If
End Sub
Private Sub TimerTick(ByVal sender As Object, ByVal e As EventArgs)
    _timer.Enabled = False
End Sub

Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
    If Not _timer.Enabled Then
        Dim item = Me.HitTest(e.X, e.Y)
        If Not item Is Nothing AndAlso Not item.SubItem Is Nothing Then
            If item.SubItem.Text = "" Then
                Dim tip = New ToolTip
                Dim p = item.SubItem.Bounds
                tip.ToolTipTitle = "Status"
                tip.ShowAlways = True
                tip.Show("FOO", Me, e.X, e.Y, 1000)
                _timer.Enabled = True
            End If
        End If
    End If

    MyBase.OnMouseMove(e)
End Sub
+3
source share
5 answers

ObjectListView ( an open source wrapper for .NET WinForms ListView) has built-in support for tooltips for cells (and, yes, it works with VB). You listen to the event CellToolTipand can do such things (which are admittedly excessive):

alt text

ObjectListView, ListView, WM_NOTIFY, TTN_GETDISPINFO , :

case TTN_GETDISPINFO:
    ListViewHitTestInfo info = this.HitTest(this.PointToClient(Cursor.Position));
    if (info.Item != null && info.SubItem != null) {
        // Call some method of your own to get the tooltip you want
        String tip = this.GetCellToolTip(info.Item, info.SubItem); 
        if (!String.IsNullOrEmpty(tip)) {
            NativeMethods.TOOLTIPTEXT ttt = (NativeMethods.TOOLTIPTEXT)m.GetLParam(typeof(NativeMethods.TOOLTIPTEXT));
            ttt.lpszText = tip;
            if (this.RightToLeft == RightToLeft.Yes)
                ttt.uFlags |= 4;
            Marshal.StructureToPtr(ttt, m.LParam, false);
            return; // do not do normal processing
        }
    }
    break;

, #, VB, .

+4

MouseMove:

private void listview1_MouseMove(object sender, MouseEventargs e)
{
    ListViewItem item = listview1.GetItemAt(e.X, e.Y);
    ListViewHitTestInfo info = listview1.HitTest(e.X, e.Y);
    if((item != null) && (info.SubItem != null))
    {
        toolTip1.SetToolTip(listview1, info.SubItem.Text);
    }
    else
    {
        toolTip1.SetToolTip(listview1, "");
    }
}
+8
+5

ShowItemTooltips ListView "" , ListView , . , FullRowSelect true. ToolTipText ListViewItem, FullRowSelect - true, ; , .

+3

, New ToolTip OnMouseMove. , ToolTip.Show , , , , ToolTip. Show , .

ToolTip, :

  • a ToolTip ;
  • ToolTip ( Finalize Dispose );
  • a Static .

, GetItemAt(), ListViewHitTestInfo .
Colin , :

Private Sub ListView_MouseMove(sender As Object, e As MouseEventArgs) _
Handles MyList1.MouseMove
    Static prevMousePos As Point = New Point(-1, -1)

    Dim lv As ListView = TryCast(sender, ListView)
    If lv Is Nothing Then _
        Exit Sub
    If prevMousePos = MousePosition Then _
        Exit Sub  ' to avoid annoying flickering

    With lv.HitTest(lv.PointToClient(MousePosition))
        If .SubItem IsNot Nothing AndAlso Not String.IsNullOrEmpty(.SubItem.Text) Then
            'AndAlso .Item.SubItems.IndexOf(.SubItem) = 1
            '...when a specific Column is needed

            Static t As ToolTip = toolTip1  ' using a form control
            'Static t As New ToolTip()      ' using a private variable
            t.ShowAlways = True
            t.UseFading = True
            ' To display at exact mouse position:
            t.Show(.SubItem.Tag, .Item.ListView, _
                   .Item.ListView.PointToClient(MousePosition), 2000)
            ' To display beneath the list subitem:
            t.Show(.SubItem.Tag, .Item.ListView, _
                   .SubItem.Bounds.Location + New Size(7, .SubItem.Bounds.Height + 1), 2000)
            ' To display beneath mouse cursor, as Windows does:
            ' (size is hardcoded in ugly manner because there is no easy way to find it)
            t.Show(.SubItem.Tag, .Item.ListView, _
                   .Item.ListView.PointToClient(Cursor.Position + New Size(1, 20)), 2000)
        End If
        prevMousePos = MousePosition
    End With        
End Sub

I made the code as general as possible so that the function can be assigned to multiple ListViews.

+2
source

Source: https://habr.com/ru/post/1715995/


All Articles