Label.PreferredHeight Propiedad

Definición

Obtiene el alto preferido del control.

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

Valor de propiedad

Int32

Alto del control, en píxeles, suponiendo que se muestra una sola línea de texto.

Atributos

Ejemplos

En el ejemplo de código siguiente se muestra cómo crear un Label control que tenga un borde tridimensional y una imagen mostrada mediante las ImageList propiedades y ImageIndex . El control también tiene un título con un carácter mnemónico especificado. El código de ejemplo usa las PreferredHeight propiedades y PreferredWidth para ajustar correctamente el tamaño del Label control en el formulario en el que se muestra. En este ejemplo se requiere que ImageList se haya creado y denominado imageList1 y que haya cargado dos imágenes. El ejemplo también requiere que el código esté dentro de un formulario que tenga el System.Drawing espacio de nombres agregado a su código.

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

Comentarios

Esta propiedad devuelve el alto que debe tener el control para mostrar correctamente el texto, en función de la fuente asignada al control. Puede usar esta propiedad junto con la PreferredWidth propiedad para asegurarse de que el texto del Label control se muestra correctamente. Puede usar la AutoSize propiedad para ajustar automáticamente el alto y el ancho del Label control, en función del texto y el tamaño de fuente.

Nota

Si la BorderStyle propiedad del Label control se establece BorderStyle.Noneen , el valor devuelto por PreferredHeight la propiedad será mayor debido a la falta de bordes.

Se aplica a

Consulte también