Label.PreferredWidth Vlastnost

Definice

Získá upřednostňovanou šířku ovládacího prvku.

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

Hodnota vlastnosti

Int32

Šířka ovládacího prvku (v pixelech) za předpokladu, že se zobrazí jeden řádek textu.

Atributy

Příklady

Následující příklad kódu ukazuje, jak vytvořit Label ovládací prvek, který má trojrozměrné ohraničení a obrázek zobrazený pomocí ImageList vlastností a ImageIndex vlastností. Ovládací prvek má také titulek se zadaným měmónovým znakem. Ukázkový kód používá PreferredHeight vlastnosti a PreferredWidth správné velikosti Label ovládacího prvku ve formuláři, na kterém je zobrazen. Tento příklad vyžaduje vytvoření ImageList a pojmenování imageList1 a načtení dvou imagí. Příklad také vyžaduje, aby kód byl ve formuláři, který má System.Drawing obor názvů přidaný do jeho kódu.

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

Poznámky

Tato vlastnost vrátí délku textového řetězce, ale nebere v úvahu zalamování řádků. Například textový řetězec, který měří šířku 300 pixelů, se může zobrazit jako tři řádky v Label šířce pouze 100 pixelů. Vlastnost PreferredWidth stále vrací 300 pixelů. Tuto vlastnost můžete použít spolu s vlastností, abyste měli jistotu PreferredHeight , že se text v ovládacím Label prvku zobrazí správně. Pomocí AutoSize vlastnosti můžete automaticky upravit výšku a šířku Label ovládacího prvku na základě textu a velikosti písma.

Poznámka

BorderStyle Pokud je vlastnost Label ovládacího prvku nastavena na BorderStyle.None, hodnota vrácená PreferredWidth vlastností bude větší kvůli nedostatku ohraničení.

Platí pro

Viz také