LordSauce
asked on
Windows Forms Listitem Tooltip
Does anyone know if it's possible to give individual list items a tooltip in .NET? It was possible in VB6, so I can't believe the functionality has gone, but I can't find any mention of how to do it.
The reason I'm after this is that my list occasionally contains items which are too big to display, and I'd like to be able to pop-up a tooltip with the full item text.
Thanks.
Sauce.
The reason I'm after this is that my list occasionally contains items which are too big to display, and I'd like to be able to pop-up a tooltip with the full item text.
Thanks.
Sauce.
ASKER
Ideally I'd like to display a tooltip for the item under the cursor, rather than the selected item.
I am not sure that you can do that. But I might be wrong
Hi LordSauce:
Based partly on the help example for tooltips
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.ICon tainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents ListBox1 As System.Windows.Forms.ListB ox
<System.Diagnostics.Debugg erStepThro ugh()> Private Sub InitializeComponent()
Me.ListBox1 = New System.Windows.Forms.ListB ox
Me.SuspendLayout()
'
'ListBox1
'
Me.ListBox1.Items.AddRange (New Object() {"Hello, how are you today!", "I am well thanks!"})
Me.ListBox1.Location = New System.Drawing.Point(56, 64)
Me.ListBox1.Name = "ListBox1"
Me.ListBox1.Size = New System.Drawing.Size(96, 30)
Me.ListBox1.TabIndex = 0
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.ListBox 1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Dim toolTip1 As New ToolTip
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Set up the delays for the ToolTip.
toolTip1.AutoPopDelay = 5000
toolTip1.InitialDelay = 1000
toolTip1.ReshowDelay = 500
' Force the ToolTip text to be displayed whether or not the form is active.
toolTip1.ShowAlways = True
' Set up the ToolTip text for the Button and Checkbox.
toolTip1.SetToolTip(Me.Lis tBox1, "My checkBox1")
End Sub
Private Sub ListBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.Mouse EventArgs) Handles ListBox1.MouseMove
Try
Dim pt As New System.Drawing.Point(e.X, e.Y)
'Dim s As String = CStr(ListBox1.GetChildAtPo int(pt).Te xt)
toolTip1.SetToolTip(ListBo x1, ListBox1.Items(e.Y Mod 12).ToString)
Catch
End Try
End Sub
End Class
Dabas
Based partly on the help example for tooltips
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.ICon
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents ListBox1 As System.Windows.Forms.ListB
<System.Diagnostics.Debugg
Me.ListBox1 = New System.Windows.Forms.ListB
Me.SuspendLayout()
'
'ListBox1
'
Me.ListBox1.Items.AddRange
Me.ListBox1.Location = New System.Drawing.Point(56, 64)
Me.ListBox1.Name = "ListBox1"
Me.ListBox1.Size = New System.Drawing.Size(96, 30)
Me.ListBox1.TabIndex = 0
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.ListBox
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Dim toolTip1 As New ToolTip
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Set up the delays for the ToolTip.
toolTip1.AutoPopDelay = 5000
toolTip1.InitialDelay = 1000
toolTip1.ReshowDelay = 500
' Force the ToolTip text to be displayed whether or not the form is active.
toolTip1.ShowAlways = True
' Set up the ToolTip text for the Button and Checkbox.
toolTip1.SetToolTip(Me.Lis
End Sub
Private Sub ListBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.Mouse
Try
Dim pt As New System.Drawing.Point(e.X, e.Y)
'Dim s As String = CStr(ListBox1.GetChildAtPo
toolTip1.SetToolTip(ListBo
Catch
End Try
End Sub
End Class
Dabas
I am not completely happy with this solution, but it will point you in the right direction.
You can see the commented line
Dim s As String = CStr(ListBox1.GetChildAtPo int(pt).Te xt)
I would have assumed that line would return the correct string, but when I tried it out, GetChildAtPoint returned Nothing.
e.Y Mod 12 will kind of work if your font size is set to 12 points.
You can see the commented line
Dim s As String = CStr(ListBox1.GetChildAtPo
I would have assumed that line would return the correct string, but when I tried it out, GetChildAtPoint returned Nothing.
e.Y Mod 12 will kind of work if your font size is set to 12 points.
Hmmm.
Just checked and the font size is definitely smaller than 12.
Anyhow, this should be pointing you in the right direction!
Dabas
Just checked and the font size is definitely smaller than 12.
Anyhow, this should be pointing you in the right direction!
Dabas
GetChildAtPoint returns a child control, not the item under the mouse pointer. you can get it with the IndexFromPoint method:
Private Sub ListBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.Mouse EventArgs) Handles ListBox1.MouseMove
Try
Dim pt As New System.Drawing.Point(e.X, e.Y)
Dim s As String = CStr(ListBox1.Items(ListBo x1.IndexFr omPoint(pt )))
toolTip1.SetToolTip(ListBo x1, s)
Catch
End Try
End Sub
this works very well
the edge
Private Sub ListBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.Mouse
Try
Dim pt As New System.Drawing.Point(e.X, e.Y)
Dim s As String = CStr(ListBox1.Items(ListBo
toolTip1.SetToolTip(ListBo
Catch
End Try
End Sub
this works very well
the edge
the-edge:
> this works very well
It certainly does!
Dabas
> this works very well
It certainly does!
Dabas
ASKER
Thanks edge, but that only exists for listboxes, not list views!
Each item in my list has an icon, which (AFAIK) rules the listbox out.
Something similar for the listview would be great though...
Thanks also Dabas - will continue playing with your code!
Each item in my list has an icon, which (AFAIK) rules the listbox out.
Something similar for the listview would be great though...
Thanks also Dabas - will continue playing with your code!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This is what I've ended up using.
Storing the last available tooltip at the form level prevents the tooltip moving with the mouse - the code to set the tooltip is only fired once each time an individual listitem is entered, so the tooltip is only displayed once.
I'm also testing the length of the item to see whether a tooltip is required - I only want to display it if the text of the item is partly hidden. Because of the icon being displayed, it's necessary to adjust the width of the list slightly - 16 pixels seems to work fine.
Thanks again for your help!
-------------------------- ---------- ---------- ---------- ----------
Private Sub lvwAvailable_MouseMove(ByV al sender As Object, ByVal e As System.Windows.Forms.Mouse EventArgs) Handles lvwAvailable.MouseMove
Try
Dim itmTemp As ListViewItem = lvwAvailable.GetItemAt(e.X , e.Y)
' check mouse is over a list item
If (Not IsNothing(itmTemp)) Then
' check whether tooltip has already been displayed since entering the item
If (Not (itmTemp.Text.Equals(strLa stAvailabl eTooltip)) ) Then
Dim g As Graphics = CreateGraphics()
' check whether tooltip is required
' only necessary if item not fully visible (width - 16 to take icon into account)
If g.MeasureString(itmTemp.Te xt, itmTemp.Font).Width > lvwAvailable.Width - 16 Then
ttpToolTip.SetToolTip(lvwA vailable, itmTemp.Text)
strLastAvailableTooltip = itmTemp.Text
Else
ttpToolTip.SetToolTip(lvwA vailable, String.Empty)
strLastAvailableTooltip = String.Empty
End If
End If
End If
Catch
End Try
End Sub
Storing the last available tooltip at the form level prevents the tooltip moving with the mouse - the code to set the tooltip is only fired once each time an individual listitem is entered, so the tooltip is only displayed once.
I'm also testing the length of the item to see whether a tooltip is required - I only want to display it if the text of the item is partly hidden. Because of the icon being displayed, it's necessary to adjust the width of the list slightly - 16 pixels seems to work fine.
Thanks again for your help!
--------------------------
Private Sub lvwAvailable_MouseMove(ByV
Try
Dim itmTemp As ListViewItem = lvwAvailable.GetItemAt(e.X
' check mouse is over a list item
If (Not IsNothing(itmTemp)) Then
' check whether tooltip has already been displayed since entering the item
If (Not (itmTemp.Text.Equals(strLa
Dim g As Graphics = CreateGraphics()
' check whether tooltip is required
' only necessary if item not fully visible (width - 16 to take icon into account)
If g.MeasureString(itmTemp.Te
ttpToolTip.SetToolTip(lvwA
strLastAvailableTooltip = itmTemp.Text
Else
ttpToolTip.SetToolTip(lvwA
strLastAvailableTooltip = String.Empty
End If
End If
End If
Catch
End Try
End Sub
current selected item, just create a tooltip object ,set its properties to
match listbox selected item.
Something like:
toolTip1.SetToolTip(Me.Lis
toolTip1.ShowAlways=True