Label.PreferredHeight プロパティ

定義

コントロールの適切な高さを取得します。

public:
 virtual property int PreferredHeight { int get(); };
[System.ComponentModel.Browsable(false)]
public virtual int PreferredHeight { get; }
[<System.ComponentModel.Browsable(false)>]
member this.PreferredHeight : int
Public Overridable ReadOnly Property PreferredHeight As Integer

プロパティ値

Int32

単一行のテキストが表示されることを想定したコントロールの高さ (ピクセル単位)。

属性

次のコード例では、3 次元境界線と、プロパティを使用して表示されるイメージを持つコントロールを作成Labelする方法をImageListImageIndex示します。 コントロールには、ニーモニック文字が指定されたキャプションもあります。 このコード例では、コントロールとPreferredWidthプロパティをPreferredHeight使用して、表示されるフォームのコントロールのサイズLabelを適切に設定します。 この例では、 ImageList imageList1 という名前が作成され、2 つのイメージが読み込まれている必要があります。 また、この例では、コードに名前空間が追加されたフォーム内にコードが含まれている 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

注釈

このプロパティは、コントロールに割り当てられたフォントに基づいて、テキストを適切に表示するためにコントロールが必要な高さを返します。 このプロパティをプロパティと共に PreferredWidth 使用して、コントロール内 Label のテキストが正しく表示されるようにすることができます。 このプロパティを AutoSize 使用すると、テキストとフォント サイズに基づいて、コントロールの Label 高さと幅を自動的に調整できます。

注意

コントロールのLabelプロパティがBorderStyle設定BorderStyle.Noneされている場合、境界線がないため、プロパティによってPreferredHeight返される値が大きくなります。

適用対象

こちらもご覧ください