C#中ListView控件应用实例_第1页
C#中ListView控件应用实例_第2页
C#中ListView控件应用实例_第3页
C#中ListView控件应用实例_第4页
C#中ListView控件应用实例_第5页
已阅读5页,还剩35页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、C#中Listview控件应用实例ListView 控件1. 功能ListView控件可以显示带图标的项列表,用户可使用该控件创建类似Windows资源管理器的用户界面。ListView控件具有4种视图模式:(1)仅文本,这是默认视图,此视图下,只显示列表项的文本;(2)带有小图标的文本,此视图下,小图标随列表项的文本同时显示;(3)带有大图标的文本,此视图下,大图标随列表项的文本同时显示;(4)报表视图,此视图下,列表项显示在多个列中。图1为List View控件。八 ListVi ew图1 ListView控件2 属性ListView控件常用属性及说明如表1所示属性说明FullfowSkc

2、t比属性用于指定在件中单击某匝时宴执行的操作过程.单 击某项时,可射旨定杲貝选择该亟还杲应选後项所在的整行View此屋性指左舟建列表视图的类型,视图粪型主要包括:尢图标、卜图 标、列表和详细信息Alignment扌淀口ginv各项的对齐方式Sorting对项进行搓序的方式Xfultis此屋性设肯T剧,表示在控件中一次可隈选舞多个项GrdLines欝髒鬆針,滾值斎衽包达件中爾刪鯛丹表1ListView控件常用属性及说明F面对比较重要的属性进行详细介绍(1)View属性。用于获取或设置项在控件中的显示方式语法:public View View get; set; 属性值:View值之一。默认为La

3、rgelcon。View的属性值及说明如表2所示。屋性值说明Detail?每牛项显丁在不同的行上,并带有关于列中所排列的各顷的进一歩信 息.最左辺的列包含一个T涸标和标籤后面闌列包含应用程序指定的 子项.列显吓一个标头,它可以昼乔列的标题.用户可以在运行时调整 各列的大小Largflcon每个项都显乔沟一个最大化圈乐在它的下面带一个标签StnaUIcon每个项都显示沟一仆甩标,在它前右辺带一个标签List訂專翳彖林图桂在它的右端鞋i締酣弼m表2View的属性值及说明(2) FullrowSelect属性。用于指定是只选择某一项,还是选择某一项所在的整语法:public bool FullRow

4、Select get; set; iI属性值:如果单击某项会选择该项及其所有子项,则为True;如果单击某项仅选择项本身,则为Falsa默认为Falseo说明:除非将ListView控件的View属性设置为Details,否则FullRowSelect属性无效。在ListView显示带有许多子项的项时,通常使用 FullrowSelect属性, 并且,在由于控件容的水平滚动而无法看到项文本时, 能够查看选定项是非常重 要的。(3) GridLines属性。指定在包含控件中项及其子项的行和列之间是否显示网格 线。语法:public bool GridL ines get; set; 属性值:如果

5、在项及其子项的周围绘制网格线,则为True;否则为Falsa默认为Falsa说明:除非将ListView控件的View属性设置为Details,否则GridLines属性 无效。示例FullrowSelect 属性本示例主要介绍View属性和FullrowSelect属性的使用方法,示例运行结果 如图2所示。图 2 FullrowSelect 属性程序主要代码如下:this.IvStude nt.View = View.Details;this.IvStude nt.FullRowSelect = True;this .lv Stude nt.GridL ines = True;完整程序代码如

6、下:主程序文件完整程序代using System;using System.Collectio ns.Ge neric;:!using System.Wi ndows.Forms;n amespace _8_07istatic class ProgramiIII III应用程序的主入口点。iIII STAThreadistatic void Mai n()!Applicatio n.E nableVisualStyles();Applicatio n.SetCompatibleTextRe nderi ngDefault(false);!Applicatio n.Run(new frmList

7、View();III Form1窗体设计文件完整程序代码using System;using System.Collectio ns.Ge neric;jusing System.Comp onen tModel;using System.Data;using System.Drawi ng;using System.Text;using System.Wi ndows.Forms;in amespace _8_07ipublic partial class Form1 : Formpublic Form1()IiIn itializeComp onen t();ii Forml窗体代码文件完

8、整程序代码n amespace _8_07ipartial class Forml!i/ !i/必需的设计器变量。iI/ private System.Comp onen tModel .ICon tai ner comp onents = nu II;/ /清理所有正在使用的资源。f!/ / 如果应释放托管资源,为true;否则为false。I1protected override void Dispose(bool dispos ing)Iiiiiif (dispos ing & (comp onents != nu II)comp onen ts.Dispose();base.Dispo

9、se(dispos in g);#region Win dows窗体设计器生成的代码III / private void In itializeComp onen t()p onents = new System.Comp onen tModel.Co ntai ner();this.AutoScaleMode = System.Wi ndows.Forms.AutoScaleMode.F ont; this.Text = Form1;#en dregi on/设计器支持所需的方法-不要III使用代码编辑器修改此方法的容。 frmListView窗体设计文件完整程序代码厂1iu

10、sing System;using System.Collectio ns.Ge neric;using System.Comp onen tModel;using System.Data;using System.Drawi ng;using System.Text;using System.Wi ndows.Forms;using System.Data.SqlClie nt;n amespace _8_07public partial class frmListView : Formpublic frmListView()In itializeComp onen t();private

11、void frmListView_Load(object sender, EventArgs e)private void bn tDelete_Click(object sen der, Eve ntArgs e)lvStude nt.ltems.Clear();private void bntAdd_Click(object sender, EventArgs e)ithis.IvStude nt.View = View.Details;jthis.IvStude nt.FullRowSelect = true;newiSqlC onnectioncon=SqlC onn ectio n(

12、 server=(local);uid=sa;pwd=;database=zhy);con. Ope n();ISqlComma nd = new SqlComma nd(select * from stude nt, con);SqlDataReader dr = .ExecuteReader();Iithis.lvStude nt.ltems.Clear();while (dr.Read()!ListViewItem lt = new ListViewltem(dr.GetValue(O).ToStri ng();ijlt.Subltems.Add(dr.GetValue(1).ToStr

13、i ng();lt.Subltems.Add(dr.GetValue (2).ToStri ng();this.lvStude nt.Items.Add(lt);!dr.Close();jcon .Close();jithis.lvStude nt.Alig nment = ListViewAlig nmen t.S napToGrid;this .lv Stude nt.GridL ines = true;private void bn tEsce_Click(object sen der. Eve ntArgs e)IiiApplicatio n.Exit();iprivate void

14、label1_Click(object sen der, Eve ntArgs e)IIprivate void lvStude nt_Selected In dexCha nged(object sen der, Even tArgs e)!private void lvStudent_Click(object sender, EventArgs e)i frmListView窗体代码文件完整程序代码:in amespace _8_07Ipartial class frmListView1III Ill必需的设计器变量。III v/summaryprivate System.Comp one

15、n tModel .ICon tai ner comp onents = nu II;Ill /清理所有正在使用的资源。为true;否则为falseoIII III vparam name=disposi ng 如果应释放托管资源,protected override void Dispose(bool dispos ing)if (dispos ing & (comp onents != nu II)comp onen ts.Dispose();base.Dispose(dispos in g);#region Win dows窗体设计器生成的代码III III设计器支持所需的方法-不要II

16、I使用代码编辑器修改此方法的容。III private void In itializeComp onen t()this.IvStude nt = new System.Wi ndows.Forms.ListView();this.colu mnH eader1 = new System.Wi ndows.Forms.Colu mnH eader();this.colu mnH eader2 = new System.Wi ndows.Forms.Colu mnH eader();this.colu mnH eader3 = new System.Wi ndows.Forms.Colu mn

17、H eader();this.b ntAdd = new System.Wi ndows.Forms.Butt on();this.b ntDelete = new System.Wi ndows.Forms.Butt on();this.b ntEsce = new System.Wi ndows.Forms.Butt on();this.txtName = new System.Wi ndows.Forms.TextBox();this.label1 = new System.Wi ndows.Forms.Label();this.Suspe ndLayout();/ lvStude nt

18、/this .lv Stude nt.Colu mn s.AddRa nge( newSystem.Wi ndows.Forms.Colu mnH eader this.colu mnH eader1,this.colu mnH eader2,this.colu mnH eader3);this .lv Stude nt.Locati on = new System.Drawi ng.Poi nt(26, 32);this.IvStude nt.Name = IvStude nt;this.IvStude nt.Size = new System.Drawi ng.Size(352, 114)

19、;this.IvStude nt.Tabl ndex = 0;this .lv Stude nt.UseCompatibleStatelmageBehavior = false;this.lvStude nt.View = System.Wi ndows.Forms.View.Details;this.lvStude nt.Selectedl ndexCha nged+=newSystem.Eve ntHa ndler(this.lvStude nt_SelectedI ndexCha nged);this.lvStude nt.Click += new System.Eve ntHa ndl

20、er(this.lvStude nt_Click);/ colu mnH eader1/ this.columnHeader1.Text =学号; this.colum nH eader1.Width = 97;/ colu mnH eader2/this.columnHeader2.Text =学生姓名this.colum nH eader2.Width = 136;/ colu mnH eader3/this.columnHeader3.Text =学生班级;this.colum nH eader3.Width = 118;/ bn tAdd/this.b ntAdd Location =

21、 new System.Draw in g.Poi nt(26, 171);this.b ntAdd.Name = b ntAdd;this.b ntAdd.Size = new System.Drawi ng.Size(75, 23);this.b ntAdd.Tabl ndex = 2;this.b ntAdd.Text =加截(&F);this.b ntAdd.UseVisualStyleBackColor = true;this.b ntAdd.Click += new System.Eve ntHa ndler(this.b ntAdd_Click);/ bn tDelete/thi

22、s.bntDelete.Location = new System.Drawing.Point(159, 171);this.b ntDelete.Name = bn tDelete;this.bntDelete.Size = new System.Drawing.Size(75, 23);this.b ntDelete.Tab In dex = 3;this.b ntDelete.Text =清除(&G);this.b ntDelete.UseVisualStyleBackColor = true;this.b ntDelete.Click += new System.Eve ntHa nd

23、ler(this.b ntDelete_Click);/ bn tEsce/this.b ntEsce Location = new System.Drawi ng.Poi nt(303, 171);this.b ntEsce.Name = bn tEsce;this.b ntEsce.Size = new System.Drawi ng.Size(75, 23);this.b ntEsce.Tab In dex = 4;this.bntEsce.Text =退出(&T);this.b ntEsce.UseVisualStyleBackColor = true;this.bntEsce.Cli

24、ck += 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.Drawing.Size(116, 21);this.txtName.Tab In dex = 5;/ label1this .1 abell.AutoSize = true;ithis.labell.Locati on = new Syste

25、m.Drawi ng.Poi nt(73, 212);!ithis.label1.Name = label1;ithis.label1.Size = new System.Drawi ng.Size(59, 12);this.label1.Tabl ndex = 6;this.label1.Text =学生姓名:;Ithis.label1.Click += new System.Eve ntHa ndler(this.label1_Click);/ frmListView/this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);

26、!this.AutoScaleMode = System.Wi ndows.Forms.AutoScaleMode.F ont;iithis.Clie ntSize = new System.Drawi ng.Size(418, 266);this.C on trols.Add(this.label1);this.Co ntrols.Add(this.txtName);this.C on trols.Add(this.b ntEsce);!this .Controls .Add(this .bntDelete);ithis.C on trols.Add(this.b ntAdd);jithis

27、.Co ntrols.Add(this.lvStude nt);this.Name = frmListView;this.Text = frmListView;Ithis.Load += new System.EventHandler(this.frmListView_Load);Ithis .ResumeLayout(false);!ithis .PerformLayout();#en dregi onIprivate System.Wi ndows.Forms.ListView lvStude nt;iiprivate System.Wi ndows.Forms.Butt on bn tA

28、dd;private System.Wi ndows.Forms.Butt on bn tDelete;private System.Wi ndows.Forms.Butt on bn tEsce;!private Syste m. Windows .Fo rms .ColumnHeader columnHeader1;i!private Syste m. Windows .Fo rms .ColumnHeader columnHeader2;iprivate System.Wi ndows.Forms.Colu mnH eader colu mnH eader3;private System

29、.Wi ndows.Forms.TextBox txtName;private System.Wi ndows.Forms.Label Iabel1;I3方法(1) HitTest方法。该方法在给定x和y坐标的情况下,提供项信息(2) Clear方法。该方法用于删除ListView控件中所有的项。语法:IvStude nt.ltems.Clear();4事件(1) ItemCheck事件。该事件在选中ListView控件项时触发此事件。(2) Click事件。该事件在单击ListView控件列时触发。语法:1 *8寸“ I” 別 I M I *111! F IFIF BaJII IR ImJ

30、|H MJHI ” M| M ” d |* * 41 卩A| I * J | : oA| 111, *| .】*| &” fcoBB 冃 ” 181|jpublic eve nt Eve ntHa ndler Click示例Click事件的使用本示例实现效果为,当程序运行时,单击【ListView】按钮,将姓名显示在 文本框中。示例运行结果如图3所示。图3 Click事件的使用程序主要代码如下:private void lvStude nt_Click(object sen der, Eve ntArgs e)IiIthis.txtName.Text = lvStude nt.Selected

31、ltems0.Subltems1.Text;完整程序代码如下:主程序文件完整程序代码using System;using System.Collectio ns.Ge neric;using System.Wi ndows.Forms;n amespace _8_07static class Program/ /应用程序的主入口点。/ STAThreadstatic void Main()Applicatio n.En ableVisualStyles();Applicatio n.SetCompatibleTextRe nderi ngDefault(false);Applicatio n.

32、Run(new frmListView(); Form1窗体设计文件完整程序代码 using System;using System.Collectio ns.Ge neric;using System.Comp onen tModel;using System.Data;using System.Drawi ng;using System.Text;using System.Wi ndows.Forms;n amespace _8_07public partial class Form1 : Formpublic Form1()In itializeComp onen t(); Form1窗

33、体代码文件完整程序代码 厂Iin amespace _8_07partial class Form1i / iiIll必需的设计器变量。iiIll v/summaryprivate Syste m. ComponentModel .1 Container components = null;/ /清理所有正在使用的资源。/ iij/ vparam name=disposing 如果应释放托管资源,为true;否则为tected override void Dispose(bool dispos ing)!i!iiif (dispos ing & (comp onents !=

34、 nu II)comp onen ts.Dispose();!Ibase .Dispose(disposing);ijij#region Win dows窗体设计器生成的代码/ /设计器支持所需的方法-不要Ill使用代码编辑器修改此方法的容。iIll v/summaryprivate void In itializeComp onen t()p onents = new System.Comp onen tModel.Co ntai ner();!this.AutoScaleMode = System.Wi ndows.Forms.AutoScaleMode.F ont;t

35、his.Text = Form1;ii#en dregi on!ii frmListView窗体设计文件完整程序代码 using System;using System.Collectio ns.Ge neric;Iusing System.Comp onen tModel;iusing System.Data;using System.Drawi ng;using System.Text;jIusing System.Wi ndows.Forms;iusing System.Data.SqlClie nt;n amespace _8_07iipublic partial class frmL

36、istView : Formiii!iipublic frmListView()In itializeComp onen t();|iprivate void frmListView_Load(object sen der, Eve ntArgs e)iiiIprivate void bn tDelete_Click(object sen der, Eve ntArgs e)iII!iilvStude nt.ltems.Clear();private void bntAdd_Click(object sender, EventArgs e)!Ithis .IvStudent .View = V

37、iew .Details;!iithis .IvStudent .F ullRowSelect = true;iiijSqlC onn ectio ncon=newSqlC onn ectio n( server=(local);uid=sa;pwd=;database=zhy);con. Ope n();SqlComma nd = new SqlComma nd(select * from stude nt, con);SqlDataReader dr = .ExecuteReader();this.lvStude nt.ltems.Clear();while (dr.Read()ListV

38、iewItem lt = new ListViewltem(dr.GetValue(0).ToStri ng();lt.Subltems.Add(dr.GetValue(1).ToStri ng();lt.Subltems.Add(dr.GetValue (2).ToStri ng();this.lvStude nt.Items.Add(lt);dr.Close();con .Close();this.lvStude nt.Alig nment = ListViewAlig nmen t.S napToGrid;this .lv Stude nt.GridL ines = true;priva

39、te void bn tEsce_Click(object sen der, Eve ntArgs e)Applicatio n.Exit();private void label1_Click(object sen der, Eve ntArgs e)private void IvStude nt_Selected In dexCha nged(object sen der. Even tArgs e) I_iiiprivate void lvStude nt_Click(object sen der, Eve ntArgs e)this.txtName.Text = lvStude nt.

40、Selectedltems0.Subltems1.Text;ijI frmListView窗体代码文件完整程序代码n amespace _8_07:!partial class frmListViewiIII /必需的设计器变量。III iprivate Syste m. ComponentModel .1 Container components = null;ii/ /清理所有正在使用的资源。i/ v/summaryfalse。/ vparam name=disposing 如果应释放托管资源,为true;否则为v/paramprotected override void Dispose(

41、bool dispos ing)if (dispos ing & (comp onents != n ull)comp onen ts.Dispose();base.Dispose(dispos in g);#region Win dows窗体设计器生成的代码/ /设计器支持所需的方法-不要/使用代码编辑器修改此方法的容。/ private void In itializeComp onen t()this.lvStude nt = new System.Wi ndows.Forms.ListView();this.colu mnH eader1 = new System.Wi ndows.F

42、orms.Colu mnH eader();this.colu mnH eader2 = new System.Wi ndows.Forms.Colu mnH eader();this.colu mnH eader3 = new System.Wi ndows.Forms.Colu mnH eader();ithis.b ntAdd = new System.Wi ndows.Forms.Butt on();!ithis.b ntDelete = new System.Wi ndows.Forms.Butt on();ithis.b ntEsce = new System.Wi ndows.F

43、orms.Butt on();this.txtName = new System.Wi ndows.Forms.TextBox();this.label1 = new System.Wi ndows.Forms.Label();Ithis.Suspe ndLayout();/ lvStude nt/this .lv Stude nt.Colu mn s.AddRa nge( new!System.Wi ndows.Forms.Colu mnH eader iithis.colu mnH eader1,this.colu mnH eader2,this.colu mnH eader3);this

44、.lvStudent.Location = new System.Drawing.Point(26, 32);!this .IvStudent .Name = lvStudent;!ithis .IvStudent .Size = new Syste m. Drawing .Size(352, 114);jithis.lvStude nt.TabI ndex = 0;this.lvStude nt.UseCompatibleStatelmageBehavior = false;this.lvStude nt.View = System.Wi ndows.Forms.View.Details;t

45、his.IvStude nt.Selectedl ndexCha nged+=newiSystem.Eve ntHa ndIer(this.IvStude nt_SeIectedI ndexCha nged);!ithis.IvStude nt.Click += new System.Eve ntHa ndler(this.lvStude nt_Click);ii/ colu mnH eader1/Ithis.columnHeader1.Text =学号;this.colum nH eader1.Width = 97;ij/ colu mnH eader2/!this.columnHeader

46、2.Text =学生姓名;iithis.colum nH eader2.Width = 136;/ colu mnH eader3/!this.columnHeader3.Text =学生班级;!jthis.colum nH eader3.Width = 118;j/ bn tAdd/this.b ntAdd. Location = new System.Draw in g.Poi nt(26, 171);this.b ntAdd.Name = b ntAdd;!ithis.b ntAdd.Size = new System.Drawi ng.Size(75, 23);iithis.b ntA

47、dd.Tabl ndex = 2;this.b ntAdd.Text =加截(&F);this.b ntAdd.UseVisualStyleBackColor = true;Ithis.b ntAdd.Click += new System.Eve ntHa ndler(this.b ntAdd_Click);/ bn tDelete/this.bntDelete.Location = new System.Drawing.Point(159, 171);!this.b ntDelete.Name = bn tDelete;iithis.bntDelete.Size = new System.

48、Drawing.Size(75, 23);this.b ntDelete.Tab In dex = 3;this.b ntDelete.Text =清除(&G);this.b ntDelete.UseVisualStyleBackColor = true;!this.b ntDelete.Click += new System.Eve ntHa ndler(this.b ntDelete_Click);/ 一ii/ bn tEsce/this.bntEsce.Location = new System.Drawing.Point(303, 171);this .bntEsce .N ame = bntEsce;this .bntEsce .Size = new Syste m. Drawing .Size(75, 23);!ithis .bntEsce .Tabindex = 4;this.bntEsce.Text =退出(&T);this.b ntEsce.UseVisualSty

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论