How do you set a tooltip to different listbox items in when bound to a dataView?

I have a listbox, it's data source is a dataview (bound to a data table). The classic problem: how do I get my tooltip to show individual items?

This following code only applies to the controls own collection of Items as it throws a System.InvalidCastException: "Cast from type 'DataRowView' to type 'String' is not valid"

Private Sub MyListBox_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyListBox.MouseMove
        Static lastIndex As Integer = -1
        Dim index As Integer = MyListBox.IndexFromPoint(MyListBox.PointToClient(Cursor.Position))
        If index <> ListBox.NoMatches Then
            If index <> lastIndex Then
                lastIndex = index
                MyToolTip.SetToolTip(MyListBox, CStr(MyListBox.Items(index)))
            End If
        End If
    End Sub

How do I alter it accordingly?
Who is Participating?
Bob LearnedCommented:
MyToolTip.SetToolTip(MyListBox, CType(MyListBox.Items(index), DataRowView)("Column name goes here").ToString())

I think this way it should read column with index 0 from listobox item (as DataRowView) with "index":

MyToolTip.SetToolTip(MyListBox, CStr(MyListBox.Items(index)(0)))

Fahad MukhtarDistinguished EngineerCommented:
MyToolTip.SetToolTip(MyListBox, MyListBox.Items(index).ToString())
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

IvanHowarthAuthor Commented:
paab: yours works, but I have turn off option strict ("Option strict disallows late binding"), which I don't want to. Is there a way of keeping it on?

Desp: your option just feeds the tooltip with the string "System.Data.DataRowView" for every item.
IvanHowarthAuthor Commented:
Bob, it works.

Thanks, to you all.

Another quick one - how do you get a multi-row tooltip? It would really be practical if I could show the values of two dataview columns.

I'll share the points upon this answer.
Bob LearnedCommented:
Something along these lines:

    Me.ToolTip1.SetToolTip(Me.PictureBox1, "This is a" & vbLf & "test")

IvanHowarthAuthor Commented:

The correct line for the multiline tooltip is:

MyToolTip(MyListBox, CType(MyListBox.Items(index), DataRowView)("Column1").ToString() + vbLf + CType(MyListBox.Items(index), DataRowView)("Column2").ToString())

Thanks all again
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.