C#中ListView控件应用实例.docx
- 文档编号:11209179
- 上传时间:2023-02-25
- 格式:DOCX
- 页数:17
- 大小:38.67KB
C#中ListView控件应用实例.docx
《C#中ListView控件应用实例.docx》由会员分享,可在线阅读,更多相关《C#中ListView控件应用实例.docx(17页珍藏版)》请在冰豆网上搜索。
C#中ListView控件应用实例
C#中ListView控件应用实例
ListView控件
1. 功能
ListView控件可以显示带图标的项列表,用户可使用该控件创建类似Windows资源管理器的用户界面。
ListView控件具有4种视图模式:
(1)仅文本,这是默认视图,此视图下,只显示列表项的文本;
(2)带有小图标的文本,此视图下,小图标随列表项的文本同时显示;
(3)带有大图标的文本,此视图下,大图标随列表项的文本同时显示;
(4)报表视图,此视图下,列表项显示在多个列中。
图1为ListView控件。
图1 ListView控件
2.属性
ListView控件常用属性及说明如表1所示。
表1 ListView控件常用属性及说明
下面对比较重要的属性进行详细介绍。
(1)View属性。
用于获取或设置项在控件中的显示方式。
语法:
publicViewView{get;set;}
属性值:
View值之一。
默认为LargeIcon。
View的属性值及说明如表2所示。
表2 View的属性值及说明
(2)FullrowSelect属性。
用于指定是只选择某一项,还是选择某一项所在的整行。
语法:
publicboolFullRowSelect{get;set;}
属性值:
如果单击某项会选择该项及其所有子项,则为True;如果单击某项仅选择项本身,则为False。
默认为False。
说明:
除非将ListView控件的View属性设置为Details,否则FullRowSelect属性无效。
在ListView显示带有许多子项的项时,通常使用FullrowSelect属性,并且,在由于控件容的水平滚动而无法看到项文本时,能够查看选定项是非常重要的。
(3)GridLines属性。
指定在包含控件中项及其子项的行和列之间是否显示网格线。
语法:
publicboolGridLines{get;set;}
属性值:
如果在项及其子项的周围绘制网格线,则为True;否则为False。
默认为False。
说明:
除非将ListView控件的View属性设置为Details,否则GridLines属性无效。
示例
FullrowSelect属性
本示例主要介绍View属性和FullrowSelect属性的使用方法,示例运行结果如图2所示。
图2 FullrowSelect属性
程序主要代码如下:
this.lvStudent.View=View.Details;
this.lvStudent.FullRowSelect=True;
this.lvStudent.GridLines=True;
完整程序代码如下:
★★★★★主程序文件完整程序代码★★★★★
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Windows.Forms;
namespace_8_07
{
staticclassProgram
{
///
///应用程序的主入口点。
///
[STAThread]
staticvoidMain()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(newfrmListView());
}
}
}
★★★★★Form1窗体设计文件完整程序代码★★★★★
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
namespace_8_07
{
publicpartialclassForm1:
Form
{
publicForm1()
{
InitializeComponent();
}
}
}
★★★★★Form1窗体代码文件完整程序代码★★★★★
namespace_8_07
{
partialclassForm1
{
///
///必需的设计器变量。
///
privateSystem.ComponentModel.IContainercomponents=null;
///
///清理所有正在使用的资源。
///
///
protectedoverridevoidDispose(booldisposing)
{
if(disposing&&(components!
=null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#regionWindows窗体设计器生成的代码
///
///设计器支持所需的方法-不要
///使用代码编辑器修改此方法的容。
///
privatevoidInitializeComponent()
{
ponents=newSystem.ComponentModel.Container();
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.Text="Form1";
}
#endregion
}
}
★★★★★frmListView窗体设计文件完整程序代码★★★★★
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Data.SqlClient;
namespace_8_07
{
publicpartialclassfrmListView:
Form
{
publicfrmListView()
{
InitializeComponent();
}
privatevoidfrmListView_Load(objectsender,EventArgse)
{
}
privatevoidbntDelete_Click(objectsender,EventArgse)
{
lvStudent.Items.Clear();
}
privatevoidbntAdd_Click(objectsender,EventArgse)
{
this.lvStudent.View=View.Details;
this.lvStudent.FullRowSelect=true;
SqlConnectioncon=newSqlConnection("server=(local);uid=sa;pwd=;database=zhy");
con.Open();
SqlCommand=newSqlCommand("select*fromstudent",con);
SqlDataReaderdr=.ExecuteReader();
this.lvStudent.Items.Clear();
while(dr.Read())
{
ListViewItemlt=newListViewItem(dr.GetValue(0).ToString());
lt.SubItems.Add(dr.GetValue
(1).ToString());
lt.SubItems.Add(dr.GetValue
(2).ToString());
this.lvStudent.Items.Add(lt);
}
dr.Close();
con.Close();
this.lvStudent.Alignment=ListViewAlignment.SnapToGrid;
this.lvStudent.GridLines=true;
}
privatevoidbntEsce_Click(objectsender,EventArgse)
{
Application.Exit();
}
privatevoidlabel1_Click(objectsender,EventArgse)
{
}
privatevoidlvStudent_SelectedIndexChanged(objectsender,EventArgse)
{
}
privatevoidlvStudent_Click(objectsender,EventArgse)
{
}
}
}
★★★★★frmListView窗体代码文件完整程序代码★★★★★
namespace_8_07
{
partialclassfrmListView
{
///
///必需的设计器变量。
///
privateSystem.ComponentModel.IContainercomponents=null;
///
///清理所有正在使用的资源。
///
///
protectedoverridevoidDispose(booldisposing)
{
if(disposing&&(components!
=null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#regionWindows窗体设计器生成的代码
///
///设计器支持所需的方法-不要
///使用代码编辑器修改此方法的容。
///
privatevoidInitializeComponent()
{
this.lvStudent=newSystem.Windows.Forms.ListView();
this.columnHeader1=newSystem.Windows.Forms.ColumnHeader();
this.columnHeader2=newSystem.Windows.Forms.ColumnHeader();
this.columnHeader3=newSystem.Windows.Forms.ColumnHeader();
this.bntAdd=newSystem.Windows.Forms.Button();
this.bntDelete=newSystem.Windows.Forms.Button();
this.bntEsce=newSystem.Windows.Forms.Button();
this.txtName=newSystem.Windows.Forms.TextBox();
this.label1=newSystem.Windows.Forms.Label();
this.SuspendLayout();
//
//lvStudent
//
this.lvStudent.Columns.AddRange(newSystem.Windows.Forms.ColumnHeader[]{
this.columnHeader1,
this.columnHeader2,
this.columnHeader3});
this.lvStudent.Location=newSystem.Drawing.Point(26,32);
this.lvStudent.Name="lvStudent";
this.lvStudent.Size=newSystem.Drawing.Size(352,114);
this.lvStudent.TabIndex=0;
this.lvStudent.UseCompatibleStateImageBehavior=false;
this.lvStudent.View=System.Windows.Forms.View.Details;
this.lvStudent.SelectedIndexChanged+=newSystem.EventHandler(this.lvStudent_SelectedIndexChanged);
this.lvStudent.Click+=newSystem.EventHandler(this.lvStudent_Click);
//
//columnHeader1
//
this.columnHeader1.Text="学号";
this.columnHeader1.Width=97;
//
//columnHeader2
//
this.columnHeader2.Text="学生姓名";
this.columnHeader2.Width=136;
//
//columnHeader3
//
this.columnHeader3.Text="学生班级";
this.columnHeader3.Width=118;
//
//bntAdd
//
this.bntAdd.Location=newSystem.Drawing.Point(26,171);
this.bntAdd.Name="bntAdd";
this.bntAdd.Size=newSystem.Drawing.Size(75,23);
this.bntAdd.TabIndex=2;
this.bntAdd.Text="加截(&F)";
this.bntAdd.UseVisualStyleBackColor=true;
this.bntAdd.Click+=newSystem.EventHandler(this.bntAdd_Click);
//
//bntDelete
//
this.bntDelete.Location=newSystem.Drawing.Point(159,171);
this.bntDelete.Name="bntDelete";
this.bntDelete.Size=newSystem.Drawing.Size(75,23);
this.bntDelete.TabIndex=3;
this.bntDelete.Text="清除(&G)";
this.bntDelete.UseVisualStyleBackColor=true;
this.bntDelete.Click+=newSystem.EventHandler(this.bntDelete_Click);
//
//bntEsce
//
this.bntEsce.Location=newSystem.Drawing.Point(303,171);
this.bntEsce.Name="bntEsce";
this.bntEsce.Size=newSystem.Drawing.Size(75,23);
this.bntEsce.TabIndex=4;
this.bntEsce.Text="退出(&T)";
this.bntEsce.UseVisualStyleBackColor=true;
this.bntEsce.Click+=newSystem.EventHandler(this.bntEsce_Click);
//
//txtName
//
this.txtName.Location=newSystem.Drawing.Point(159,209);
this.txtName.Name="txtName";
this.txtName.Size=newSystem.Drawing.Size(116,21);
this.txtName.TabIndex=5;
//
//label1
//
this.label1.AutoSize=true;
this.label1.Location=newSystem.Drawing.Point(73,212);
this.label1.Name="label1";
this.label1.Size=newSystem.Drawing.Size(59,12);
this.label1.TabIndex=6;
this.label1.Text="学生姓名:
";
this.label1.Click+=newSystem.EventHandler(this.label1_Click);
//
//frmListView
//
this.AutoScaleDimensions=newSystem.Drawing.SizeF(6F,12F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize=newSystem.Drawing.Size(418,266);
this.Controls.Add(this.label1);
this.Controls.Add(this.txtName);
this.Controls.Add(this.bntEsce);
this.Controls.Add(this.bntDelete);
this.Controls.Add(this.bntAdd);
this.Controls.Add(this.lvStudent);
this.Name="frmListView";
this.Text="frmListView";
this.Load+=newSystem.EventHandler(this.frmListView_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
privateSystem.Windows.Forms.ListViewlvStudent;
privateSystem.Windows.Forms.ButtonbntAdd;
privateSystem.Windows.Forms.ButtonbntDelete;
privateSystem.Windows.Forms.ButtonbntEsce;
privateSystem.Windows.Forms.ColumnHeadercolumnHeader1;
privateSystem.Windows.Forms.ColumnHeadercolumnHeader2;
privateSystem.Windows.Forms.ColumnHeadercolumnHeader3;
privateSystem.Windows.Forms.TextBoxtxtName;
privateSystem.Windows.Forms.Labellabel1;
}
}
3.方法
(1)HitTest方法。
该方法在给定x和y坐标的情况下,提供项信息。
(2)Clear方法。
该方法用于删除ListView控件中所有的项。
语法:
publicvoidClear()
例如,下面删除名称为lvStudent的ListView控件的所有项:
lvStudent.Items.Clear();
4.事件
(1)ItemCheck事件。
该事件在选中ListView控件项时触发此事件。
(2)Click事件。
该事件在单击ListView控件列时触发。
语法:
publiceventEventHandlerClick
示例
Click事件的使用
本示例实现效果为,当程序运行时,单击【ListView】按钮,将姓名显示在文本框中。
示例运行结果如图3所示。
图3Click事件的使用
程序主要代码如下:
privatevoidlvStudent_Click(objectsender,EventArgse)
{
this.txtName.Text=lvStudent.SelectedItems[0].SubItems[1].Text;
}
完整程序代码如下:
★★★★★主程序文件完整程序代码★★★★★
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Windows.Forms;
namespace_8_07
{
staticclassProgram
{
///
///应用程序的主入口点。
///
[STAThread]
staticvoidMain()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(newfrmListView());
}
}
}
★★★★★Form1窗体设计文件完整程序代码★★★★★
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Text;
usingSystem.Windows.Forms;
namespace_8_07
{
publicpartialclassForm1:
Form
{
publicForm1()
{
InitializeComponent();
}
}
}
★★★★★Form1窗体代码文件完整程序代码
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C# ListView 控件 应用 实例