vb@rchiv
VB Classic
VB.NET
ADO.NET
VBA
C#
TOP-Angebot: 17 bzw. 24 Entwickler-Vollversionen zum unschlagbaren Preis!  
 vb@rchiv Quick-Search: Suche startenErweiterte Suche starten   Impressum  | Datenschutz  | vb@rchiv CD Vol.6  | Shop Copyright ©2000-2024
 
zurück
Rubrik: Grafik und Font · Font & Text   |   VB-Versionen: VB.NET04.05.06
ComboBox zur Schriftauswahl (.NET)

Eine ComboBox, in der alle verfügbaren Schriften grafisch zur Auswahl angezeigt werden.

Autor:   Dieter OtterBewertung:     [ Jetzt bewerten ]Views:  18.254 
www.tools4vb.deSystem:  WinNT, Win2k, WinXP, Win7, Win8, Win10, Win11 Beispielprojekt auf CD 

Heute möchten wir Ihnen eine ComboBox vorstellen, in der alle verfügbaren Schriften zur Auswahl angezeigt werden. Die jeweiligen Einträge sollen hierbei selbstverständlich in der jeweiligen Schriftart angezeigt werden!

Das Füllen der ComboBox mit den verfügbaren Schriften erledigen wir im Form_Load Ereignis. Hier wird dem Control dann auch mitgeteilt, dass die Darstellung der Listen-Elemente nicht autom. erfolgen soll, sondern von uns selbst gezeichnet werden, was durch Setzen der DrawMode-Eigenschaft auf "OwnerDrawFixed" festgelegt wird.

Private Sub Form1_Load(ByVal sender As System.Object, _
  ByVal e As System.EventArgs) Handles MyBase.Load
 
  ' alle verfügbaren Schriften auflisten
  With ComboBox1.Items
    For Each oFont As FontFamily In Font.FontFamily.Families
      .Add(oFont)
    Next
  End With
 
  ' Wir möchten uns um die Ausgabe der 
  ' Listen-Einträge selbst kümmern
  ComboBox1.DrawMode = DrawMode.OwnerDrawFixed
End Sub

Wann immer jetzt ein Eintrag der ComboBox-Liste gezeichnet werden muss, löst das Control das DrawItem-Ereignis aus, d.h. hier muss unser Code zum Zeichnen des Eintrags eingefügt werden:

Private Sub ComboBox1_DrawItem(ByVal sender As Object, _
  ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
 
  ' ComboBox-Eintrag (Schriftname)
  Dim sItem As String = ComboBox1.Items(e.Index).Name
 
  Dim oFont As Font
 
  ' Neues Schrift-Objekt erzeugen und verfügbare Styles berücksichtigen
  Dim oFamily As FontFamily = ComboBox1.Items(e.Index)
  If oFamily.IsStyleAvailable(FontStyle.Regular) Then
    oFont = New Font(sItem, ComboBox1.Font.Size, FontStyle.Regular)
  ElseIf oFamily.IsStyleAvailable(FontStyle.Italic) Then
    oFont = New Font(sItem, ComboBox1.Font.Size, FontStyle.Italic)
  ElseIf oFamily.IsStyleAvailable(FontStyle.Bold) Then
    oFont = New Font(sItem, ComboBox1.Font.Size, FontStyle.Bold)
  ElseIf oFamily.IsStyleAvailable(FontStyle.Underline) Then
    oFont = New Font(sItem, ComboBox1.Font.Size, FontStyle.Underline)
  Else
    oFont = New Font(sItem, ComboBox1.Font.Size, FontStyle.Strikeout)
  End If
 
  ' Brush für Textfarbe
  Dim oBrush As Brush = New SolidBrush(e.ForeColor)
 
  ' Hintergrund
  e.DrawBackground()
 
  ' Text (Schriftname) ausgeben
  e.Graphics.DrawString(sItem, oFont, oBrush, e.Bounds.X, e.Bounds.Y)
 
  ' Aufräumen
  oBrush.Dispose()
End Sub