次の方法で共有


Label.PreferredHeight プロパティ

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

Public Overridable ReadOnly Property PreferredHeight As Integer
[C#]
public virtual int PreferredHeight {get;}
[C++]
public: __property virtual int get_PreferredHeight();
[JScript]
public function get PreferredHeight() : int;

プロパティ値

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

解説

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

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

使用例

3 次元の輪郭と、 ImageList プロパティおよび ImageIndex プロパティを使用して表示するイメージを持つ Label コントロールの作成方法の例を次に示します。このコントロールには、ニーモニック文字を指定したキャプションも表示されます。このコードの例では、 PreferredHeight プロパティと PreferredWidth プロパティを使用して、表示するフォーム上の Label コントロールのサイズを適切に設定します。また、この例では、 imageList1 という名前の ImageList が作成され、2 つのイメージが読み込まれていることを前提にしています。このコードがフォーム内にあり、そのフォームのコードに System.Drawing 名前空間が追加されていることも前提にしています。

 
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


[C#] 
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...
}


[C++] 
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 = S"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...
 }
 

[JScript] 
public function CreateMyLabel()
{
   // Create an instance of a Label.
   var label1 : Label = 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 System.Drawing.Size (label1.PreferredWidth, label1.PreferredHeight);

   //...Code to add the control to the form...
}

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

Label クラス | Label メンバ | System.Windows.Forms 名前空間 | PreferredWidth | AutoSize