2017-03-13 5 views
0

よりも、リストボックスよりも広くなっているテキストを表示する方法を誰もが知っている、私はそれがより広いが、そのが動作していない、VB表示リストボックスのテキストは、より広いボックス

を助けてください場合は、テキストを表示するツールチップを使用して、いくつかのコードを見つけましたあなたはListBoxDataTableをバインドしている場合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 
+0

はおそらく、あなたが「働いていない」を定義することができることを停止するようにとにかくあります。あなたはデバッグしましたか?そうでない場合は、コードの動作があなたの期待と異なるところを正確に説明してください。 – jmcilhinney

+0

それはsが常に "System.Data.DataRowView"であるので、幅が175で一定であるため、Dim s As String = Me.ListBox2.Items(itemIndex).ToString()はリストボックス内のテキストをピックアップしません。マウスは、私はそれを変更する方法を知らない – JPS

答えて

1

を使用すると、各アイテムは、アイテムにToStringを呼び出すときにそのテキストを取得する理由である、DataRowViewです。 ListBoxにはGetItemTextメソッドがあり、特定のアイテムの表示テキストが表示されます。

実際の問題の内容を教えていただければ、それがどれほど簡単かをご確認ください。すべての関連情報を常に提供します。実際に起こることは常に関連しています。変更されたコードを、ここで働くのです

0

素晴らしいが、唯一の問題は今、ツールヒントのテキストのちらつきで、

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 
関連する問題