0

Does anyone know how to show text that is wider than the listbox, I found some code, it uses a tooltip to show text if its wider but its not working, help please

using VB 2010

Private Sub ListBox2_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox2.MouseMove
    Dim ListMousePosition As Point = Me.ListBox2.PointToClient(Me.MousePosition)
    Dim itemIndex As Integer = Me.ListBox2.IndexFromPoint(ListMousePosition)
    If itemIndex > -1 Then
        Dim s As String = Me.ListBox2.Items(itemIndex).ToString()
        Dim g As Graphics = Me.ListBox2.CreateGraphics()

        If g.MeasureString(s, Me.ListBox2.Font).Width > Me.ListBox2.ClientRectangle.Width Then
            Me.ToolTip1.SetToolTip(Me.ListBox2, s)
        Else
            Me.ToolTip1.SetToolTip(Me.ListBox2, "")
        End If
         g.Dispose()
    End If
End Sub
2
  • Perhaps you could define "not working". Have you debugged? If not, do so and explain exactly where the behaviour of the code differs from your expectation. Commented Mar 13, 2017 at 23:19
  • it appears that s is always "System.Data.DataRowView" so the width is constant at 175, the Dim s As String = Me.ListBox2.Items(itemIndex).ToString() is not picking up the text in the listbox where the mouse is, and I don't know how to change it Commented Mar 14, 2017 at 7:09

2 Answers 2

1

If you have bound a DataTable to the ListBox then each item is a DataRowView, which is why you get that text when calling ToString on an item. The ListBox has a GetItemText method that will get you the displayed text for a specific item.

See how easy that was, once you told us what the actual problem was? ALWAYS provide ALL the relevant information. What actually happens is always relevant.

Sign up to request clarification or add additional context in comments.

Comments

0

great that works here's the modified code, only issue now is the flickering of the tooltip text, is there anyway to stop that

   If itemIndex > -1 Then
                  Dim item As Object = ListBox2.Items(itemIndex) 'get the item at that index.
        Dim s As String = ListBox2.GetItemText(item) 'get the text displayed
        Dim g As Graphics = Me.ListBox2.CreateGraphics()

        If g.MeasureString(s, Me.ListBox2.Font).Width > Me.ListBox2.ClientRectangle.Width Then
            Me.ToolTip1.SetToolTip(Me.ListBox2, s)
        Else
            Me.ToolTip1.SetToolTip(Me.ListBox2, "")
        End If
        g.Dispose()
    End If

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.