




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、【精品文档】如有侵权,请联系网站删除,仅供学习与交流C#中ListView控件应用实例.精品文档.C#中ListView控件应用实例ListView控件1 功能ListView控件可以显示带图标的项列表,用户可使用该控件创建类似Windows资源管理器的用户界面。ListView控件具有4种视图模式:(1)仅文本,这是默认视图,此视图下,只显示列表项的文本;(2)带有小图标的文本,此视图下,小图标随列表项的文本同时显示;(3)带有大图标的文本,此视图下,大图标随列表项的文本同时显示;(4)报表视图,此视图下,列表项显示在多个列中。图1为List View控件。图1 Li
2、stView 控件2属性ListView控件常用属性及说明如表1所示。表1 ListView控件常用属性及说明下面对比较重要的属性进行详细介绍。(1)View属性。用于获取或设置项在控件中的显示方式。语法: public View View get; set; 属性值:View值之一。默认为LargeIcon。View的属性值及说明如表2所示。表2 View的属性值及说明(2)FullrowSelect属性。用于指定是只选择某一项,还是选择某一项所在的整行。语法: public
3、bool FullRowSelect get; set; 属性值:如果单击某项会选择该项及其所有子项,则为True;如果单击某项仅选择项本身,则为False。默认为False。说明:除非将ListView控件的View属性设置为Details,否则FullRowSelect属性无效。在ListView显示带有许多子项的项时,通常使用FullrowSelect属性,并且,在由于控件内容的水平滚动而无法看到项文本时,能够查看选定项是非常重要的。(3)GridLines属性。指定在包含控件中项及其子项的行和列之间是否显示网格线。语法: public bool GridLines get;
4、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
5、;完整程序代码如下:主程序文件完整程序代码using System;using System.Collections.Generic;using System.Windows.Forms;namespace _8_07static class Program/ <summary>/ 应用程序的主入口点。/ </summary>STAThreadstatic void Main()Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application
6、.Run(new frmListView();Form1窗体设计文件完整程序代码using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace _8_07public partial class Form1 : Formpublic Form1()InitializeComponent();Form1窗体代码文件完整程序代码n
7、amespace _8_07partial class Form1/ <summary>/ 必需的设计器变量。/ </summary>private System.ComponentModel.IContainer components = null;/ <summary>/ 清理所有正在使用的资源。/ </summary>/ <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>protected override void Dispos
8、e(bool disposing)if (disposing && (components != null)components.Dispose();base.Dispose(disposing);#region Windows 窗体设计器生成的代码/ <summary>/ 设计器支持所需的方法 - 不要/ 使用代码编辑器修改此方法的内容。/ </summary>private void InitializeComponent()ponents = new System.ComponentModel.Container();this.AutoScaleM
9、ode = System.Windows.Forms.AutoScaleMode.Font;this.Text = "Form1"#endregionfrmListView窗体设计文件完整程序代码using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;namesp
10、ace _8_07public partial class frmListView : Formpublic frmListView()InitializeComponent();private void frmListView_Load(object sender, EventArgs e)private void bntDelete_Click(object sender, EventArgs e)lvStudent.Items.Clear();private void bntAdd_Click(object sender, EventArgs e)this.lvStudent.View
11、= View.Details;this.lvStudent.FullRowSelect = true;SqlConnection con = new SqlConnection("server=(local);uid=sa;pwd=;database=zhy");con.Open();SqlCommand com = new SqlCommand("select * from student", con);SqlDataReader dr = com.ExecuteReader();this.lvStudent.Items.Clear();while (
12、dr.Read()ListViewItem lt = new ListViewItem(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;privat
13、e void bntEsce_Click(object sender, EventArgs e)Application.Exit();private void label1_Click(object sender, EventArgs e)private void lvStudent_SelectedIndexChanged(object sender, EventArgs e)private void lvStudent_Click(object sender, EventArgs e)frmListView窗体代码文件完整程序代码namespace _8_07partial class f
14、rmListView/ <summary>/ 必需的设计器变量。/ </summary>private System.ComponentModel.IContainer components = null;/ <summary>/ 清理所有正在使用的资源。/ </summary>/ <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>protected override void Dispose(bool disposing)if (di
15、sposing && (components != null)components.Dispose();base.Dispose(disposing);#region Windows 窗体设计器生成的代码/ <summary>/ 设计器支持所需的方法 - 不要/ 使用代码编辑器修改此方法的内容。/ </summary>private void InitializeComponent()this.lvStudent = new System.Windows.Forms.ListView();this.columnHeader1 = new System.W
16、indows.Forms.ColumnHeader();this.columnHeader2 = new System.Windows.Forms.ColumnHeader();this.columnHeader3 = new System.Windows.Forms.ColumnHeader();this.bntAdd = new System.Windows.Forms.Button();this.bntDelete = new System.Windows.Forms.Button();this.bntEsce = new System.Windows.Forms.Button();th
17、is.txtName = new System.Windows.Forms.TextBox();this.label1 = new System.Windows.Forms.Label();this.SuspendLayout();/ lvStudent/this.lvStudent.Columns.AddRange(new System.Windows.Forms.ColumnHeader this.columnHeader1,this.columnHeader2,this.columnHeader3);this.lvStudent.Location = new System.Drawing
18、.Point(26, 32);this.lvStudent.Name = "lvStudent"this.lvStudent.Size = new System.Drawing.Size(352, 114);this.lvStudent.TabIndex = 0;this.lvStudent.UseCompatibleStateImageBehavior = false;this.lvStudent.View = System.Windows.Forms.View.Details;this.lvStudent.SelectedIndexChanged += new Syst
19、em.EventHandler(this.lvStudent_SelectedIndexChanged);this.lvStudent.Click += new System.EventHandler(this.lvStudent_Click);/ columnHeader1/this.columnHeader1.Text = "学号"this.columnHeader1.Width = 97;/ columnHeader2/this.columnHeader2.Text = "学生姓名"this.columnHeader2.Width = 136;/
20、columnHeader3/this.columnHeader3.Text = "学生班级"this.columnHeader3.Width = 118;/ bntAdd/this.bntAdd.Location = new System.Drawing.Point(26, 171);this.bntAdd.Name = "bntAdd"this.bntAdd.Size = new System.Drawing.Size(75, 23);this.bntAdd.TabIndex = 2;this.bntAdd.Text = "加截(&F
21、)"this.bntAdd.UseVisualStyleBackColor = true;this.bntAdd.Click += new System.EventHandler(this.bntAdd_Click);/ bntDelete/this.bntDelete.Location = new System.Drawing.Point(159, 171);this.bntDelete.Name = "bntDelete"this.bntDelete.Size = new System.Drawing.Size(75, 23);this.bntDelete.T
22、abIndex = 3;this.bntDelete.Text = "清除(&G)"this.bntDelete.UseVisualStyleBackColor = true;this.bntDelete.Click += new System.EventHandler(this.bntDelete_Click);/ bntEsce/this.bntEsce.Location = new System.Drawing.Point(303, 171);this.bntEsce.Name = "bntEsce"this.bntEsce.Size =
23、new System.Drawing.Size(75, 23);this.bntEsce.TabIndex = 4;this.bntEsce.Text = "退出(&T)"this.bntEsce.UseVisualStyleBackColor = true;this.bntEsce.Click += new System.EventHandler(this.bntEsce_Click);/ txtName/this.txtName.Location = new System.Drawing.Point(159, 209);this.txtName.Name = &
24、quot;txtName"this.txtName.Size = new System.Drawing.Size(116, 21);this.txtName.TabIndex = 5;/ label1/this.label1.AutoSize = true;this.label1.Location = new System.Drawing.Point(73, 212);this.label1.Name = "label1"this.label1.Size = new System.Drawing.Size(59, 12);this.label1.TabIndex
25、= 6;this.label1.Text = "学生姓名:"this.label1.Click += new System.EventHandler(this.label1_Click);/ frmListView/this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;this.ClientSize = new System.Drawing.Size(418, 266);this.Con
26、trols.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 += new System.EventHandler(this.fr
27、mListView_Load);this.ResumeLayout(false);this.PerformLayout();#endregionprivate System.Windows.Forms.ListView lvStudent;private System.Windows.Forms.Button bntAdd;private System.Windows.Forms.Button bntDelete;private System.Windows.Forms.Button bntEsce;private System.Windows.Forms.ColumnHeader colum
28、nHeader1;private System.Windows.Forms.ColumnHeader columnHeader2;private System.Windows.Forms.ColumnHeader columnHeader3;private System.Windows.Forms.TextBox txtName;private System.Windows.Forms.Label label1;3方法(1)HitTest方法。该方法在给定x和y坐标的情况下,提供项信息。(2)Clear方法。该方法用于删除ListView控件中所有的项。语法: public void
29、 Clear ()例如,下面删除名称为lvStudent的ListView控件的所有项:lvStudent.Items.Clear();4事件(1)ItemCheck事件。该事件在选中ListView控件项时触发此事件。(2)Click事件。该事件在单击ListView控件列时触发。语法: public event EventHandler Click示例Click事件的使用本示例实现效果为,当程序运行时,单击【ListView】按钮,将姓名显示在文本框中。示例运行结果如图3所示。图3 Click事件的使用程序主要代码如下:private void lvStudent_Click(o
30、bject sender, EventArgs e)this.txtName.Text = lvStudent.SelectedItems0.SubItems1.Text;完整程序代码如下:主程序文件完整程序代码using System;using System.Collections.Generic;using System.Windows.Forms;namespace _8_07static class Program/ <summary>/ 应用程序的主入口点。/ </summary>STAThreadstatic void Main()Application.
31、EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(new frmListView();Form1窗体设计文件完整程序代码using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace _8_07pu
32、blic partial class Form1 : Formpublic Form1()InitializeComponent();Form1窗体代码文件完整程序代码namespace _8_07partial class Form1/ <summary>/ 必需的设计器变量。/ </summary>private System.ComponentModel.IContainer components = null;/ <summary>/ 清理所有正在使用的资源。/ </summary>/ <param name="dispo
33、sing">如果应释放托管资源,为 true;否则为 false。</param>protected override void Dispose(bool disposing)if (disposing && (components != null)components.Dispose();base.Dispose(disposing);#region Windows 窗体设计器生成的代码/ <summary>/ 设计器支持所需的方法 - 不要/ 使用代码编辑器修改此方法的内容。/ </summary>private void
34、 InitializeComponent()ponents = new System.ComponentModel.Container();this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;this.Text = "Form1"#endregionfrmListView窗体设计文件完整程序代码using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Dr
35、awing;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;namespace _8_07public partial class frmListView : Formpublic frmListView()InitializeComponent();private void frmListView_Load(object sender, EventArgs e)private void bntDelete_Click(object sender, EventArgs e)lvStudent.It
36、ems.Clear();private void bntAdd_Click(object sender, EventArgs e)this.lvStudent.View = View.Details;this.lvStudent.FullRowSelect = true;SqlConnection con = new SqlConnection("server=(local);uid=sa;pwd=;database=zhy");con.Open();SqlCommand com = new SqlCommand("select * from student&qu
37、ot;, con);SqlDataReader dr = com.ExecuteReader();this.lvStudent.Items.Clear();while (dr.Read()ListViewItem lt = new ListViewItem(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.lvSt
38、udent.Alignment = ListViewAlignment.SnapToGrid;this.lvStudent.GridLines = true;private void bntEsce_Click(object sender, EventArgs e)Application.Exit();private void label1_Click(object sender, EventArgs e)private void lvStudent_SelectedIndexChanged(object sender, EventArgs e)private void lvStudent_C
39、lick(object sender, EventArgs e)this.txtName.Text = lvStudent.SelectedItems0.SubItems1.Text;frmListView窗体代码文件完整程序代码namespace _8_07partial class frmListView/ <summary>/ 必需的设计器变量。/ </summary>private System.ComponentModel.IContainer components = null;/ <summary>/ 清理所有正在使用的资源。/ </su
40、mmary>/ <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>protected override void Dispose(bool disposing)if (disposing && (components != null)components.Dispose();base.Dispose(disposing);#region Windows 窗体设计器生成的代码/ <summary>/ 设计器支持所需的方法 - 不要/ 使用代码编辑器修改
41、此方法的内容。/ </summary>private void InitializeComponent()this.lvStudent = new System.Windows.Forms.ListView();this.columnHeader1 = new System.Windows.Forms.ColumnHeader();this.columnHeader2 = new System.Windows.Forms.ColumnHeader();this.columnHeader3 = new System.Windows.Forms.ColumnHeader();this.
42、bntAdd = new System.Windows.Forms.Button();this.bntDelete = new System.Windows.Forms.Button();this.bntEsce = new System.Windows.Forms.Button();this.txtName = new System.Windows.Forms.TextBox();this.label1 = new System.Windows.Forms.Label();this.SuspendLayout();/ lvStudent/this.lvStudent.Columns.AddR
43、ange(new System.Windows.Forms.ColumnHeader this.columnHeader1,this.columnHeader2,this.columnHeader3);this.lvStudent.Location = new System.Drawing.Point(26, 32);this.lvStudent.Name = "lvStudent"this.lvStudent.Size = new System.Drawing.Size(352, 114);this.lvStudent.TabIndex = 0;this.lvStuden
44、t.UseCompatibleStateImageBehavior = false;this.lvStudent.View = System.Windows.Forms.View.Details;this.lvStudent.SelectedIndexChanged += new System.EventHandler(this.lvStudent_SelectedIndexChanged);this.lvStudent.Click += new System.EventHandler(this.lvStudent_Click);/ columnHeader1/this.columnHeade
45、r1.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 = new System.Drawing.Point(26, 171);th
46、is.bntAdd.Name = "bntAdd"this.bntAdd.Size = new System.Drawing.Size(75, 23);this.bntAdd.TabIndex = 2;this.bntAdd.Text = "加截(&F)"this.bntAdd.UseVisualStyleBackColor = true;this.bntAdd.Click += new System.EventHandler(this.bntAdd_Click);/ bntDelete/this.bntDelete.Location = new
47、 System.Drawing.Point(159, 171);this.bntDelete.Name = "bntDelete"this.bntDelete.Size = new System.Drawing.Size(75, 23);this.bntDelete.TabIndex = 3;this.bntDelete.Text = "清除(&G)"this.bntDelete.UseVisualStyleBackColor = true;this.bntDelete.Click += new System.EventHandler(this.bntDelete_Click);/ bntEsce/this.bntEsce.Location = new System.Drawing.Point(303, 171);this.bntEsce.Name = "bntEsce"this.bntEsce.Size = new System.Drawing.Size(75, 23);this.bntEsce.TabIndex = 4;this.bntEsce.Text = "退出(&T)"this.bntEsce.
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 知识点强化的2025年行政组织理论试题及答案
- 周口公园湖面管理制度
- 公路路政督察管理制度
- 公司食堂奖惩管理制度
- 冷链产品交接管理制度
- 客户服务车辆管理制度
- 公路工程技术创新试题及答案
- 商场宣传物料管理制度
- 城镇保洁人员管理制度
- 确保团队间有效协作的项目管理方法与应用实例试题及答案
- 铁路工务技术手册
- (完整版)硬件测试规范
- 2006年工资标准及套改对应表
- 《港口装卸工艺学》课程设计
- 《洁净工程项目定额》(征求意见稿)
- JJG 151-2006 金属维氏硬度计检定规程-(高清现行)
- DBJ∕T 13-183-2014 基桩竖向承载力自平衡法静载试验技术规程
- 张双楼煤矿安全评价报告(出版稿10.14)
- [模板]健康教育处方
- 妇产科英语词汇
- 病媒生物密度监测方法 蜚蠊
评论
0/150
提交评论