当前位置:文档之家› C#中ListView控件应用实例

C#中ListView控件应用实例

C#中 ListView 控件应用实例ListView 控件 1. 功能 ListView 控件可以显示带图标的项列表,用户可使用该控件创建类似 Windows 资源管理器的用户界面。

ListView 控件具有 4 种视图模式: (1)仅文本,这是默认视图,此视图下,只显示列表项的文本; (2)带有小图标的文本,此视图下,小图标随列表项的文本同时显示; (3)带有大图标的文本,此视图下,大图标随列表项的文本同时显示; (4)报表视图,此视图下,列表项显示在多个列中。

图 1 为 List View 控件。

图1ListView 控件2.属性 ListView 控件常用属性及说明如表 1 所示。

表1ListView 控件常用属性及说明下面对比较重要的属性进行详细介绍。

(1)View 属性。

用于获取或设置项在控件中的显示方式。

语法: public View View { get; set; } 属性值:View 值之一。

默认为 LargeIcon。

View 的属性值及说明如表 2 所示。

表2View 的属性值及说明(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_07 { static class Program { /// <summary> /// 应用程序的主入口点。

/// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new frmListView()); } } } ★★★★★Form1 窗体设计文件完整程序代码★★★★★ using using using using using System; System.Collections.Generic; ponentModel; System.Data; System.Drawing;using System.Text; using System.Windows.Forms; namespace _8_07 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } } } ★★★★★Form1 窗体代码文件完整程序代码★★★★★ namespace _8_07 { partial class Form1 { /// <summary> /// 必需的设计器变量。

/// </summary> private ponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。

/// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。

</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。

/// </summary> private void InitializeComponent() { ponents = new ponentModel.Container(); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;this.Text = "Form1"; } #endregion } } ★★★★★frmListView 窗体设计文件完整程序代码★★★★★ using System; using System.Collections.Generic; using ponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace _8_07 { public partial class frmListView : Form { public 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 EventArgs e) { } private void lvStudent_Click(object sender, EventArgs e) { } } } ★★★★★frmListView 窗体代码文件完整程序代码★★★★★sender,namespace _8_07 { partial class frmListView { /// <summary> /// 必需的设计器变量。

/// </summary> private ponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。

/// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。

</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose();} base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。

相关主题