次の方法で共有


ListView.View プロパティ

定義

コントロールに項目を表示する方法を取得または設定します。

public:
 property System::Windows::Forms::View View { System::Windows::Forms::View get(); void set(System::Windows::Forms::View value); };
public System.Windows.Forms.View View { get; set; }
member this.View : System.Windows.Forms.View with get, set
Public Property View As View

プロパティ値

View値の 1 つ。 既定値は、LargeIcon です。

例外

指定された値は、 View 値の 1 つではありません。

次のコード例では、3 つのListViewItem オブジェクトを指定し、項目ごとに 3 つのListViewItem.ListViewSubItem オブジェクトを指定して、ListView コントロールを作成します。 この例では、詳細ビューにサブ項目を表示する ColumnHeader オブジェクトも作成します。 コード例では、ListViewItem オブジェクトのイメージを提供するために、2 つのImageList オブジェクトも作成されます。 これらの ImageList オブジェクトは、 LargeImageList プロパティと SmallImageList プロパティに追加されます。 この例では、 ListView コントロールの作成に次のプロパティを使用します。

この例では、コードを Form に追加し、この例で作成したメソッドをコンストラクターまたはフォーム上の別のメソッドから呼び出す必要があります。 この例では、 MySmallImage1MySmallImage2MyLargeImage1MyLargeImage2 という名前のイメージがドライブ C のルート ディレクトリに配置されている必要もあります。

private:
   void CreateMyListView()
   {
      // Create a new ListView control.
      ListView^ listView1 = gcnew ListView;
      listView1->Bounds = Rectangle(Point(10,10),System::Drawing::Size( 300, 200 ));

      // Set the view to show details.
      listView1->View = View::Details;

      // Allow the user to edit item text.
      listView1->LabelEdit = true;

      // Allow the user to rearrange columns.
      listView1->AllowColumnReorder = true;

      // Display check boxes.
      listView1->CheckBoxes = true;

      // Select the item and subitems when selection is made.
      listView1->FullRowSelect = true;

      // Display grid lines.
      listView1->GridLines = true;

      // Sort the items in the list in ascending order.
      listView1->Sorting = SortOrder::Ascending;

      // Create three items and three sets of subitems for each item.
      ListViewItem^ item1 = gcnew ListViewItem( "item1",0 );

      // Place a check mark next to the item.
      item1->Checked = true;
      item1->SubItems->Add( "1" );
      item1->SubItems->Add( "2" );
      item1->SubItems->Add( "3" );
      ListViewItem^ item2 = gcnew ListViewItem( "item2",1 );
      item2->SubItems->Add( "4" );
      item2->SubItems->Add( "5" );
      item2->SubItems->Add( "6" );
      ListViewItem^ item3 = gcnew ListViewItem( "item3",0 );

      // Place a check mark next to the item.
      item3->Checked = true;
      item3->SubItems->Add( "7" );
      item3->SubItems->Add( "8" );
      item3->SubItems->Add( "9" );

      // Create columns for the items and subitems.
      // Width of -2 indicates auto-size.
      listView1->Columns->Add( "Item Column", -2, HorizontalAlignment::Left );
      listView1->Columns->Add( "Column 2", -2, HorizontalAlignment::Left );
      listView1->Columns->Add( "Column 3", -2, HorizontalAlignment::Left );
      listView1->Columns->Add( "Column 4", -2, HorizontalAlignment::Center );

      //Add the items to the ListView.
      array<ListViewItem^>^temp1 = {item1,item2,item3};
      listView1->Items->AddRange( temp1 );

      // Create two ImageList objects.
      ImageList^ imageListSmall = gcnew ImageList;
      ImageList^ imageListLarge = gcnew ImageList;

      // Initialize the ImageList objects with bitmaps.
      imageListSmall->Images->Add( Bitmap::FromFile( "C:\\MySmallImage1.bmp" ) );
      imageListSmall->Images->Add( Bitmap::FromFile( "C:\\MySmallImage2.bmp" ) );
      imageListLarge->Images->Add( Bitmap::FromFile( "C:\\MyLargeImage1.bmp" ) );
      imageListLarge->Images->Add( Bitmap::FromFile( "C:\\MyLargeImage2.bmp" ) );

      //Assign the ImageList objects to the ListView.
      listView1->LargeImageList = imageListLarge;
      listView1->SmallImageList = imageListSmall;
      
      // Add the ListView to the control collection.
      this->Controls->Add( listView1 );
   }
private void CreateMyListView()
{
    // Create a new ListView control.
    ListView listView1 = new ListView();
    listView1.Bounds = new Rectangle(new Point(10,10), new Size(300,200));

    // Set the view to show details.
    listView1.View = View.Details;
    // Allow the user to edit item text.
    listView1.LabelEdit = true;
    // Allow the user to rearrange columns.
    listView1.AllowColumnReorder = true;
    // Display check boxes.
    listView1.CheckBoxes = true;
    // Select the item and subitems when selection is made.
    listView1.FullRowSelect = true;
    // Display grid lines.
    listView1.GridLines = true;
    // Sort the items in the list in ascending order.
    listView1.Sorting = SortOrder.Ascending;
                
    // Create three items and three sets of subitems for each item.
    ListViewItem item1 = new ListViewItem("item1",0);
    // Place a check mark next to the item.
    item1.Checked = true;
    item1.SubItems.Add("1");
    item1.SubItems.Add("2");
    item1.SubItems.Add("3");
    ListViewItem item2 = new ListViewItem("item2",1);
    item2.SubItems.Add("4");
    item2.SubItems.Add("5");
    item2.SubItems.Add("6");
    ListViewItem item3 = new ListViewItem("item3",0);
    // Place a check mark next to the item.
    item3.Checked = true;
    item3.SubItems.Add("7");
    item3.SubItems.Add("8");
    item3.SubItems.Add("9");

    // Create columns for the items and subitems.
    // Width of -2 indicates auto-size.
    listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left);
    listView1.Columns.Add("Column 2", -2, HorizontalAlignment.Left);
    listView1.Columns.Add("Column 3", -2, HorizontalAlignment.Left);
    listView1.Columns.Add("Column 4", -2, HorizontalAlignment.Center);

    //Add the items to the ListView.
    listView1.Items.AddRange(new ListViewItem[]{item1,item2,item3});

    // Create two ImageList objects.
    ImageList imageListSmall = new ImageList();
    ImageList imageListLarge = new ImageList();

    // Initialize the ImageList objects with bitmaps.
    imageListSmall.Images.Add(Bitmap.FromFile("C:\\MySmallImage1.bmp"));
    imageListSmall.Images.Add(Bitmap.FromFile("C:\\MySmallImage2.bmp"));
    imageListLarge.Images.Add(Bitmap.FromFile("C:\\MyLargeImage1.bmp"));
    imageListLarge.Images.Add(Bitmap.FromFile("C:\\MyLargeImage2.bmp"));

    //Assign the ImageList objects to the ListView.
    listView1.LargeImageList = imageListLarge;
    listView1.SmallImageList = imageListSmall;

    // Add the ListView to the control collection.
    this.Controls.Add(listView1);
}
Private Sub CreateMyListView()
    ' Create a new ListView control.
    Dim listView1 As New ListView()
    listView1.Bounds = New Rectangle(New Point(10, 10), New Size(300, 200))

    ' Set the view to show details.
    listView1.View = View.Details
    ' Allow the user to edit item text.
    listView1.LabelEdit = True
    ' Allow the user to rearrange columns.
    listView1.AllowColumnReorder = True
    ' Display check boxes.
    listView1.CheckBoxes = True
    ' Select the item and subitems when selection is made.
    listView1.FullRowSelect = True
    ' Display grid lines.
    listView1.GridLines = True
    ' Sort the items in the list in ascending order.
    listView1.Sorting = SortOrder.Ascending

    ' Create three items and three sets of subitems for each item.
    Dim item1 As New ListViewItem("item1", 0)
    ' Place a check mark next to the item.
    item1.Checked = True
    item1.SubItems.Add("1")
    item1.SubItems.Add("2")
    item1.SubItems.Add("3")
    Dim item2 As New ListViewItem("item2", 1)
    item2.SubItems.Add("4")
    item2.SubItems.Add("5")
    item2.SubItems.Add("6")
    Dim item3 As New ListViewItem("item3", 0)
    ' Place a check mark next to the item.
    item3.Checked = True
    item3.SubItems.Add("7")
    item3.SubItems.Add("8")
    item3.SubItems.Add("9")

    ' Create columns for the items and subitems.
    ' Width of -2 indicates auto-size.
    listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left)
    listView1.Columns.Add("Column 2", -2, HorizontalAlignment.Left)
    listView1.Columns.Add("Column 3", -2, HorizontalAlignment.Left)
    listView1.Columns.Add("Column 4", -2, HorizontalAlignment.Center)

    'Add the items to the ListView.
    listView1.Items.AddRange(New ListViewItem() {item1, item2, item3})

    ' Create two ImageList objects.
    Dim imageListSmall As New ImageList()
    Dim imageListLarge As New ImageList()

    ' Initialize the ImageList objects with bitmaps.
    imageListSmall.Images.Add(Bitmap.FromFile("C:\MySmallImage1.bmp"))
    imageListSmall.Images.Add(Bitmap.FromFile("C:\MySmallImage2.bmp"))
    imageListLarge.Images.Add(Bitmap.FromFile("C:\MyLargeImage1.bmp"))
    imageListLarge.Images.Add(Bitmap.FromFile("C:\MyLargeImage2.bmp"))

    'Assign the ImageList objects to the ListView.
    listView1.LargeImageList = imageListLarge
    listView1.SmallImageList = imageListSmall

    ' Add the ListView to the control collection.
    Me.Controls.Add(listView1)
End Sub

注釈

View プロパティを使用すると、ListView コントロールが項目の表示に使用する表示の種類を指定できます。 Viewプロパティを設定して、大きいアイコンまたは小さいアイコンで各項目を表示したり、項目を縦のリストに表示したりできます。 最も豊富なオプションは詳細ビューであり、アイテムだけでなく、各項目に指定されたサブ項目も表示できます。 各項目はグリッドに表示され、各項目は垂直方向に一覧表示され、各項目のサブ項目は列見出しと共に列に表示されます。 詳細ビューは、データベース情報をユーザーに表示するのに最適な方法です。 Windows XP および Windows Server 2003 では、大きなアイコンと選択したサブ項目情報を表示することで、グラフィカル情報とテキスト情報のバランスを取るタイルとして項目を表示することもできます。 タイル ビューを有効にするには、アプリケーションで Application.EnableVisualStyles メソッドを呼び出す必要があります。 小さい画像ビューでは、アイコンとテキスト情報がアイコンの右側に各項目に表示されます。 大きな画像ビューには、アイコンとアイコンの下にテキスト情報が表示された各項目が表示されます。 イメージリストのアイコンのサイズは、SmallImageListまたはLargeImageListプロパティのImageListImageSizeプロパティによって指定されます。

複数のイメージ リストを使用している場合は、小さいアイコン ビューと大きいアイコン ビューで、 ListView コントロールを使用して、それぞれのイメージ リスト内の同じインデックス位置に小さいサイズのイメージを配置する必要があります。 ビューを切り替えるとき、指定されたキー値に関係なく、一方のリスト内のイメージのインデックス位置を使用して、もう一方のリスト内のイメージを検索します。

ListView コントロールのほとんどのプロパティは、さまざまなビューの動作や表示方法に影響します。 項目のビューに影響を与えるプロパティの中には、 View プロパティが特定の値に設定されている場合にのみ役立つプロパティもあれば、すべてのビューで役立つプロパティもあります。 たとえば、 GridLinesFullRowSelect などのプロパティは、 View プロパティが View.Detailsに設定されている場合にのみ役立ちますが、 MultiSelect プロパティと CheckBoxes プロパティはすべてのビューで役立ちます。

次の表は、 ListView メンバーの一部と、それらが有効なビューを示しています。

ListView メンバー 表示
Alignment プロパティ SmallIcon または LargeIcon
AutoArrange プロパティ SmallIcon または LargeIcon
AutoResizeColumn メソッド Details
CheckBoxes を除くすべてのビュー Tile
Columns プロパティ Details または Tile
DrawSubItem 出来事 Details
FindItemWithText メソッド DetailsList、または Tile
FindNearestItem メソッド SmallIcon または LargeIcon
GetItemAt メソッド Details または Tile
Groups プロパティ を除くすべてのビュー List
HeaderStyle プロパティ Details
InsertionMark プロパティ LargeIconSmallIcon、または Tile

View プロパティを使用すると、アプリケーション内のさまざまなデータ ビューを提供したり、特定のビューをロックしてそのビューの利点を利用することができます。 たとえば、詳細ビューには他のビューでは使用できない表示オプションが多数用意されているため、 View プロパティは多くの場合、 View.Details に設定されます。

ListView コントロールに列ヘッダーが指定されておらず、View プロパティを View.Details に設定した場合、ListView コントロールには項目は表示されません。 ListView コントロールに列ヘッダーが指定されておらず、View プロパティを View.Tile に設定した場合、ListView コントロールにはサブ項目は表示されません。

タイル ビューには、各項目が左側に大きなアイコンが表示され、右側にテキスト情報が表示されます。 テキスト情報は、項目ラベルとそれに続くサブ項目で構成されます。 既定では、最初のサブ項目のみが表示され、項目ラベルに対応します。 追加のサブ項目を表示するには、Columns コレクションColumnHeaderオブジェクトを追加する必要があります。 タイル内の各サブ項目は、列ヘッダーに対応します。 表示するサブ項目とその表示順序を制御するには、各項目の ListViewItem.ListViewSubItem.Name プロパティと各ヘッダーの ColumnHeader.Name プロパティを設定する必要があります。 その後、 Columns コレクション内のヘッダーを追加、削除、および再配置して、目的の結果を得ることができます。

タイル ビューのタイルのサイズを制御するには、 TileSize プロパティを設定します。 これは、サブ項目テキストが 1 行に対して長すぎる場合に行折り返しを防ぐのに役立ちます。

タイル ビューの例については、 TileSize プロパティを参照してください。

列は詳細ビューにのみ表示されますが、列ヘッダーのないサブ項目は詳細ビューまたはタイル ビューには表示されません。

タイル ビューは、アプリケーションが Application.EnableVisualStyles メソッドを呼び出すときに、Windows XP および Windows Server 2003 でのみ使用できます。 以前のオペレーティング システムでは、タイル ビューに関連するコードは影響を受けず、 ListView コントロールは大きなアイコン ビューに表示されます。 その結果、タイル ビューに依存するコードが正しく機能しない可能性があります。

タイル ビューを使用できるかどうかを決定するコードを含め、使用できない場合は代替機能を提供することができます。 たとえば、所有者描画を使用してタイル ビュー内の ListView 項目の外観をカスタマイズする場合は、タイル ビューをサポートしていないオペレーティング システムで実行するときに、大きなアイコン ビューに適した描画コードを使用できます。

タイル ビュー機能は、オペレーティング システムのテーマ機能を提供するのと同じライブラリによって提供されます。 このライブラリの可用性を確認するには、 FeatureSupport.IsPresent(Object) メソッドのオーバーロードを呼び出し、 OSFeature.Themes 値を渡します。

適用対象

こちらもご覧ください