Label.PreferredWidth Propriedade
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Obtém a largura preferida do controlo.
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 Propriedade
A largura do controlo (em pixels), assumindo que uma única linha de texto é apresentada.
- Atributos
Exemplos
O exemplo de código seguinte demonstra como criar um Label controlo que tem uma borda tridimensional e uma imagem exibida usando as ImageList propriedades e.ImageIndex O controlo também tem uma legenda com uma personagem mnemónica especificada. O código de exemplo utiliza as PreferredHeight propriedades e PreferredWidth para dimensionar corretamente o Label controlo no formulário onde é exibido. Este exemplo exige que um ImageList tenha sido criado e nomeado imageList1 e que tenha carregado duas imagens. O exemplo também exige que o código esteja dentro de um formulário cujo System.Drawing espaço de nomes é adicionado ao seu 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
Observações
Esta propriedade devolve o comprimento da cadeia de texto, mas não tem em conta o enrolamento de linhas. Por exemplo, uma cadeia de texto com 300 pixels de largura pode ser apresentada como três linhas num Label que tem apenas 100 pixels de largura. A PreferredWidth propriedade ainda devolve 300 píxeis. Pode usar esta propriedade, juntamente com a propriedade PreferredHeight , para garantir que o texto no Label controlo é exibido corretamente. Podes usar a AutoSize propriedade para ajustar automaticamente a altura e a largura do Label controlo com base no texto e no tamanho da fonte.
Note
Se a BorderStyle propriedade do Label controlo for definida para BorderStyle.None, o valor devolvido pela PreferredWidth propriedade será maior devido à ausência de bordas.