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

下载本文档

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

文档简介

C#中ListView控件应用实例ListView控件1 功能ListView控件可以显示带图标的项列表,用户可使用该控件创建类似Windows资源管理器的用户界面。ListView控件具有4种视图模式:(1)仅文本,这是默认视图,此视图下,只显示列表项的文本;(2)带有小图标的文本,此视图下,小图标随列表项的文本同时显示;(3)带有大图标的文本,此视图下,大图标随列表项的文本同时显示;(4)报表视图,此视图下,列表项显示在多个列中。图1为List View控件。图1 ListView 控件2属性ListView控件常用属性及说明如表1所示。表1 ListView控件常用属性及说明下面对比较重要的属性进行详细介绍。(1)View属性。用于获取或设置项在控件中的显示方式。语法:public View View get; set; 属性值:View值之一。默认为LargeIcon。View的属性值及说明如表2所示。表2 View的属性值及说明(2)FullrowSelect属性。用于指定是只选择某一项,还是选择某一项所在的整行。语法:public bool FullRowSelect get; set; 属性值:如果单击某项会选择该项及其所有子项,则为True;如果单击某项仅选择项本身,则为False。默认为False。说明:除非将ListView控件的View属性设置为Details,否则FullRowSelect属性无效。在ListView显示带有许多子项的项时,通常使用FullrowSelect属性,并且,在由于控件内容的水平滚动而无法看到项文本时,能够查看选定项是非常重要的。(3)GridLines属性。指定在包含控件中项及其子项的行和列之间是否显示网格线。语法:public bool GridLines 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;完整程序代码如下:主程序文件完整程序代码using System;using System.Collections.Generic;using System.Windows.Forms;namespace _8_07static class Program/ / 应用程序的主入口点。/ STAThreadstatic void Main()Application.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_07public partial class Form1 : Formpublic Form1()InitializeComponent();Form1窗体代码文件完整程序代码namespace _8_07partial class Form1/ / 必需的设计器变量。/ private System.ComponentModel.IContainer components = null;/ / 清理所有正在使用的资源。/ / 如果应释放托管资源,为 true;否则为 tected override void Dispose(bool disposing)if (disposing & (components != null)components.Dispose();base.Dispose(disposing);#region Windows 窗体设计器生成的代码/ / 设计器支持所需的方法 - 不要/ 使用代码编辑器修改此方法的内容。/ private void 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.Drawing;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.Items.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, 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.lvStudent.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_Click(object sender, EventArgs e)frmListView窗体代码文件完整程序代码namespace _8_07partial class frmListView/ / 必需的设计器变量。/ private System.ComponentModel.IContainer components = null;/ / 清理所有正在使用的资源。/ / 如果应释放托管资源,为 true;否则为 tected override void Dispose(bool disposing)if (disposing & (components != null)components.Dispose();base.Dispose(disposing);#region Windows 窗体设计器生成的代码/ / 设计器支持所需的方法 - 不要/ 使用代码编辑器修改此方法的内容。/ 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.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.AddRange(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.lvStudent.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.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 = 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;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);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.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.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.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.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 += new System.EventHandler(this.frmListView_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 columnHeader1;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 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(object 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/ / 应用程序的主入口点。/ STAThreadstatic void Main()Application.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_07public partial class Form1 : Formpublic Form1()InitializeComponent();Form1窗体代码文件完整程序代码namespace _8_07partial class Form1/ / 必需的设计器变量。/ private System.ComponentModel.IContainer components = null;/ / 清理所有正在使用的资源。/ / 如果应释放托管资源,为 true;否则为 tected override void Dispose(bool disposing)if (disposing & (components != null)components.Dispose();base.Dispose(disposing);#region Windows 窗体设计器生成的代码/ / 设计器支持所需的方法 - 不要/ 使用代码编辑器修改此方法的内容。/ private void 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.Drawing;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.Items.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, 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.lvStudent.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_Click(object sender, EventArgs e)this.txtName.Text = lvStudent.SelectedItems0.SubItems1.Text;frmListView窗体代码文件完整程序代码namespace _8_07partial class frmListView/ / 必需的设计器变量。/ private System.ComponentModel.IContainer components = null;/ / 清理所有正在使用的资源。/ / 如果应释放托管资源,为 true;否则为 tected override void Dispose(bool disposing)if (disposing & (components != null)components.Dispose();base.Dispose(disposing);#region Windows 窗体设计器生成的代码/ / 设计器支持所需的方法 - 不要/ 使用代码编辑器修改此方法的内容。/ 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.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.AddRange(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.lvStudent.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.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 = 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;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);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.UseVisualStyleBackColor = true;this.bntEsce.Click += new System.Event

温馨提示

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

评论

0/150

提交评论