Freigeben über


Label.PreferredHeight-Eigenschaft

Ruft die bevorzugte Höhe des Steuerelements ab.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

Syntax

'Declaration
Public Overridable ReadOnly Property PreferredHeight As Integer
'Usage
Dim instance As Label
Dim value As Integer

value = instance.PreferredHeight
public virtual int PreferredHeight { get; }
public:
virtual property int PreferredHeight {
    int get ();
}
/** @property */
public int get_PreferredHeight ()
public function get PreferredHeight () : int

Eigenschaftenwert

Die Höhe des Steuerelements (in Pixel) unter der Voraussetzung, dass eine Textzeile angezeigt wird.

Hinweise

Diese Eigenschaft gibt die zur richtigen Darstellung von Text erforderliche Höhe des Steuerelements in Abhängigkeit von der dem Steuerelement zugewiesenen Schriftart an. Sie können diese Eigenschaft zusammen mit der PreferredWidth-Eigenschaft verwenden, um sicherzustellen, dass der Text im Label-Steuerelement richtig angezeigt wird. Mithilfe der AutoSize-Eigenschaft werden Höhe und Breite des Label-Steuerelements automatisch auf der Grundlage von Text und Schriftart angepasst.

Hinweis

Wenn die BorderStyle-Eigenschaft des Label-Steuerelements auf BorderStyle.None festgelegt ist, ist der von der PreferredHeight-Eigenschaft zurückgegebene Wert größer, da keine Rahmen vorhanden sind.

Beispiel

Im folgenden Beispiel wird veranschaulicht, wie mithilfe der Eigenschaften ImageList und ImageIndex ein Label-Steuerelement mit einem dreidimensionalen Rahmen und einem Bild erstellt wird. Das Steuerelement verfügt außerdem über eine Beschriftung mit einem angegebenen mnemonischen Zeichen. Im Beispielcode werden die PreferredHeight-Eigenschaft und die PreferredWidth-Eigenschaft dazu verwendet, die Größe des Label-Steuerelements in dem Formular anzupassen, auf dem es angezeigt wird. Für dieses Beispiel muss eine ImageList mit dem Namen imageList1 erstellt werden, in die zwei Bilder geladen werden. Weiterhin ist erforderlich, dass sich der Code in einem Formular befindet, in dessen Code der System.Drawing-Namespace eingebunden ist.

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
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:
   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.set_BorderStyle(System.Windows.Forms.BorderStyle.Fixed3D);
    // Set the ImageList to use for displaying an image.
    label1.set_ImageList(imageList1);
    // Use the second image in imageList1.
    label1.set_ImageIndex(1);
    // Align the image to the top left corner.
    label1.set_ImageAlign(ContentAlignment.TopLeft);
    // Specify that the text can display mnemonic characters.
    label1.set_UseMnemonic(true);
    // Set the text of the control and specify a mnemonic character.
    label1.set_Text("First &Name:");
    /* Set the size of the control based on the PreferredHeight
       and PreferredWidth values. */
    label1.set_Size(new Size(label1.get_PreferredWidth(), 
        label1.get_PreferredHeight()));
    //...Code to add the control to the form...
} //CreateMyLabel 
public function CreateMyLabel()
{
   // Create an instance of a Label.
   var label1 : Label = 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 System.Drawing.Size (label1.PreferredWidth, label1.PreferredHeight);

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

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

Siehe auch

Referenz

Label-Klasse
Label-Member
System.Windows.Forms-Namespace
PreferredWidth
Label.AutoSize-Eigenschaft