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

4 vb.net listview tooltip

我正在尝试为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
Run Code Online (Sandbox Code Playgroud)

小智 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, "");
    }
}
Run Code Online (Sandbox Code Playgroud)


And*_*ndy 5

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