




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、【精品文檔】如有侵權,請聯系網站刪除,僅供學習與交流C#中ListView控件應用實例.精品文檔.C#中ListView控件應用實例ListView控件1 功能ListView控件可以顯示帶圖標的項列表,用戶可使用該控件創建類似Windows資源管理器的用戶界面。ListView控件具有4種視圖模式:(1)僅文本,這是默認視圖,此視圖下,只顯示列表項的文本;(2)帶有小圖標的文本,此視圖下,小圖標隨列表項的文本同時顯示;(3)帶有大圖標的文本,此視圖下,大圖標隨列表項的文本同時顯示;(4)報表視圖,此視圖下,列表項顯示在多個列中。圖1為List View控件。圖1 Li
2、stView 控件2屬性ListView控件常用屬性及說明如表1所示。表1 ListView控件常用屬性及說明下面對比較重要的屬性進行詳細介紹。(1)View屬性。用于獲取或設置項在控件中的顯示方式。語法: public View View get; set; 屬性值:View值之一。默認為LargeIcon。View的屬性值及說明如表2所示。表2 View的屬性值及說明(2)FullrowSelect屬性。用于指定是只選擇某一項,還是選擇某一項所在的整行。語法: public
3、bool FullRowSelect get; set; 屬性值:如果單擊某項會選擇該項及其所有子項,則為True;如果單擊某項僅選擇項本身,則為False。默認為False。說明:除非將ListView控件的View屬性設置為Details,否則FullRowSelect屬性無效。在ListView顯示帶有許多子項的項時,通常使用FullrowSelect屬性,并且,在由于控件內容的水平滾動而無法看到項文本時,能夠查看選定項是非常重要的。(3)GridLines屬性。指定在包含控件中項及其子項的行和列之間是否顯示網格線。語法: public bool GridLines get;
4、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
5、;完整程序代碼如下:主程序文件完整程序代碼using System;using System.Collections.Generic;using System.Windows.Forms;namespace _8_07static class Program/ <summary>/ 應用程序的主入口點。/ </summary>STAThreadstatic void Main()Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application
6、.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窗體代碼文件完整程序代碼n
7、amespace _8_07partial class Form1/ <summary>/ 必需的設計器變量。/ </summary>private System.ComponentModel.IContainer components = null;/ <summary>/ 清理所有正在使用的資源。/ </summary>/ <param name="disposing">如果應釋放托管資源,為 true;否則為 false。</param>protected override void Dispos
8、e(bool disposing)if (disposing && (components != null)components.Dispose();base.Dispose(disposing);#region Windows 窗體設計器生成的代碼/ <summary>/ 設計器支持所需的方法 - 不要/ 使用代碼編輯器修改此方法的內容。/ </summary>private void InitializeComponent()ponents = new System.ComponentModel.Container();this.AutoScaleM
9、ode = 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;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>/ 必需的設計器變量。/ </summary>private System.ComponentModel.IContainer components = null;/ <summary>/ 清理所有正在使用的資源。/ </summary>/ <param name="disposing">如果應釋放托管資源,為 true;否則為 false。</param>protected override void Dispose(bool disposing)if (di
15、sposing && (components != null)components.Dispose();base.Dispose(disposing);#region Windows 窗體設計器生成的代碼/ <summary>/ 設計器支持所需的方法 - 不要/ 使用代碼編輯器修改此方法的內容。/ </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 = "學號"this.columnHeader1.Width = 97;/ columnHeader2/this.columnHeader2.Text = "學生姓名"this.columnHeader2.Width = 136;/
20、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
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 = "學生姓名:"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坐標的情況下,提供項信息。(2)Clear方法。該方法用于刪除ListView控件中所有的項。語法: public void
29、 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(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>/ 應用程序的主入口點。/ </summary>STAThreadstatic void Main()Application.
31、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_07pu
32、blic partial class Form1 : Formpublic Form1()InitializeComponent();Form1窗體代碼文件完整程序代碼namespace _8_07partial class Form1/ <summary>/ 必需的設計器變量。/ </summary>private System.ComponentModel.IContainer components = null;/ <summary>/ 清理所有正在使用的資源。/ </summary>/ <param name="dispo
33、sing">如果應釋放托管資源,為 true;否則為 false。</param>protected override void Dispose(bool disposing)if (disposing && (components != null)components.Dispose();base.Dispose(disposing);#region Windows 窗體設計器生成的代碼/ <summary>/ 設計器支持所需的方法 - 不要/ 使用代碼編輯器修改此方法的內容。/ </summary>private void
34、 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.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>/ 必需的設計器變量。/ </summary>private System.ComponentModel.IContainer components = null;/ <summary>/ 清理所有正在使用的資源。/ </su
40、mmary>/ <param name="disposing">如果應釋放托管資源,為 true;否則為 false。</param>protected override void Dispose(bool disposing)if (disposing && (components != null)components.Dispose();base.Dispose(disposing);#region Windows 窗體設計器生成的代碼/ <summary>/ 設計器支持所需的方法 - 不要/ 使用代碼編輯器修改
41、此方法的內容。/ </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 = "學號"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);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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 超長信用債探微跟蹤:超長信用的韌性撐多久
- 如何科學護理老年認知障礙
- 針對工業互聯網平臺的2025年入侵檢測系統:異常行為分析與優化實踐報告
- 智能化升級改造對城市污水處理廠設備壽命影響研究報告
- 2025年農業機械化智能化發展中的農業產業結構優化與升級報告
- 2025年餐飲業會員營銷活動效果評估與客戶忠誠度增長報告
- 2025年“健康中國”戰略下醫療健康產業投資策略與風險控制研究報告
- 量子計算技術在金融風險模擬中的大數據分析與風險管理研究報告
- 數字孿生在城市公共空間規劃中的互動體驗設計報告
- 2025細胞治療臨床試驗與審批流程中的臨床試驗倫理審查倫理學案例報告
- 防汛防雷安全培訓
- 2024版壓力容器設計審核機考題庫-簡答題3-3
- 2025-2030國內天然橡膠行業深度分析及競爭格局與發展前景預測研究報告
- 四年級2025年小學語文下學期期末考試真題人教版
- 西安美術統考考題及答案
- 2024年東莞市“百萬英才匯南粵行動計劃”事業編制教師招聘筆試真題
- DB43T-湖南省改性玻化微珠復合材料外墻修繕系統應用技術標準
- 產品質量檢驗方法
- 直播帶貨主播培訓課程
- 放射治療擺位技術
- 2025年上半年云南普洱市事業單位招聘易考易錯模擬試題(共500題)試卷后附參考答案
評論
0/150
提交評論