How Can I Listall Fonts into ListBox with Own FontSyle
I just want to list all fonts into ListBox and fonts. and When I Chose ListBox item Come into TextBox.
I found some codes. But when I chose ListrBox Listed Font, it comes to TextBox like this = "Arial Narrow; 14pt"
Even I used Substring... Not Get a correct Result. ( Maybe I use in wrong Row)
But I want only "Arial Narrow"
is there any easy way to do this correctly ?
Code:
Imports System.Drawing.Text
Public Class Form1
Private Sub BindListBox()
ListBox1.DrawMode = DrawMode.OwnerDrawFixed
ListBox1.Font = New Font("Microsoft Sans Serif, 11.25pt", 11.25)
ListBox1.ItemHeight = 20
Dim objFontFamily As FontFamily
Dim objFontCollection As System.Drawing.Text.FontCollection
Dim tempFont As Font
objFontCollection = New System.Drawing.Text.InstalledFontCollection()
For Each objFontFamily In objFontCollection.Families
If objFontFamily.IsStyleAvailable(FontStyle.Regular) Then
tempFont = New Font(objFontFamily, 14, FontStyle.Regular)
ElseIf objFontFamily.IsStyleAvailable(FontStyle.Bold) Then
tempFont = New Font(objFontFamily, 14, FontStyle.Bold)
ElseIf objFontFamily.IsStyleAvailable(FontStyle.Italic) Then
tempFont = New Font(objFontFamily, 14, FontStyle.Italic)
End If
Dim lst As New ListViewItem
lst.Font = tempFont
lst.Text = tempFont.FontFamily.Name
ListBox1.Items.Add(tempFont)
Next
End Sub
Private Sub ComboBox1_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
e.DrawBackground()
If (e.State And DrawItemState.Focus) <> 0 Then
e.DrawFocusRectangle()
End If
Dim objBrush As Brush = Nothing
Try
objBrush = New SolidBrush(e.ForeColor)
e.Graphics.DrawString(DirectCast(ComboBox1.Items(e.Index), Font).Name, DirectCast(ComboBox1.Items(e.Index), Font), objBrush, e.Bounds)
Finally
If objBrush IsNot Nothing Then
objBrush.Dispose()
End If
objBrush = Nothing
End Try
End Sub
Private Sub ListBox1_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
e.DrawBackground()
If (e.State And DrawItemState.Focus) <> 0 Then
e.DrawFocusRectangle()
End If
Dim objBrush As Brush = Nothing
Try
objBrush = New SolidBrush(e.ForeColor)
e.Graphics.DrawString(DirectCast(ListBox1.Items(e.Index), Font).Name, DirectCast(ListBox1.Items(e.Index), Font), objBrush, e.Bounds)
'e.Graphics.DrawString(ListBox1.Items(e.Index).ToString(), e.Font, New SolidBrush(e.ForeColor), e.Bounds, StringFormat.GenericDefault)
e.DrawFocusRectangle()
Finally
If objBrush IsNot Nothing Then
objBrush.Dispose()
End If
objBrush = Nothing
End Try
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ListBox1.DrawMode = DrawMode.OwnerDrawFixed
BindListBox()
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
TextBox1.Focus()
If ListBox1.SelectedIndex >= 0 AndAlso ListBox1.SelectedIndex < ListBox1.Items.Count - 1 Then
'astring = astring.Substring(0, astring.IndexOf(","))
'Dim LstBxText = TextBox1.Text.Substring(0, TextBox1.Text.IndexOf(";"))
'TextBox1.Text = LstBxText
TextBox1.Text = ListBox1.Text 'ListBox1.Text 'ListBox1.SelectedItem.ToString()
TextBox1.Focus()
TextBox1.SelectAll()
End If
End Sub
End Class
Re: How Can I Listall Fonts into ListBox with Own FontSyle
Re: How Can I Listall Fonts into ListBox with Own FontSyle
Thank you Paul. I miss some code Rows in this sample. Thank you again. God bless you.