Label.PreferredWidth Eigenschaft
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Ruft die bevorzugte Breite des Steuerelements ab.
public:
virtual property int PreferredWidth { int get(); };
[System.ComponentModel.Browsable(false)]
public virtual int PreferredWidth { get; }
[<System.ComponentModel.Browsable(false)>]
member this.PreferredWidth : int
Public Overridable ReadOnly Property PreferredWidth As Integer
Eigenschaftswert
Die Breite des Steuerelements (in Pixel), vorausgesetzt, eine einzelne Textzeile wird angezeigt.
- Attribute
Beispiele
Im folgenden Codebeispiel wird das Erstellen eines Steuerelements mit einem Label dreidimensionalen Rahmen und einem Bild veranschaulicht, das mit den ImageList Eigenschaften ImageIndex angezeigt wird. Das Steuerelement verfügt auch über eine Beschriftung mit einem angegebenen Mnemonic-Zeichen. Der Beispielcode verwendet die Eigenschaften und PreferredWidth die PreferredHeight richtige Größe des Label Steuerelements im Formular, auf dem es angezeigt wird. In diesem Beispiel ist erforderlich, dass ein ImageList ImageList1 erstellt und mit dem Namen "imageList1" erstellt wurde und zwei Bilder geladen hat. Das Beispiel erfordert außerdem, dass sich der Code in einem Formular befindet, das dem Code den System.Drawing Namespace hinzugefügt hat.
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
Hinweise
Diese Eigenschaft gibt die Länge der Textzeichenfolge zurück, berücksichtigt jedoch keinen Zeilenumbruch. Beispielsweise könnte eine Textzeichenfolge mit einer Breite von 300 Pixeln als drei Zeilen in einer Label Nur-100-Pixel-Breite angezeigt werden. Die PreferredWidth Eigenschaft gibt weiterhin 300 Pixel zurück. Sie können diese Eigenschaft zusammen mit der PreferredHeight Eigenschaft verwenden, um sicherzustellen, dass der Text im Label Steuerelement ordnungsgemäß angezeigt wird. Sie können die AutoSize Eigenschaft verwenden, um die Höhe und breite des Label Steuerelements basierend auf dem Text und schriftgrad automatisch anzupassen.
Hinweis
Wenn die BorderStyle Eigenschaft des Label Steuerelements auf festgelegt BorderStyle.Noneist, wird der von PreferredWidth der Eigenschaft zurückgegebene Wert aufgrund fehlender Rahmen größer.