如何查看ListBox中的项目长度超过控件的宽度

时间:2013-11-13 19:41:16

标签: listbox vb.net-2010 listbox-control

我有一个具有特定尺寸的列表框 有些项目比ListBox控件的宽度更长 当我将鼠标移过这些项目时,我希望看到所有名称。 我正在使用vb.net

1 个答案:

答案 0 :(得分:1)

试试这个:

Private Sub ListBox1_MouseMove (ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseMove
    Dim ListMousePosition As Point = Me.ListBox1.PointToClient(Me.MousePosition)
    Dim itemIndex As Integer = Me.ListBox1.IndexFromPoint(ListMousePosition)
    If itemIndex > -1 Then
        Dim s As String = Me.ListBox1.Items(itemIndex).ToString()
        Dim g As Graphics = Me.ListBox1.CreateGraphics()
        If g.MeasureString(s, Me.ListBox1.Font).Width > Me.ListBox1.ClientRectangle.Width Then
        Me.ToolTip.SetToolTip(Me.ListBox1, s)
        else
        Me.ToolTip.SetToolTip(Me.ListBox, "")
        End If
        g.Dispose()
    End If
End Sub