版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、C#中ListView控件应用实例ListView控件1 功能ListView控件可以显示带图标的项列表,用户可使用该控件创建类似Windows资源管理器的用户界面。ListView控件具有4种视图模式:(1)仅文本,这是默认视图,此视图下,只显示列表项的文本;(2)带有小图标的文本,此视图下,小图标随列表项的文本同时显示;(3)带有大图标的文本,此视图下,大图标随列表项的文本同时显示;(4)报表视图,此视图下,列表项显示在多个列中。图1为List View控件。图1 ListView 控件2属性ListView控件常用属性及说明如表1所示。表1
2、 ListView控件常用属性及说明下面对比较重要的属性进行详细介绍。(1)View属性。用于获取或设置项在控件中的显示方式。语法: public View View get; set; 属性值:View值之一。默认为LargeIcon。View的属性值及说明如表2所示。表2 View的属性值及说明(2)FullrowSelect属性。用于指定是只选择某一项,还是选择某一项所在的整行。语法: public bool FullRowSelect get; set; 属性值:如果单击某项会选择该项及其所有子
3、项,则为True;如果单击某项仅选择项本身,则为False。默认为False。说明:除非将ListView控件的View属性设置为Details,否则FullRowSelect属性无效。在ListView显示带有许多子项的项时,通常使用FullrowSelect属性,并且,在由于控件内容的水平滚动而无法看到项文本时,能够查看选定项是非常重要的。(3)GridLines属性。指定在包含控件中项及其子项的行和列之间是否显示网格线。语法: public bool GridLines get; set; 属性值:如果在项及其子项的周围绘制网格线,则为True;否则为False。默认为Fals
4、e。说明:除非将ListView控件的View属性设置为Details,否则GridLines属性无效。示例FullrowSelect属性本示例主要介绍View属性和FullrowSelect属性的使用方法,示例运行结果如图2所示。图2 FullrowSelect属性程序主要代码如下:this.lvStudent.View = View.Details;this.lvStudent.FullRowSelect = True;this.lvStudent.GridLines = True;完整程序代码如下:主程序文件完整程序代码using System;using System.Co
5、llections.Generic;using System.Windows.Forms;namespace _8_07static class Program/ <summary>/ 应用程序的主入口点。/ </summary>STAThreadstatic void Main()Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(new frmListView();Form1窗体设计文件完整程序代码using Sys
6、tem;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窗体代码文件完整程序代码namespace _8_07partial class Form1/ <summary>
7、;/ 必需的设计器变量。/ </summary>private System.ComponentModel.IContainer components = null;/ <summary>/ 清理所有正在使用的资源。/ </summary>/ <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>protected override void Dispose(bool disposing)if (disposing && (compon
8、ents != null)components.Dispose();base.Dispose(disposing);#region Windows 窗体设计器生成的代码/ <summary>/ 设计器支持所需的方法 - 不要/ 使用代码编辑器修改此方法的内容。/ </summary>private void InitializeComponent()ponents = new System.ComponentModel.Container();this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;thi
9、s.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;namespace _8_07public partial class frmListView : Formp
10、ublic 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 = View.Details;this.lvStudent.FullRowSelect = tru
11、e;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 (dr.Read()ListViewItem lt = new ListViewItem(dr.Ge
12、tValue(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;private void bntEsce_Click(object sender, EventArgs e)A
13、pplication.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 frmListView/ <summary>/ 必需的设计器变量。/ </summ
14、ary>private System.ComponentModel.IContainer components = null;/ <summary>/ 清理所有正在使用的资源。/ </summary>/ <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>protected override void Dispose(bool disposing)if (disposing && (components != null)components
15、.Dispose();base.Dispose(disposing);#region Windows 窗体设计器生成的代码/ <summary>/ 设计器支持所需的方法 - 不要/ 使用代码编辑器修改此方法的内容。/ </summary>private void InitializeComponent()this.lvStudent = new System.Windows.Forms.ListView();this.columnHeader1 = new System.Windows.Forms.ColumnHeader();this.columnHeader2 =
16、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();this.txtName = new System.Windows.Forms.TextBox();t
17、his.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.Point(26, 32);this.lvStudent.Name = "lvStud
18、ent"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 System.EventHandler(this.lvStudent_SelectedIndexChang
19、ed);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;/ columnHeader3/this.columnHeader3.Text = "学生班
20、级"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)"this.bntAdd.UseVisualStyleBackColor = true
21、;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.TabIndex = 3;this.bntDelete.Text = "清除(&G
22、)"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.TabI
23、ndex = 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 = "txtName"this.txtName.Size = new System.
24、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 = 6;this.label1.Text = "学生姓名:"this.labe
25、l1.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.Controls.Add(this.label1);this.Controls.Add(this.txt
26、Name);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.frmListView_Load);this.ResumeLayout(false);this.Per
27、formLayout();#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 columnHeader1;private System.Windows.Forms.ColumnHeade
28、r 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 Clear ()例如,下面删除名称为lvStudent的ListView控件的所有项:lvStu
29、dent.Items.Clear();4事件(1)ItemCheck事件。该事件在选中ListView控件项时触发此事件。(2)Click事件。该事件在单击ListView控件列时触发。语法: public event EventHandler Click示例Click事件的使用本示例实现效果为,当程序运行时,单击【ListView】按钮,将姓名显示在文本框中。示例运行结果如图3所示。图3 Click事件的使用程序主要代码如下:private void lvStudent_Click(object sender, EventArgs e)this.txtName.Text = lvS
30、tudent.SelectedItems0.SubItems1.Text;完整程序代码如下:主程序文件完整程序代码using System;using System.Collections.Generic;using System.Windows.Forms;namespace _8_07static class Program/ <summary>/ 应用程序的主入口点。/ </summary>STAThreadstatic void Main()Application.EnableVisualStyles();Application.SetCompatibleTex
31、tRenderingDefault(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_07public partial class Form1 : Formpublic Form1()Init
32、ializeComponent();Form1窗体代码文件完整程序代码namespace _8_07partial class Form1/ <summary>/ 必需的设计器变量。/ </summary>private System.ComponentModel.IContainer components = null;/ <summary>/ 清理所有正在使用的资源。/ </summary>/ <param name="disposing">如果应释放托管资源,为 true;否则为 false。</par
33、am>protected override void Dispose(bool disposing)if (disposing && (components != null)components.Dispose();base.Dispose(disposing);#region Windows 窗体设计器生成的代码/ <summary>/ 设计器支持所需的方法 - 不要/ 使用代码编辑器修改此方法的内容。/ </summary>private void InitializeComponent()ponents = new System.Compon
34、entModel.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.Drawing;using System.Text;using System.Windows.Form
35、s;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.Items.Clear();private void bntAdd_Click(object send
36、er, 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", con);SqlDataReader dr = com.ExecuteReader();
37、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.lvStudent.Alignment = ListViewAlignment.SnapToGrid;th
38、is.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_Click(object sender, EventArgs e)this.txtName.Text
39、 = lvStudent.SelectedItems0.SubItems1.Text;frmListView窗体代码文件完整程序代码namespace _8_07partial class frmListView/ <summary>/ 必需的设计器变量。/ </summary>private System.ComponentModel.IContainer components = null;/ <summary>/ 清理所有正在使用的资源。/ </summary>/ <param name="disposing"&g
40、t;如果应释放托管资源,为 true;否则为 false。</param>protected override void Dispose(bool disposing)if (disposing && (components != null)components.Dispose();base.Dispose(disposing);#region Windows 窗体设计器生成的代码/ <summary>/ 设计器支持所需的方法 - 不要/ 使用代码编辑器修改此方法的内容。/ </summary>private void InitializeC
41、omponent()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.bntAdd = new System.Windows.Forms.Button();this.b
42、ntDelete = 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.AddRange(new System.Windows.Forms.ColumnHeader this.c
43、olumnHeader1,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.lvStudent.UseCompatibleStateImageBehavior = false;this.lv
44、Student.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.columnHeader1.Text = "学号"this.columnHeader1.Width
45、= 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);this.bntAdd.Name = "bntAdd"this.bntAdd.Si
46、ze = 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 System.Drawing.Point(159, 171);this.bntDelete.Na
47、me = "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.UseVi
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 天然气管网监控系统设计方案
- 压铸生产车间布局优化方案
- 中介用工方合同范本
- 代言人拍摄合同范本
- 设备竣工验收合同范本
- st生物转让协议书
- 代理商期权合同范本
- 三星半导体合同范本
- 会所家具转让协议书
- AI智能在污水处理中的创新与应用
- 浙江省杭州市学军中学2025-2026学年高一10月月考语文试题(原卷版)
- 2025西南有色昆明勘测设计(院)股份有限公司专业技术人员招聘(9人)考试笔试备考试题及答案解析
- 完整版2025年一级建造师《公路实务》考试真题及答案解析
- 青年创业培训课件
- 超市散货干货知识培训
- DB37∕T 5156-2020 既有居住建筑加装电梯附属建筑工程技术标准
- 2025公安机关人民警察(高级)执法资格等级考试自测试题及答案
- 农村冬季防火消防知识培训
- 2025年度济南市工会社会工作专业人才联合招聘(47人)笔试参考题库附答案解析
- 【必背】三级政务服务办事员备考题库宝典-2025核心题版
- 《城市轨道交通 场站及周边土地综合开发评价》编制说明
评论
0/150
提交评论