Share via


Label.PreferredHeight Propriedade

Definição

Obtém a altura preferencial do controle.

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 da propriedade

Int32

A altura do controle (em pixels), supondo que uma única linha de texto é exibida.

Atributos

Exemplos

O exemplo de código a seguir demonstra como criar um Label controle que tenha uma borda tridimensional e uma imagem exibida usando as propriedades eImageIndex.ImageList O controle também tem uma legenda com um caractere mnemônico especificado. O código de exemplo usa as PreferredHeight propriedades e PreferredWidth para dimensionar corretamente o Label controle no formulário no qual ele é exibido. Este exemplo requer que um ImageList imageList1 foi criado e nomeado e que tenha carregado duas imagens. O exemplo também exige que o código esteja dentro de um formulário que tenha o System.Drawing namespace adicionado ao 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

Comentários

Essa propriedade retorna a altura em que o controle deve ser para exibir corretamente o texto, com base na fonte atribuída ao controle. Você pode usar essa propriedade junto com a PreferredWidth propriedade para garantir que o texto no Label controle seja exibido corretamente. Você pode usar a AutoSize propriedade para ajustar automaticamente a altura e a largura do Label controle, com base no tamanho do texto e da fonte.

Observação

Se a BorderStyle propriedade do Label controle for definida como BorderStyle.None, o valor retornado pela PreferredHeight propriedade será maior devido à falta de bordas.

Aplica-se a

Confira também