Label.PreferredWidth Właściwość

Definicja

Pobiera preferowaną szerokość kontrolki.

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

Wartość właściwości

Szerokość kontrolki (w pikselach) przy założeniu, że wyświetlany jest pojedynczy wiersz tekstu.

Atrybuty

Przykłady

W poniższym przykładzie kodu pokazano, jak utworzyć kontrolkę Label , która ma obramowanie trójwymiarowe i obraz wyświetlany przy użyciu ImageList właściwości i ImageIndex . Kontrolka ma również podpis z określonym znakiem mnemonic. Przykładowy kod używa PreferredHeight właściwości i PreferredWidth do prawidłowego rozmiaru Label kontrolki w formularzu, na którym jest wyświetlany. Ten przykład wymaga ImageList utworzenia i nazwy imageList1 oraz załadowania dwóch obrazów. Przykład wymaga również, aby kod znajduje się w formularzu, który ma System.Drawing przestrzeń nazw dodaną do kodu.

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

Uwagi

Ta właściwość zwraca długość ciągu tekstowego, ale nie uwzględnia zawijania wierszy. Na przykład ciąg tekstowy, który mierzy 300 pikseli szerokości, może być wyświetlany jako trzy wiersze w rozmiarze Label tylko 100 pikseli szerokości. Właściwość PreferredWidth nadal zwraca 300 pikseli. Można użyć tej właściwości wraz z właściwością PreferredHeight , aby upewnić się, że tekst w kontrolce Label jest wyświetlany prawidłowo. Możesz użyć AutoSize właściwości , aby automatycznie dostosować wysokość i szerokość Label kontrolki na podstawie tekstu i rozmiaru czcionki.

Uwaga

BorderStyle Jeśli właściwość kontrolki Label jest ustawiona na BorderStyle.None, wartość zwrócona przez PreferredWidth właściwość będzie większa z powodu braku obramowań.

Dotyczy

Zobacz też