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

Posted on 2006-05-04
Last Modified: 2008-01-09
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?
Question by:IvanHowarth
    LVL 6

    Assisted Solution

    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)))

    LVL 19

    Expert Comment

    MyToolTip.SetToolTip(MyListBox, MyListBox.Items(index).ToString())

    Author Comment

    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.
    LVL 96

    Accepted Solution

    MyToolTip.SetToolTip(MyListBox, CType(MyListBox.Items(index), DataRowView)("Column name goes here").ToString())


    Author Comment

    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.
    LVL 96

    Expert Comment

    by:Bob Learned
    Something along these lines:

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


    Author Comment


    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now