Label.PreferredWidth 속성

정의

이 컨트롤의 기본 설정 너비를 가져옵니다.

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

속성 값

한 줄의 텍스트가 표시되는 것으로 가정하는 컨트롤의 너비(단위: 픽셀)입니다.

특성

예제

다음 코드 예제를 만드는 Label 방법에 설명 합니다 3 차원 테두리와 이미지를 사용 하 여 ImageList 표시 되는 컨트롤을 및 ImageIndex 속성입니다. 또한 컨트롤에는 니모닉 문자가 지정된 캡션이 있습니다. 예제 코드는 및 PreferredWidth 속성을 사용하여 PreferredHeight 컨트롤이 표시되는 폼의 Label 컨트롤 크기를 적절하게 조정합니다. 이 예제에서는 ImageList 을 만들고 imageList1로 명명하고 두 개의 이미지를 로드해야 합니다. 또한 이 예제에서는 코드가 해당 코드에 네임스페이스가 추가된 System.Drawing 양식 내에 있어야 합니다.

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

설명

이 속성은 텍스트 문자열의 길이를 반환하지만 줄 바꿈을 고려하지 않습니다. 예를 들어 너비가 300픽셀인 텍스트 문자열은 너비가 100픽셀인 에 Label 3줄로 표시될 수 있습니다. 속성은 PreferredWidth 여전히 300픽셀을 반환합니다. 속성과 함께 PreferredHeight 이 속성을 사용하여 컨트롤의 텍스트 Label 가 제대로 표시되는지 확인할 수 있습니다. 사용 하 여는 AutoSize 속성을 자동으로 높이 및 텍스트 및 글꼴 크기에 따라 컨트롤의 Label 너비를 조정할 수 있습니다.

참고

컨트롤의 BorderStyleLabel 속성을 로 설정 BorderStyle.None하면 테두리가 없어 속성에서 반환되는 PreferredWidth 값이 더 커집니다.

적용 대상

추가 정보