This documentation is archived and is not being maintained.

TextBoxBase.PreferredHeight Property

Gets the preferred height for a single-line text box.

[Visual Basic]
Public ReadOnly Property PreferredHeight As Integer
[C#]
public int PreferredHeight {get;}
[C++]
public: __property int get_PreferredHeight();
[JScript]
public function get PreferredHeight() : int;

Property Value

The preferred height of a single-line text box.

Remarks

The size returned by this property is based on the font height and border style of the text box. You can use this property to determine the appropriate size of the text box to ensure that text is properly displayed in the control. The value returned by this property is in pixels.

Example

[Visual Basic, C#, C++] The following example uses TextBox, a derived class, to create a text box that can properly display text using a 20-point Arial font with a single border. This example uses the PreferredHeight property to determine the appropriate height of the control after the font and BorderStyle have been assigned.

[Visual Basic] 
Public Sub CreateTextBox()
    ' Create an instance of the TextBox control.
    Dim textBox1 As New TextBox()
    ' Set the TextBox Font property to 20-point Arial.
    textBox1.Font = New Font("Arial", 20)
    ' Set the BorderStyle property to FixedSingle.
    textBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
    ' Make the height of the control equal to the preferred height.
    textBox1.Height = textBox1.PreferredHeight
End Sub


[C#] 
public void CreateTextBox()
 {
    // Create an instance of the TextBox control.
    TextBox textBox1 = new TextBox();
    // Set the TextBox Font property to 20-point Arial.
    textBox1.Font = new Font ("Arial" , 20);
    // Set the BorderStyle property to FixedSingle.
    textBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
    // Make the height of the control equal to the preferred height.
    textBox1.Height = textBox1.PreferredHeight;
 }
 

[C++] 
public:
void CreateTextBox()
 {
    // Create an instance of the TextBox control.
    TextBox* textBox1 = new TextBox();
    // Set the TextBox Font property to 20-point Arial.
    textBox1->Font = new System::Drawing::Font (S"Arial", 20);
    // Set the BorderStyle property to FixedSingle.
    textBox1->BorderStyle = System::Windows::Forms::BorderStyle::FixedSingle;
    // Make the height of the control equal to the preferred height.
    textBox1->Height = textBox1->PreferredHeight;
 }
 

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

TextBoxBase Class | TextBoxBase Members | System.Windows.Forms Namespace | AutoSize

Show: