VB显示列表框文本宽于框

时间:2017-03-13 20:32:11

标签: vb.net listbox

有没有人知道如何显示比列表框更宽的文本,我找到了一些代码,它使用工具提示来显示文本,如果它更宽但不起作用,请帮助

使用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 个答案:

答案 0 :(得分:1)

如果您已将DataTable绑定到ListBox,那么每个项目都是DataRowView,这就是您在项目上调用ToString时获得该文本的原因。 ListBox有一个GetItemText方法,可以显示特定项目的显示文字。

一旦你告诉我们实际问题是什么,看看有多容易?始终提供所有相关信息。实际发生的事情总是相关的。

答案 1 :(得分:0)

很棒,这里有效的修改后的代码,现在唯一的问题就是工具提示文本的闪烁,无论如何都要停止那个

 <input type="file" id="fileInput" style="display:none;" (change)="setFileName()"/>
相关问题