TextBoxBase.AutoSize Proprietà
Definizione
Ottiene o imposta un valore che indica se la dimensione del controllo viene modificata automaticamente al variare del tipo di carattere assegnato al controllo.Gets or sets a value indicating whether the height of the control automatically adjusts when the font assigned to the control is changed.
public:
virtual property bool AutoSize { bool get(); void set(bool value); };
[System.ComponentModel.Browsable(false)]
public override bool AutoSize { get; set; }
public virtual bool AutoSize { get; set; }
[<System.ComponentModel.Browsable(false)>]
member this.AutoSize : bool with get, set
member this.AutoSize : bool with get, set
Public Overrides Property AutoSize As Boolean
Public Overridable Property AutoSize As Boolean
Valore della proprietà
true
se l'altezza del controllo cambia automaticamente al variare del tipo di carattere; in caso contrario, false
.true
if the height of the control automatically adjusts when the font is changed; otherwise, false
. Il valore predefinito è true
.The default is true
.
- Attributi
Esempio
Questo esempio presuppone che si disponga di un modulo con due caselle di testo, due pulsanti e fare clic su eventi per ognuno dei pulsanti.This example assumes that you have a form with two text boxes, two buttons, and click events for each of the buttons. Nell'esempio viene illustrata la AutoSize Proprietà impostandola su true
per una casella di testo e false
per l'altra.The example demonstrates the AutoSize property by setting it to true
for one text box and false
for the other. Quando si fa clic su un pulsante, le caselle di testo vengono riempite con un testo più piccolo e quando si fa clic sul pulsante altro le caselle di testo vengono riempite con testo di dimensioni maggiori.When you click one button the text boxes are filled with a smaller text, and when you click the other button the text boxes are filled with larger text. La casella di testo AutoSize impostata per true
espandersi in altezza per contenere il testo più grande.The text box that has AutoSize set to true
expands in height to accommodate the larger text. La larghezza non cambia.The width does not change.
private void button1_Click(object sender, EventArgs e)
{
this.textBox1.AutoSize = true;
this.textBox1.Text = "Hello world!";
this.textBox1.Font = new System.Drawing.Font("Arial", 10, FontStyle.Regular);
this.textBox2.AutoSize = false;
this.textBox2.Text = "Hello world!";
this.textBox2.Font = new System.Drawing.Font("Arial", 10, FontStyle.Regular);
}
private void button2_Click(object sender, EventArgs e)
{
this.textBox1.AutoSize = true;
this.textBox1.Text = "Goodbye world!";
this.textBox1.Font = new System.Drawing.Font("ArialBlack", 14, FontStyle.Regular);
this.textBox2.AutoSize = false;
this.textBox2.Text = "Goodbye world!";
this.textBox2.Font = new System.Drawing.Font("ArialBlack", 14, FontStyle.Regular);
}
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.TextBox1.AutoSize = True
Me.TextBox1.Text = "Hello world!"
Me.TextBox1.Font = New System.Drawing.Font("Arial", 10, FontStyle.Regular)
Me.TextBox2.AutoSize = False
Me.TextBox2.Text = "Hello world!"
Me.TextBox2.Font = New System.Drawing.Font("Arial", 10, FontStyle.Regular)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.TextBox1.AutoSize = True
Me.TextBox1.Text = "Goodbye world!"
Me.TextBox1.Font = New System.Drawing.Font("ArialBlack", 14, FontStyle.Regular)
Me.TextBox2.AutoSize = False
Me.TextBox2.Text = "Goodbye world!"
Me.TextBox2.Font = New System.Drawing.Font("ArialBlack", 14, FontStyle.Regular)
End Sub
Commenti
Quando si imposta la AutoSize proprietà su true
per un oggetto TextBox , quando Font cambia, TextBox espande o contrae Height per contenere il testo più grande o più piccolo.When you set the AutoSize property to true
for a TextBox, when the Font changes, the TextBox expands or contracts the Height to accommodate the larger or smaller text. Il Width di TextBox non cambia.The Width of the TextBox does not change.
Se si desidera modificare le dimensioni del controllo quando l'utente immette testo, è possibile utilizzare un RichTextBox controllo e utilizzare il relativo ContentsResized evento per modificarne le dimensioni.If you want to change the size of the control as the user enters text, you can use a RichTextBox control and use its ContentsResized event to change its size.