如何在.Net中的ListView子项上设置工具提示

问题描述 投票:3回答:5

我正在尝试为listview控件中的一些子项设置工具提示文本。我无法获得显示的工具提示。

有人有什么建议吗?

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
vb.net listview tooltip
5个回答
4
投票

ObjectListView(围绕.NET WinForms ListView的开源包装)内置了对单元工具提示的支持(是的,它确实可以与VB一起使用)。你在听CellToolTip事件,你可以做这样的事情(这无疑是过分的):

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;

显然,这是C#,而不是VB,但你明白了。


9
投票

你可以使用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, "");
    }
}

5
投票

假设.NET 2.0或更高版本,您还可以将ListView.ShowItemToolTips设置为true。如果需要自定义给定项目的工具提示文本,请将ListViewItem.ToolTipText设置为要显示的字符串。


4
投票

问题中的原始代码不起作用,因为它在New ToolTip中创建了OnMouseMove。我猜ToolTip.Show方法是异步的,因此函数在调用后立即退出,破坏了临时的ToolTip。当Show执行时,该对象不再存在。

解决方案是通过以下方式创建持久性ToolTip对象:

  1. 形式上的ToolTip控制;要么
  2. 私人ToolTip类领域(在该类的FinalizeDispose方法中处置);要么
  3. 函数内部的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's 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

我已经使代码尽可能通用,以便可以将函数分配给多个ListViews。


3
投票

如果在“详细信息”模式下为ListView控件设置ShowItemTooltips而不执行任何其他操作,则ListView控件将自动为超出其列宽的项目和子项提供工具提示。即使FullRowSelect属性设置为true,这也会起作用。如果为ListViewItem设置了ToolTipText并且FullRowSelect为true,则将显示整行的工具提示;这就是不会为子项目显示工具提示的情况。

© www.soinside.com 2019 - 2024. All rights reserved.