Label.PreferredWidth 屬性

定義

取得控制項的慣用寬度。Gets the preferred width of the control.

public:
 virtual property int PreferredWidth { int get(); };
[System.ComponentModel.Browsable(false)]
public virtual int PreferredWidth { get; }
member this.PreferredWidth : int
Public Overridable ReadOnly Property PreferredWidth As Integer

屬性值

控制項的寬度 (單位為像素),假設所顯示的是單行文字。The width of the control (in pixels), assuming a single line of text is displayed.

屬性

範例

下列程式碼範例示範如何建立具有三維框線的 Label 控制項,以及使用 ImageListImageIndex 屬性顯示的影像。The following code example demonstrates how to create a Label control that has a three-dimensional border and an image displayed using the ImageList and ImageIndex properties. 控制項也有一個標題,其中指定了助憶鍵字元。The control also has a caption with a mnemonic character specified. 範例程式碼會使用 PreferredHeightPreferredWidth 屬性,適當地將 Label 控制項的大小調整為其顯示所在的表單。The example code uses the PreferredHeight and PreferredWidth properties to properly size the Label control on the form on which it is displayed. 這個範例需要已建立 ImageList 並將其命名為 imageList1,且已載入兩個映射。This example requires that an ImageList has been created and named imageList1 and that it has loaded two images. 此範例也要求程式碼必須在已將 System.Drawing 命名空間加入其程式碼的表單中。The example also requires that the code is within a form that has the System.Drawing namespace added to its code.

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

備註

這個屬性會傳回文字字串的長度,但不會將分行符號換成考慮。This property returns the length of the text string, but does not take line wrapping into consideration. 例如,測量300圖元寬的文字字串可以在僅100圖元寬的 Label 中顯示為三行。For example, a text string that measures 300 pixels wide could be displayed as three lines in a Label that is only 100 pixels wide. PreferredWidth 屬性仍會傳回300圖元。The PreferredWidth property still returns 300 pixels. 您可以使用這個屬性以及 PreferredHeight 屬性,以確保 Label 控制項中的文字會正確顯示。You can use this property, along with the PreferredHeight property, to ensure that the text in the Label control is displayed properly. 您可以使用 [AutoSize] 屬性,根據文字和字型大小,自動調整 Label 控制項的高度和寬度。You can use the AutoSize property to automatically adjust the height and the width of the Label control based on the text and font size.

注意

如果 Label 控制項的 BorderStyle 屬性設定為 BorderStyle.NonePreferredWidth 屬性所傳回的值將會因為缺少框線而變大。If the BorderStyle property of the Label control is set to BorderStyle.None, the value returned by PreferredWidth property will be larger due to the lack of borders.

適用於

另請參閱