FontComboBox

    • VB.NET

    Es gibt 2 Antworten in diesem Thema. Der letzte Beitrag () ist von ~blaze~.

      FontComboBox

      Hi
      Hier gibts mal nen Code für die beliebte FontComboBox:

      VB.NET-Quellcode

      1. Imports System.Windows.Forms
      2. Imports System.ComponentModel
      3. Public Class FontComboBox : Inherits ComboBox
      4. Private Sub FontComboBox_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Me.DrawItem
      5. Dim itm As Object = Me.Items(e.Index)
      6. Select Case e.State
      7. 'Wenn das Item Angewählt ist
      8. Case DrawItemState.Selected
      9. 'wird ein blauer Hintergrund gezeichnet
      10. e.Graphics.FillRectangle(Brushes.MidnightBlue, e.Bounds)
      11. If TypeOf itm Is FontFamily Then
      12. Try
      13. e.Graphics.DrawString(CType(itm, FontFamily).Name, New Font(CType(itm, FontFamily), 10), Brushes.White, e.Bounds.Location)
      14. Catch ex As Exception
      15. 'Kein Format vorhanden oder anderer Fehler
      16. e.Graphics.DrawString(CType(itm, FontFamily).Name, New Font("Arial", 9), Brushes.White, e.Bounds.Location)
      17. End Try
      18. ElseIf TypeOf itm Is Font Then
      19. e.Graphics.DrawString(CType(itm, Font).Name, itm, Brushes.White, e.Bounds.Location)
      20. Else
      21. e.Graphics.DrawString(itm.ToString, New Font("Arial", 9), Brushes.White, e.Bounds.Location)
      22. End If
      23. Case Else
      24. e.Graphics.FillRectangle(Brushes.White, e.Bounds)
      25. If TypeOf itm Is FontFamily Then
      26. Try
      27. e.Graphics.DrawString(CType(itm, FontFamily).Name, New Font(CType(itm, FontFamily), 10), Brushes.Black, e.Bounds.Location)
      28. Catch ex As Exception
      29. 'Kein Format vorhanden oder anderer Fehler
      30. e.Graphics.DrawString(CType(itm, FontFamily).Name, New Font("Arial", 9), Brushes.Black, e.Bounds.Location)
      31. End Try
      32. ElseIf TypeOf itm Is Font Then
      33. e.Graphics.DrawString(CType(itm, Font).Name, itm, Brushes.Black, e.Bounds.Location)
      34. Else
      35. e.Graphics.DrawString(itm.ToString, New Font("Arial", 9), Brushes.Black, e.Bounds.Location)
      36. End If
      37. End Select
      38. End Sub
      39. Private Sub FontComboBox_Format(ByVal sender As Object, ByVal e As System.Windows.Forms.ListControlConvertEventArgs) Handles Me.Format
      40. If TypeOf e.ListItem Is FontFamily Then
      41. 'Wenn das gewählte Item eine Font Family ist, soll der Name von ihr dargestellt werden.
      42. e.Value = CType(e.ListItem, FontFamily).Name
      43. End If
      44. End Sub
      45. 'Ausgeblender Modus
      46. <Browsable(False), EditorBrowsable(EditorBrowsableState.Never)> _
      47. Public Shadows Property DrawMode() As DrawMode
      48. Get
      49. Return MyBase.DrawMode
      50. End Get
      51. Set(ByVal value As DrawMode)
      52. MyBase.DrawMode = value
      53. End Set
      54. End Property
      55. ''' <summary>
      56. ''' Gibt an, ob die Schriftarten formattiert dargestellt werden sollen.
      57. ''' </summary>
      58. Public Property EnableFontFormatting() As Boolean
      59. Get
      60. Return MyBase.DrawMode = Windows.Forms.DrawMode.OwnerDrawVariable
      61. End Get
      62. Set(ByVal value As Boolean)
      63. If value = True Then
      64. MyBase.DrawMode = Windows.Forms.DrawMode.OwnerDrawVariable
      65. Else
      66. MyBase.DrawMode = Windows.Forms.DrawMode.Normal
      67. End If
      68. End Set
      69. End Property
      70. Public Sub New()
      71. '"Eigenhändiges" Zeichnen der Items aktivieren
      72. MyBase.DrawMode = Windows.Forms.DrawMode.OwnerDrawVariable
      73. End Sub
      74. End Class



      Ganz einfach zu bedienen:

      VB.NET-Quellcode

      1. Public Sub AddFonts()
      2. FontComboBox1.Items.AddRange(FontFamily.Families)
      3. End Sub


      Gruß
      ~blaze~
    • Benutzer online 1

      1 Besucher

    • 2 Benutzer haben hier geschrieben