Бөлісу құралы:


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 элемент управления с трехмерной границей и изображением, отображаемым с помощью ImageList и ImageIndex свойства. Элемент управления также содержит заголовок с заданным символом mnemonic. В примере кода используются PreferredHeight свойства и PreferredWidth свойства для правильного 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 пикселей, может отображаться как три строки в Label 100 пикселей. Свойство PreferredWidth по-прежнему возвращает 300 пикселей. Это свойство можно использовать вместе со свойством PreferredHeight , чтобы убедиться, что текст в элементе Label управления отображается правильно. Свойство можно использовать AutoSize для автоматической Label настройки высоты и ширины элемента управления на основе размера текста и шрифта.

Замечание

BorderStyle Если для свойства Label элемента управления задано BorderStyle.Noneзначение, возвращаемое PreferredWidth свойством, будет больше из-за отсутствия границ.

Применяется к

См. также раздел