项目如何立项:CListView 类

来源:百度文库 编辑:九乡新闻网 时间:2024/05/08 01:56:54

ListView 类

.NET Framework 2.0其他版本

表示 Windows 列表视图控件,该控件显示可用四种不同视图之一显示的项集合。

命名空间:System.Windows.Forms
程序集:System.Windows.Forms(在 system.windows.forms.dll 中)

语法
C#C++VB
[ComVisibleAttribute(true)] [ClassInterfaceAttribute(ClassInterfaceType::AutoDispatch)] public ref class ListView : public Control
J#
/** @attribute ComVisibleAttribute(true) */ /** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) */ public class ListView extends Control
JScript
ComVisibleAttribute(true) ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) public class ListView extends Control
备注

ListView 控件允许您显示项列表,这些项带有项文本和图标(可选)来标识项的类型。例如,Windows 资源管理器的文件列表就与 ListView 控件的外观相似。它显示树中当前选定的文件和文件夹的列表。每个文件和文件夹都显示一个与之相关的图标,以帮助标识文件或文件夹的类型。ListViewItem 类表示 ListView 控件中的项。列表中显示的项可以用五种不同视图之一显示。这些项可以显示为大图标、小图标,也可以在垂直列表中显示为小图标。这些项也可以具有子项,子项包含与父项相关的信息。详细信息视图允许您在网格中显示项及其子项,并通过列标头标识要在子项中显示的信息。平铺视图的应用范围有一定的限制(如下所述),它允许以类似贴瓷砖的方式显示项及其子项,每块“瓷砖”(平铺单元)包含一个大图标以及图标旁的文字信息。ListView 支持单选和多选。多重选择功能使用户可以按照与 ListBox 控件相似的方式从项列表中进行选择。另外,用户还可以激活选定项来执行任务。例如,可以使用 ListView 控件显示应用程序可以打开并使用的文件的列表。用户可以选择要打开的文件,然后双击它们来激活项,并在应用程序中打开文件。ListView 也可以使用 CheckBoxes 属性显示复选框,以使用户可以选中要对其执行操作的项。可以用各种方式来使用 ListView 控件。控件可用于显示来自应用程序、数据库或文本文件的信息。ListView 也可用于获取来自用户的信息,例如选择一组要处理的文件。

ListView 提供了大量可灵活设置外观和行为的属性。View 属性允许您更改项的显示方式。LargeImageList、SmallImageList 和 StateImageList 属性允许您指定包含为项显示的图像的 ImageList 对象;并且,就 StateImageList 属性而言,当 CheckBoxes 属性设置为 true 时,它允许您指定所显示的复选框。要确定选中了哪些项,可使用 CheckedItems 属性来访问 ListView.CheckedListViewItemCollection 集合。Columns 属性允许访问ListView.ColumnHeaderCollection,它存储了当控件的 View 属性设置为 Details 时显示的列标头。通过 Items 属性,可以在 ListView 中添加和移除项。Items 属性允许您访问控件的 ListView.ListViewItemCollection,它提供在控件中操作项的方法。如果需要允许用户编辑项的文本,可使用 LabelEdit 属性。当控件包含大量的项时,用户在经过排序的列表中查看这些项通常会更加容易。您可以使用 Sorting 属性按字母顺序对项进行排序。您也可以对 ListView 控件的外观进行全面的自定义。为实现此目的,可将 OwnerDraw 属性设置为 true,并处理以下的一个或多个事件:DrawItem、DrawSubItem 和DrawColumnHeader。

当 ListView 控件的 View 属性设置为 Details 时,将使用该控件的许多属性。AllowColumnReorder 属性允许 ListView 控件的用户在运行时重新配置列的顺序。FullRowSelect 属性允许选择项及其子项(而不仅仅是项)。要在详细资料视图中显示网格线以标识 ListView 中的项及其子项的边界,可使用 GridLines属性。HeaderStyle 属性允许您指定要显示的列标头的类型。

除了可用于 ListView 控件的许多属性外,应用程序还可以使用方法和事件来为 ListView 提供附加功能。通过 BeginUpdate 和 EndUpdate 方法,可在每次添加项时防止控件进行重新绘制,从而在向 ListView 添加多个项时改善性能。如果 ListView 控件显示的是项和子项,您也许会需要提供用户用鼠标右键单击子项时的功能。要确定其子项被单击的项,可使用 GetItemAt 方法。在用户编辑项后对它们执行验证时,您也许需要向用户显示要更改的特定项。可以调用EnsureVisible 方法来确保特定项位于控件的可视区域中。

如果 LabelEdit 属性设置为 true,则可以执行如下的类似任务:通过为 BeforeLabelEdit 和 AfterLabelEdit 事件创建事件处理程序,在文本更改前后对所编辑的文本进行验证。要执行打开文件或显示对话框来编辑 ListView 中显示的项这样的任务,可以为 ItemActivate 事件创建事件处理程序。如果允许在用户单击列标头时对 ListView 中的项进行排序,则可以为 ColumnClick 事件创建事件处理程序以执行排序操作。当 CheckBoxes 属性设置为 true 时,您可以通过处理ItemCheck 事件来确定项的选中状态何时发生更改。

还可以使用 BackgroundImage 属性设置 ListView 的背景图像。为了正确显示 ListView 控件的背景图像,您的应用程序必须对其 Main 方法应用STAThreadAttribute。此外,如果带有背景图像的 ListView 控件寄宿在 Internet Explorer 中,请在应用程序的清单文件中将 comctl32.dll 6.0 版指定为依赖程序集,确保背景图像能够正确显示。

注意

为 ListView 控件设置 Cursor 属性不会对 .NET Framework 1.1 版及更早版本中的光标外观产生任何影响。

Windows XP 和 Windows Server 2003 提供了三种功能,以便在您的应用程序调用 Application.EnableVisualStyles 方法时增强 ListView 控件:平铺视图、分组和插入标记。

平铺视图通过在大图标旁边显示项及子项的文本,同时兼顾了图像和文字信息。通过将 View 属性设置为 View.Tile 可启用此行为。

分组功能允许您以可视化形式将项分组到相关类别之中。若要启用此功能,可使用 Groups 属性将 ListViewGroup 对象添加到 ListView 控件。若要临时禁用此功能,请将 ShowGroups 属性设置为 false

插入标记功能通过指示放置位置,为利用拖放操作调整项的位置提供了视觉反馈信息。使用通过 InsertionMark 属性检索到的 ListViewInsertionMark 对象显示插入标记。

这些功能仅能在 Windows XP 和 Windows Server 2003 下使用。对于较早期的平台,与这些功能相关的代码不会产生任何作用,平铺视图将显示为大图标视图,而插入标记和组则不会显示。在某些情况下,您可能需要编写代码来确定是否能够使用这些功能,并在它们不可用的情况下提供相应的替代功能。提供这些功能的库与提供操作系统主题功能的库为同一个库。若要检查此库的可用性,请调用 FeatureSupport.IsPresent(Object) 方法重载并传入 OSFeature.Themes值。

下表显示 ListView 的某些成员以及它们可用于的视图。

ListView 成员

视图

Alignment 属性

SmallIcon 或 LargeIcon

AutoArrange 属性

SmallIcon 或 LargeIcon

AutoResizeColumn 方法

Details

Columns 属性

Details 或 Tile

DrawSubItem 事件

Details

FindItemWithText 方法

Details、List 或 Tile

FindNearestItem 方法

SmallIcon 或 LargeIcon

GetItemAt 方法

Details 或 Tile

Groups 属性

除 List 之外的所有视图

HeaderStyle 属性

Details

InsertionMark 属性

LargeIconSmallIcon 或 Tile

示例

下面的代码示例创建一个 ListView 控件,其中带有三个指定的 ListViewItem 对象,而这三个对象中的每一项又带有三个指定的ListViewItem.ListViewSubItem 对象。该示例还创建 ColumnHeader 对象以在详细资料视图中显示子项。在代码示例中还创建两个 ImageList 对象,以便为ListViewItem 对象提供图像。这些 ImageList 对象被添加到 LargeImageList 和 SmallImageList 属性中。在创建 ListView 控件的过程中,该示例使用了下列属性:

  • View

  • LabelEdit

  • AllowColumnReorder

  • CheckBoxes

  • FullRowSelect

  • GridLines

  • Sorting

该示例要求已该代码添加到某个 Form 中,然后在构造函数或该窗体的其他方法调用在该示例中创建的方法。该示例还要求名为MySmallImage1MySmallImage2MyLargeImage1 和 MyLargeImage2 的图像位于驱动器 C 的根目录下。

C#C++VB
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.      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^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 );   }
J#
private void CreateMyListView(){    // Create a new ListView control.    ListView listView1 = new ListView();    listView1.set_Bounds(new Rectangle(new Point(10, 10),         new Size(300, 200)));    // Set the view to show details.    listView1.set_View(View.Details);    // Allow the user to edit item text.    listView1.set_LabelEdit(true);    // Allow the user to rearrange columns.    listView1.set_AllowColumnReorder(true);    // Display check boxes.    listView1.set_CheckBoxes(true);    // Select the item and subitems when selection is made.    listView1.set_FullRowSelect(true);    // Display grid lines.    listView1.set_GridLines(true);    // Sort the items in the list in ascending order.    listView1.set_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.set_Checked(true);    item1.get_SubItems().Add("1");    item1.get_SubItems().Add("2");    item1.get_SubItems().Add("3");    ListViewItem item2 = new ListViewItem("item2", 1);    item2.get_SubItems().Add("4");    item2.get_SubItems().Add("5");    item2.get_SubItems().Add("6");    ListViewItem item3 = new ListViewItem("item3", 0);    // Place a check mark next to the item.    item3.set_Checked(true);    item3.get_SubItems().Add("7");    item3.get_SubItems().Add("8");    item3.get_SubItems().Add("9");    // Create columns for the items and subitems.    listView1.get_Columns().Add("Item Column", -2,         HorizontalAlignment.Left);    listView1.get_Columns().Add("Column 2", -2, HorizontalAlignment.Left);    listView1.get_Columns().Add("Column 3", -2, HorizontalAlignment.Left);    listView1.get_Columns().Add("Column 4", -2, HorizontalAlignment.Center);    //Add the items to the ListView.    listView1.get_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.get_Images().Add(Bitmap.FromFile(        "C:\\MySmallImage1.bmp"));    imageListSmall.get_Images().Add(Bitmap.FromFile(        "C:\\MySmallImage2.bmp"));    imageListLarge.get_Images().Add(Bitmap.FromFile(        "C:\\MyLargeImage1.bmp"));    imageListLarge.get_Images().Add(Bitmap.FromFile(        "C:\\MyLargeImage2.bmp"));    //Assign the ImageList objects to the ListView.    listView1.set_LargeImageList(imageListLarge);    listView1.set_SmallImageList(imageListSmall);    // Add the ListView to the control collection.    this.get_Controls().Add(listView1);} //CreateMyListView
继承层次结构
System.Object 
   System.MarshalByRefObject 
     System.ComponentModel.Component 
       System.Windows.Forms.Control 
        System.Windows.Forms.ListView
线程安全
此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0
请参见

参考

ListView 成员 
System.Windows.Forms 命名空间 
ListViewItem 
ImageList 类 
Application.EnableVisualStyles