Changer la font d'une textbox depuis une combobox

Résolu
penchu Messages postés 167 Date d'inscription mercredi 11 février 2004 Statut Membre Dernière intervention 5 janvier 2006 - 21 déc. 2004 à 14:23
penchu Messages postés 167 Date d'inscription mercredi 11 février 2004 Statut Membre Dernière intervention 5 janvier 2006 - 21 déc. 2004 à 15:11
dans ma form, g une combobox et une textbox.
dans la combo, j'ai mis un code qui reprend la liste de toutes les fonts installées sur ma machine.
Comment peut-on faire pour assigner une font a une textbox?

J'ai essayé la facon suivante :
this.textBox1.font=this.combobox1.selectedindex;

mais ca ne marche pas

www.penchu.dyndns.org (si mon serveur est allumé)

4 réponses

cs_coq Messages postés 6349 Date d'inscription samedi 1 juin 2002 Statut Membre Dernière intervention 2 août 2014 101
21 déc. 2004 à 14:46
faudrais peut etre lire les docs au lieu de taper du code au pif...

tu ajoutes au combo le nom du FontFamily
et en lisant la doc c'est justement ce qui pourrait servir pour instancier Font
donc :
this.textBox1.Font=new Font((string)this.comboBox1.SelectedItem, this.textBox1.Font.SizeInPoints);


Cocoricoooooooo !!!!
coq
MVP Visual C#
3
cs_coq Messages postés 6349 Date d'inscription samedi 1 juin 2002 Statut Membre Dernière intervention 2 août 2014 101
21 déc. 2004 à 14:31
TextBox.Font => instance de la classe Font
ComboBox.SelectedIndex => int

en résumé : tu dois créer une instance de la classe Font suivant la sélection dans le ComboBox
this.textBox1.Font = new Font(...)

Cocoricoooooooo !!!!
coq
MVP Visual C#
0
penchu Messages postés 167 Date d'inscription mercredi 11 février 2004 Statut Membre Dernière intervention 5 janvier 2006
21 déc. 2004 à 14:36
ca?
this.textBox1.Font=new Font(this.comboBox1.SelectedIndex);

et bien ca marche pas

voici mon code en fait (ce n'est qu'un test, c'est pour intégrer ca dans un autre programme)

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Text;

namespace WindowsApplication14
{
/// <summary>
/// Description résumée de Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.ComboBox comboBox1;
/// <summary>
/// Variable nécessaire au concepteur.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Requis pour la prise en charge du Concepteur Windows Forms
//
InitializeComponent();
InstalledFontCollection fonts = new System.Drawing.Text.InstalledFontCollection();
FontFamily [] fontfamille = fonts.Families;
for (long i = fontfamille.GetLowerBound(0);i<=fontfamille.GetUpperBound(0);i++)
{
this.comboBox1.Items.Add(fontfamille[i].Name);
}

fontfamille = null;
fonts = null;
this.comboBox1.SelectedIndex = 0;

//
// TODO : ajoutez le code du constructeur après l'appel à InitializeComponent
//
}

/// <summary>
/// Nettoyage des ressources utilisées.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Code généré par le Concepteur Windows Form
/// <summary>
/// Méthode requise pour la prise en charge du concepteur - ne modifiez pas
/// le contenu de cette méthode avec l'éditeur de code.
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(160, 120);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
//
// comboBox1
//
this.comboBox1.Location = new System.Drawing.Point(24, 120);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 21);
this.comboBox1.TabIndex = 1;
this.comboBox1.Text = "comboBox1";
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// Point d'entrée principal de l'application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
this.textBox1.Font=new Font(this.comboBox1.SelectedIndex);
}
}
}

www.penchu.dyndns.org (si mon serveur est allumé)
0
penchu Messages postés 167 Date d'inscription mercredi 11 février 2004 Statut Membre Dernière intervention 5 janvier 2006
21 déc. 2004 à 15:11
ca marche nickel chrome

ps: je tape un peu au pif parce que g toujours eu du mal avec les combobox... (dans n'importe quel langage)

:-(

en tout cas un grand merci à toi

www.penchu.dyndns.org (si mon serveur est allumé)
0
Rejoignez-nous