Hinweis
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, sich anzumelden oder das Verzeichnis zu wechseln.
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, das Verzeichnis zu wechseln.
Ruft die bevorzugte Breite des Steuerelements ab.
Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)
Syntax
'Declaration
Public Overridable ReadOnly Property PreferredWidth As Integer
'Usage
Dim instance As Label
Dim value As Integer
value = instance.PreferredWidth
public virtual int PreferredWidth { get; }
public:
virtual property int PreferredWidth {
int get ();
}
/** @property */
public int get_PreferredWidth ()
public function get PreferredWidth () : int
Eigenschaftenwert
Die Breite des Steuerelements (in Pixel) unter der Voraussetzung, dass eine Textzeile angezeigt wird.
Hinweise
Diese Eigenschaft gibt die Länge der Textzeichenfolge zurück, berücksichtigt jedoch keine Zeilenumbrüche. Eine 300 Pixel lange Textzeichenfolge könnte z. B. in einem nur 100 Pixel breiten Label in 3 Zeilen dargestellt werden. Die PreferredWidth-Eigenschaft gibt dennoch 300 Pixel zurück. Diese Eigenschaft können Sie zusammen mit der PreferredHeight-Eigenschaft verwenden, um sicherzustellen, dass der Text im Label-Steuerelement richtig dargestellt 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 PreferredWidth-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
Label.PreferredHeight-Eigenschaft
Label.AutoSize-Eigenschaft