Label.PreferredHeight Label.PreferredHeight Label.PreferredHeight Label.PreferredHeight Property

定义

获取控件的首选高度。Gets the preferred height of the control.

public:
 virtual property int PreferredHeight { int get(); };
[System.ComponentModel.Browsable(false)]
public virtual int PreferredHeight { get; }
member this.PreferredHeight : int
Public Overridable ReadOnly Property PreferredHeight As Integer

属性值

控件的高度(以像素为单位),假定显示单行文本。The height of the control (in pixels), assuming a single line of text is displayed.

示例

下面的代码示例演示如何创建一个Label控件, 该控件具有三维边框和ImageList使用和ImageIndex属性显示的图像。The following code example demonstrates how to create a Label control that has a three-dimensional border and an image displayed using the ImageList and ImageIndex properties. 该控件还具有一个标题, 其中指定了助记符。The control also has a caption with a mnemonic character specified. 示例代码使用PreferredHeightPreferredWidth属性在显示控件的窗体Label上正确调整控件的大小。The example code uses the PreferredHeight and PreferredWidth properties to properly size the Label control on the form on which it is displayed. 此示例要求ImageList已创建并命名为 imageList1, 并且已加载了两个图像。This example requires that an ImageList has been created and named imageList1 and that it has loaded two images. 该示例还要求代码位于将System.Drawing命名空间添加到其代码中的窗体中。The example also requires that the code is within a form that has the System.Drawing namespace added to its code.

public:
   void CreateMyLabel()
   {
      // Create an instance of a Label.
      Label^ label1 = gcnew Label;
      
      // Set the border to a three-dimensional border.
      label1->BorderStyle = System::Windows::Forms::BorderStyle::Fixed3D;
      // Set the ImageList to use for displaying an image.
      label1->ImageList = imageList1;
      // Use the second image in imageList1.
      label1->ImageIndex = 1;
      // Align the image to the top left corner.
      label1->ImageAlign = ContentAlignment::TopLeft;
      
      // Specify that the text can display mnemonic characters.
      label1->UseMnemonic = true;
      // Set the text of the control and specify a mnemonic character.
      label1->Text = "First &Name:";
      
      /* Set the size of the control based on the PreferredHeight and PreferredWidth values. */
      label1->Size = System::Drawing::Size( label1->PreferredWidth, label1->PreferredHeight );
      
      //...Code to add the control to the form...
   }
public void CreateMyLabel()
{
   // Create an instance of a Label.
   Label label1 = new Label();

   // Set the border to a three-dimensional border.
   label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
   // Set the ImageList to use for displaying an image.
   label1.ImageList = imageList1;
   // Use the second image in imageList1.
   label1.ImageIndex = 1;
   // Align the image to the top left corner.
   label1.ImageAlign = ContentAlignment.TopLeft;

   // Specify that the text can display mnemonic characters.
   label1.UseMnemonic = true;
   // Set the text of the control and specify a mnemonic character.
   label1.Text = "First &Name:";
   
   /* Set the size of the control based on the PreferredHeight and PreferredWidth values. */
   label1.Size = new Size (label1.PreferredWidth, label1.PreferredHeight);

   //...Code to add the control to the form...
}

Public Sub CreateMyLabel()
    ' Create an instance of a Label.
    Dim label1 As New Label()
       
    ' Set the border to a three-dimensional border.
    label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
    ' Set the ImageList to use for displaying an image.
    label1.ImageList = imageList1
    ' Use the second image in imageList1.
    label1.ImageIndex = 1
    ' Align the image to the top left corner.
    label1.ImageAlign = ContentAlignment.TopLeft
     
    ' Specify that the text can display mnemonic characters.
    label1.UseMnemonic = True
    ' Set the text of the control and specify a mnemonic character.
    label1.Text = "First &Name:"
       
    ' Set the size of the control based on the PreferredHeight and PreferredWidth values. 
    label1.Size = New Size(label1.PreferredWidth, label1.PreferredHeight)

    '...Code to add the control to the form...
End Sub

注解

此属性根据分配给控件的字体返回控件应为正确显示文本的高度。This property returns the height that the control should be in order to properly display text, based on the font assigned to the control. 可以将此属性与PreferredWidth属性一起使用, 以确保正确显示Label控件中的文本。You can use this property along with the PreferredWidth property to ensure that the text in the Label control is displayed properly. 您可以使用AutoSize属性根据文本和字号自动调整Label控件的高度和宽度。You can use the AutoSize property to automatically adjust the height and the width of the Label control, based on the text and font size.

备注

如果将BorderStyle Label控件的属性设置为BorderStyle.None, 则由于缺少边框, PreferredHeight属性返回的值将更大。If the BorderStyle property of the Label control is set to BorderStyle.None, the value returned by PreferredHeight property will be larger due to the lack of borders.

适用于

另请参阅