Label.PreferredWidth Propiedad

Definición

Obtiene el ancho preferido del control.

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

Valor de propiedad

Ancho del control, en píxeles, suponiendo que se muestra una sola línea de texto.

Atributos

Ejemplos

En el ejemplo de código siguiente se muestra cómo crear un Label control que tenga un borde tridimensional y una imagen mostrada mediante las ImageList propiedades y ImageIndex . El control también tiene un título con un carácter mnemónico especificado. El código de ejemplo usa las PreferredHeight propiedades y PreferredWidth para ajustar correctamente el tamaño del Label control en el formulario en el que se muestra. En este ejemplo se requiere que ImageList se haya creado y denominado imageList1 y que haya cargado dos imágenes. El ejemplo también requiere que el código esté dentro de un formulario que tenga el System.Drawing espacio de nombres agregado a su código.

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

Comentarios

Esta propiedad devuelve la longitud de la cadena de texto, pero no tiene en cuenta el ajuste de línea. Por ejemplo, una cadena de texto que mide 300 píxeles de ancho podría mostrarse como tres líneas en un Label que solo tiene 100 píxeles de ancho. La PreferredWidth propiedad sigue devuelve 300 píxeles. Puede usar esta propiedad, junto con la PreferredHeight propiedad , para asegurarse de que el texto del Label control se muestra correctamente. Puede usar la AutoSize propiedad para ajustar automáticamente el alto y el ancho del Label control en función del texto y el tamaño de fuente.

Nota:

Si la BorderStyle propiedad del Label control se establece BorderStyle.Noneen , el valor devuelto por PreferredWidth la propiedad será mayor debido a la falta de bordes.

Se aplica a

Consulte también