C#中ListView控件應(yīng)用實(shí)例_第1頁
C#中ListView控件應(yīng)用實(shí)例_第2頁
C#中ListView控件應(yīng)用實(shí)例_第3頁
C#中ListView控件應(yīng)用實(shí)例_第4頁
C#中ListView控件應(yīng)用實(shí)例_第5頁
已閱讀5頁,還剩12頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、【精品文檔】如有侵權(quán),請聯(lián)系網(wǎng)站刪除,僅供學(xué)習(xí)與交流C#中ListView控件應(yīng)用實(shí)例.精品文檔.C#中ListView控件應(yīng)用實(shí)例ListView控件1  功能ListView控件可以顯示帶圖標(biāo)的項(xiàng)列表,用戶可使用該控件創(chuàng)建類似Windows資源管理器的用戶界面。ListView控件具有4種視圖模式:(1)僅文本,這是默認(rèn)視圖,此視圖下,只顯示列表項(xiàng)的文本;(2)帶有小圖標(biāo)的文本,此視圖下,小圖標(biāo)隨列表項(xiàng)的文本同時(shí)顯示;(3)帶有大圖標(biāo)的文本,此視圖下,大圖標(biāo)隨列表項(xiàng)的文本同時(shí)顯示;(4)報(bào)表視圖,此視圖下,列表項(xiàng)顯示在多個(gè)列中。圖1為List View控件。圖1  Li

2、stView 控件2屬性ListView控件常用屬性及說明如表1所示。表1    ListView控件常用屬性及說明下面對比較重要的屬性進(jìn)行詳細(xì)介紹。(1)View屬性。用于獲取或設(shè)置項(xiàng)在控件中的顯示方式。語法: public View View get; set; 屬性值:View值之一。默認(rèn)為LargeIcon。View的屬性值及說明如表2所示。表2      View的屬性值及說明(2)FullrowSelect屬性。用于指定是只選擇某一項(xiàng),還是選擇某一項(xiàng)所在的整行。語法: public

3、bool FullRowSelect get; set; 屬性值:如果單擊某項(xiàng)會(huì)選擇該項(xiàng)及其所有子項(xiàng),則為True;如果單擊某項(xiàng)僅選擇項(xiàng)本身,則為False。默認(rèn)為False。說明:除非將ListView控件的View屬性設(shè)置為Details,否則FullRowSelect屬性無效。在ListView顯示帶有許多子項(xiàng)的項(xiàng)時(shí),通常使用FullrowSelect屬性,并且,在由于控件內(nèi)容的水平滾動(dòng)而無法看到項(xiàng)文本時(shí),能夠查看選定項(xiàng)是非常重要的。(3)GridLines屬性。指定在包含控件中項(xiàng)及其子項(xiàng)的行和列之間是否顯示網(wǎng)格線。語法: public bool GridLines get;

4、set; 屬性值:如果在項(xiàng)及其子項(xiàng)的周圍繪制網(wǎng)格線,則為True;否則為False。默認(rèn)為False。說明:除非將ListView控件的View屬性設(shè)置為Details,否則GridLines屬性無效。示例FullrowSelect屬性本示例主要介紹View屬性和FullrowSelect屬性的使用方法,示例運(yùn)行結(jié)果如圖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>/ 應(yīng)用程序的主入口點(diǎn)。/ </summary>STAThreadstatic void Main()Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application

6、.Run(new frmListView();Form1窗體設(shè)計(jì)文件完整程序代碼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>/ 必需的設(shè)計(jì)器變量。/ </summary>private System.ComponentModel.IContainer components = null;/ <summary>/ 清理所有正在使用的資源。/ </summary>/ <param name="disposing">如果應(yīng)釋放托管資源,為 true;否則為 false。</param>protected override void Dispos

8、e(bool disposing)if (disposing && (components != null)components.Dispose();base.Dispose(disposing);#region Windows 窗體設(shè)計(jì)器生成的代碼/ <summary>/ 設(shè)計(jì)器支持所需的方法 - 不要/ 使用代碼編輯器修改此方法的內(nèi)容。/ </summary>private void InitializeComponent()ponents = new System.ComponentModel.Container();this.AutoScaleM

9、ode = System.Windows.Forms.AutoScaleMode.Font;this.Text = "Form1"#endregionfrmListView窗體設(shè)計(jì)文件完整程序代碼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>/ 必需的設(shè)計(jì)器變量。/ </summary>private System.ComponentModel.IContainer components = null;/ <summary>/ 清理所有正在使用的資源。/ </summary>/ <param name="disposing">如果應(yīng)釋放托管資源,為 true;否則為 false。</param>protected override void Dispose(bool disposing)if (di

15、sposing && (components != null)components.Dispose();base.Dispose(disposing);#region Windows 窗體設(shè)計(jì)器生成的代碼/ <summary>/ 設(shè)計(jì)器支持所需的方法 - 不要/ 使用代碼編輯器修改此方法的內(nèi)容。/ </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 = "學(xué)號(hào)"this.columnHeader1.Width = 97;/ columnHeader2/this.columnHeader2.Text = "學(xué)生姓名"this.columnHeader2.Width = 136;/

20、columnHeader3/this.columnHeader3.Text = "學(xué)生班級"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 = "學(xué)生姓名:"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坐標(biāo)的情況下,提供項(xiàng)信息。(2)Clear方法。該方法用于刪除ListView控件中所有的項(xiàng)。語法: public void

29、 Clear ()例如,下面刪除名稱為lvStudent的ListView控件的所有項(xiàng):lvStudent.Items.Clear();4事件(1)ItemCheck事件。該事件在選中ListView控件項(xiàng)時(shí)觸發(fā)此事件。(2)Click事件。該事件在單擊ListView控件列時(shí)觸發(fā)。語法: public event EventHandler Click示例Click事件的使用本示例實(shí)現(xiàn)效果為,當(dāng)程序運(yùn)行時(shí),單擊【ListView】按鈕,將姓名顯示在文本框中。示例運(yùn)行結(jié)果如圖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>/ 應(yīng)用程序的主入口點(diǎn)。/ </summary>STAThreadstatic void Main()Application.

31、EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(new frmListView();Form1窗體設(shè)計(jì)文件完整程序代碼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>/ 必需的設(shè)計(jì)器變量。/ </summary>private System.ComponentModel.IContainer components = null;/ <summary>/ 清理所有正在使用的資源。/ </summary>/ <param name="dispo

33、sing">如果應(yīng)釋放托管資源,為 true;否則為 false。</param>protected override void Dispose(bool disposing)if (disposing && (components != null)components.Dispose();base.Dispose(disposing);#region Windows 窗體設(shè)計(jì)器生成的代碼/ <summary>/ 設(shè)計(jì)器支持所需的方法 - 不要/ 使用代碼編輯器修改此方法的內(nèi)容。/ </summary>private void

34、 InitializeComponent()ponents = new System.ComponentModel.Container();this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;this.Text = "Form1"#endregionfrmListView窗體設(shè)計(jì)文件完整程序代碼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>/ 必需的設(shè)計(jì)器變量。/ </summary>private System.ComponentModel.IContainer components = null;/ <summary>/ 清理所有正在使用的資源。/ </su

40、mmary>/ <param name="disposing">如果應(yīng)釋放托管資源,為 true;否則為 false。</param>protected override void Dispose(bool disposing)if (disposing && (components != null)components.Dispose();base.Dispose(disposing);#region Windows 窗體設(shè)計(jì)器生成的代碼/ <summary>/ 設(shè)計(jì)器支持所需的方法 - 不要/ 使用代碼編輯器修改

41、此方法的內(nèi)容。/ </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 = "學(xué)號(hào)"this.columnHeader1.Width = 97;/ columnHeader2/this.columnHeader2.Text = "學(xué)生姓名"this.columnHeader2.Width = 136;/ columnHeader3/this.columnHeader3.Text = "學(xué)生班級"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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論