更新 : 2007 年 11 月
名前空間 :
System.Windows.Forms アセンブリ :
System.Windows.Forms (System.Windows.Forms.dll 内)
<ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _
<DefaultBindingPropertyAttribute("Text")> _
<ComVisibleAttribute(True)> _
Public Class Label _
Inherits Control
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)]
[DefaultBindingPropertyAttribute("Text")]
[ComVisibleAttribute(true)]
public class Label : Control
[ClassInterfaceAttribute(ClassInterfaceType::AutoDispatch)]
[DefaultBindingPropertyAttribute(L"Text")]
[ComVisibleAttribute(true)]
public ref class Label : public Control
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) */
/** @attribute DefaultBindingPropertyAttribute("Text") */
/** @attribute ComVisibleAttribute(true) */
public class Label extends Control
public class Label extends Control
通常、Label コントロールは、コントロールについて説明するテキストを表示するために使用されます。たとえば、Label を使用して TextBox コントロールを説明するテキストを追加し、ユーザーがそのコントロールに入力できるデータの型を示すことができます。Label コントロールは、Form に説明テキストを追加し、ユーザーに有用な情報を提供するためにも使用されます。たとえば、Form の上部に Label を追加し、フォーム上のコントロールにデータを入力する方法をユーザーに説明できます。アプリケーションのステータスの実行時情報を表示するためにも Label コントロールを使用できます。たとえば、フォームに Label コントロールを追加し、一連のファイルの処理中に、各ファイルのステータスを表示できます。
Label はフォーム上のタブ オーダーには含まれますが、フォーカスは受け取りません。タブ オーダーの次のコントロールがフォーカスを受け取ります。たとえば、UseMnemonic プロパティが true に設定され、コントロールの Text プロパティにニーモニック文字 (アンパサンド (&) の直後の文字) が指定されている場合、ユーザーが Alt キーを押しながらそのニーモニック キーを押すと、フォーカスはタブ オーダーの次のコントロールに移動します。これにより、キーボードを使用してフォーム上でフォーカスを移動できます。Label コントロールは、テキストを表示するだけではなく、Image プロパティを使用するか、ImageIndex プロパティと ImageList プロパティを組み合わせて使用することによって、イメージも表示できます。
メモ : |
|---|
Label の BackColor プロパティを Color.Transparent に設定すると、ラベルを透明にすることができます。透明のラベルを使用する場合は、現在のデバイス座標系だけを使用してコンテナ上に描画します。他のデバイス座標系を使用すると、Label の後ろにあるコントロールが適切に描画されない場合があります。 |
境界線が 3 次元で、イメージを表示する Label コントロールの作成方法を次のコード例に示します。イメージは ImageList プロパティと ImageIndex プロパティを使用して表示されます。このコントロールには、ニーモニック文字を指定したキャプションも表示されます。このコードの例では、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
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:
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.set_BorderStyle(System.Windows.Forms.BorderStyle.Fixed3D);
// Set the ImageList to use for displaying an image.
label1.set_ImageList(imageList1);
// Use the second image in imageList1.
label1.set_ImageIndex(1);
// Align the image to the top left corner.
label1.set_ImageAlign(ContentAlignment.TopLeft);
// Specify that the text can display mnemonic characters.
label1.set_UseMnemonic(true);
// Set the text of the control and specify a mnemonic character.
label1.set_Text("First &Name:");
/* Set the size of the control based on the PreferredHeight
and PreferredWidth values. */
label1.set_Size(new Size(label1.get_PreferredWidth(),
label1.get_PreferredHeight()));
//...Code to add the control to the form...
} //CreateMyLabel
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...
}
System..::.Object
System..::.MarshalByRefObject
System.ComponentModel..::.Component
System.Windows.Forms..::.Control
System.Windows.Forms..::.Label
System.Windows.Forms..::.LinkLabel
この型のすべてのパブリック static (Visual Basic では Shared) メンバは、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC
.NET Framework および .NET Compact Framework では、各プラットフォームのすべてのバージョンはサポートしていません。サポートされているバージョンについては、「.NET Framework システム要件」を参照してください。
.NET Framework
サポート対象 : 3.5、3.0、2.0、1.1、1.0
.NET Compact Framework
サポート対象 : 3.5、2.0、1.0
参照