Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionlsbPolice.Items.Add(fontfamill[i].Name);
Sinon tu dois récupérer un font en fonction du nom sélectionné.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Visualisation { public partial class frmVisu : Form { public frmVisu() { InitializeComponent(); } private void frmVisu_Load(object sender, EventArgs e) { System.Drawing.Text.InstalledFontCollection fonts = new System.Drawing.Text.InstalledFontCollection(); FontFamily[] fontfamill = fonts.Families; for (long i = fontfamill.GetLowerBound(0); i <= fontfamill.GetUpperBound(0); i++) lsbPolice.Items.Add(fontfamill[i].Name); fontfamill = null; fonts = null; lsbPolice.SelectedIndex = 0; lblVisu.Text = "Voici le résultat"; } private void btValider_Click(object sender, EventArgs e) { bool tr = false; int i = 0; int pos = -1; if (txbPolice.Text != null) { while ((i <lsbPolice.Items.Count) && (tr false)) { if (txbPolice == lsbPolice.Items[i]) { tr = true; pos = i; } else i++; } if (pos != -1) { MessageBox.Show("la police que vous avez saisi existe déjà dans la liste !"); txbPolice.Text = null; } else { lsbPolice.Items.Add(txbPolice.Text); txbPolice.Text = null; } } else MessageBox.Show("Veuillez saisir la police à ajouter dans la liste !"); } private void btSupprimer_Click(object sender, EventArgs e) { if (lsbPolice.SelectedItem != null) { lsbPolice.Items.Remove(lsbPolice.SelectedItem); } else { MessageBox.Show("Veuillez sélectionner une police à supprimer dans la liste !"); } } private void btQuitter_Click(object sender, EventArgs e) { Close(); } private void btAffiche_Click(object sender, EventArgs e) { bool tr = false; int i = fontfamill.GetLowerBound(0); int pos = -1; if (lsbPolice.SelectedItem != null) { pos = lsbPolice.SelectedIndex; while (i <fontfamill.GetUpperBound(0) && (tr false)) { if (i == pos) { lblVisu.Font = fontfamill[i].Font; } } } else { MessageBox.Show("Veuillez sélectionner une police à afficher dans la liste !"); } } } }
FontFamily[] fontfamill;//déclaration private void Ailleurs() { .... fontfamill = fonts.Families;//initilaisation ...}
private void btAffiche_Click(object sender, EventArgs e) { if (lsbPolice.SelectedItem != null) { int pos = lsbPolice.SelectedIndex; lblVisu.Font = fontfamill[pos].Font; } else { MessageBox.Show("Veuillez sélectionner une police à afficher dans la liste !"); } }
private void lsbPolice_SelectedItemChanged(object sender, EventArgs e) { AccesButtons(); } private void AccesButtons() { Button1.Visible = (lsbPolice.SelectedItem != null); Button2.Enabled = !Button1.Visible; ....}
lblVisu.Font = fontfamill[pos];
lblVisu.Font = (Font)fontfamill[pos];
lblVisu.Font = new Font(fontfamill[pos].Name,20);
lblVisu.Font fontfamill[pos];> Erreur 1 Impossible de convertir implicitement le type 'System.Drawing.FontFamily' en 'System.Drawing.Font'
lblVisu.Font = (Font)fontfamill[pos];
lblVisu.Font new Font(fontfamill[pos].Name,20);> Pas d'erreur, mais lors du débug : L'exception NullReferenceException n'a pas été gérée - La référence d'objet n'est pas définie à une instance d'un objet.