1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
private void TextBox_TextChanged(object sender, System.EventArgs e)
{
// Création de l'objet Graphics pour la TextBox.
Graphics g = textBox1.CreateGraphics();
// Récupération du texte
int nbLigne = 0;
int hauteurLigne = 0;
foreach (string line in textBox1.Lines)
{
// Mesure du texte
SizeF size = g.MeasureString((string.IsNullOrEmpty(line)) ? " " : line, textBox1.Font);
// Calcul du nombre de lignes prises par le texte.
nbLigne += 1 + (int)(size.Width / textBox1.Width);
}
hauteurLigne = 20;
// Mise à jour de la hauteur de la TextBox
int hauteurTotale = hauteurLigne * nbLigne;
if (hauteurTotale > 20 )
this.textBox1.Height = hauteurTotale;
else
this.textBox1.Height = 20;
} |
Partager