fork download
  1. Imports System.Drawing
  2. Imports System.Windows.Forms
  3.  
  4. Public Class WrapedListBox
  5.  
  6. Inherits System.Windows.Forms.ListBox
  7.  
  8. Public Sub New()
  9.  
  10. Me.ScrollAlwaysVisible = True
  11. Me.DrawMode = Windows.Forms.DrawMode.OwnerDrawVariable
  12.  
  13. End Sub
  14.  
  15. Protected Overrides Sub OnClientSizeChanged(e As EventArgs)
  16. MyBase.OnClientSizeChanged(e)
  17. Me.Refresh()
  18. End Sub
  19.  
  20. Protected Overrides Sub OnMeasureItem(ByVal e As System.Windows.Forms.MeasureItemEventArgs)
  21.  
  22. If DesignMode Then
  23. MyBase.OnMeasureItem(e)
  24. Exit Sub
  25. End If
  26.  
  27. Dim proposedSize As Size = New Size(Me.ClientSize.Width, Integer.MaxValue)
  28. 'Dim flags As TextFormatFlags = TextFormatFlags.WordBreak
  29. 'Dim returnValue As Size = TextRenderer.MeasureText(e.Graphics, Me.Items(e.Index).ToString, Me.Font, proposedSize, flags)
  30. 'e.ItemWidth = returnValue.Width
  31. 'e.ItemHeight = returnValue.Height
  32.  
  33. Using sf = New System.Drawing.StringFormat()
  34. Dim sizeF As SizeF = e.Graphics.MeasureString(Me.Items(e.Index).ToString(), Me.Font, proposedSize, sf)
  35. e.ItemWidth = Convert.ToInt32(sizeF.Width + 0.9)
  36. e.ItemHeight = Convert.ToInt32(sizeF.Height + 4.9)
  37. End Using
  38.  
  39. MyBase.OnMeasureItem(e)
  40.  
  41. End Sub
  42.  
  43. Protected Overrides Sub OnDrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs)
  44.  
  45. If DesignMode Then
  46. MyBase.OnDrawItem(e)
  47. Exit Sub
  48. End If
  49.  
  50. e.DrawBackground()
  51.  
  52. 'Dim brush = New SolidBrush(e.ForeColor)
  53. 'e.Graphics.DrawString(Me.Items(e.Index).ToString, e.Font, brush, e.Bounds, StringFormat.GenericDefault)
  54.  
  55. Using brush = New SolidBrush(e.ForeColor)
  56. Using sf = New System.Drawing.StringFormat()
  57. Dim rect As Rectangle = e.Bounds
  58. e.Graphics.DrawString(Me.Items(e.Index).ToString, e.Font, brush, rect, sf)
  59. End Using
  60. End Using
  61.  
  62. MyBase.OnDrawItem(e)
  63.  
  64. End Sub
  65.  
  66. End Class
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty