Label.PreferredHeight Proprietà

Definizione

Ottiene l'altezza preferenziale del controllo.

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

Valore della proprietà

Int32

L'altezza del controllo espressa in pixel, supponendo che sia visualizzata una riga di testo singola.

Attributi

Esempio

Nell'esempio di codice seguente viene illustrato come creare un Label controllo con un bordo tridimensionale e un'immagine visualizzata usando le ImageList proprietà e ImageIndex . Il controllo ha anche una didascalia con un carattere mnemonico specificato. Nel codice di esempio vengono utilizzate le PreferredHeight proprietà e PreferredWidth per ridimensionare correttamente il Label controllo nel form in cui viene visualizzato. In questo esempio è necessario che ImageList sia stato creato e denominato imageList1 e che siano state caricate due immagini. L'esempio richiede inoltre che il codice si trova all'interno di un modulo con lo System.Drawing spazio dei nomi aggiunto al codice.

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

Commenti

Questa proprietà restituisce l'altezza che il controllo deve essere per visualizzare correttamente il testo, in base al tipo di carattere assegnato al controllo. È possibile utilizzare questa proprietà insieme alla PreferredWidth proprietà per assicurarsi che il Label testo nel controllo venga visualizzato correttamente. È possibile utilizzare la AutoSize proprietà per regolare automaticamente l'altezza e la larghezza del Label controllo, in base al testo e alle dimensioni del carattere.

Nota

Se la BorderStyle proprietà del Label controllo è impostata su BorderStyle.None, il valore restituito dalla PreferredHeight proprietà sarà maggiore a causa della mancanza di bordi.

Si applica a

Vedi anche