mario.deghetto

Visual Basic & .NET
posts - 332, comments - 79, trackbacks - 0

[VB.NET] Elencare i font installati nel sistema (con LINQ e senza)

Se volete avere l’elenco di tutti i font installati nel vostro sistema, potete utilizzare almeno due modi diversi: uno “classico” e uno con LINQ. Vediamoli:

1. Creiamo un nuovo progetto di applicazione Windows Forms (per semplicità)

2. Inseriamo un controllo di tipo ListBox e due controlli di tipo Button

3. Il codice dei due gestori degli eventi Click (e il fondamentale “Imports” iniziale) è il seguente:

 

Imports System.Drawing.Text

Public Class Form1
    Private Sub Button1_Click(
            ByVal sender As System.Object,
            ByVal e As System.EventArgs) _
            Handles Button1.Click

        ' ------- metodo "classico" -------
        ListBox1.Items.Clear()

        Dim fonts As New InstalledFontCollection
        For Each one As FontFamily In fonts.Families
            ListBox1.Items.Add(one.Name)
        Next

    End Sub

    Private Sub Button2_Click(
            ByVal sender As System.Object,
            ByVal e As System.EventArgs) _
            Handles Button2.Click

        ' ------- metodo con LINQ -------
        ListBox1.Items.Clear()
        Dim lista = From f
                    In (New InstalledFontCollection).Families
                    Select f.Name

        For Each f As String In lista
            ListBox1.Items.Add(f)
        Next

    End Sub
End Class

 

In entrambi i casi, il risultato è il seguente:

image

Download del progetto di esempio

Print | posted on mercoledì 27 ottobre 2010 20.30 |

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 2 and 7 and type the answer here:

Powered by: