C#中教你一步步实现一个电话本窗体程序
生活随笔
收集整理的這篇文章主要介紹了
C#中教你一步步实现一个电话本窗体程序
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
場景
使用C#窗體程序?qū)崿F(xiàn)一個電話本應(yīng)用程序。
實現(xiàn)
新建窗體程序,PhoneBook
?
然后打開工具箱,拖拽一個ListView到窗體中
拖拽完成后點擊右上角三角符號,編輯選項--編輯列。
?
點擊添加,修改每列的Name屬性和顯示text
?
添加三列,分別為姓名、類型、號碼,適當(dāng)調(diào)整每列的寬度。
?
然后將其視圖切換為Details然后就能顯示每列了。
?
拖拽完ListView后再在右邊拖拽一些label、TextBox、Button等,并設(shè)置有意義的Name屬性和Text顯示。
?
右鍵項目--添加-類,添加聯(lián)系人實體類
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace PhoneBook {class PhoneBook{public PhoneBook(string name, string phoneType, string phoneNumber){this.Name = name;this.PhoneType = phoneType;this.PhoneNumber = phoneNumber;}private string name;public string Name{get { return name; }set { name = value; }}private string phoneType;public string PhoneType{get { return phoneType; }set { phoneType = value; }}private string phoneNumber;public string PhoneNumber{get { return phoneNumber; }set { phoneNumber = value; }}} }聯(lián)系人加載
首先聲明一個全局類變量存儲聯(lián)系人。
?//聲明一個全局 類變量 電話本鍵值對 key為string,value為聯(lián)系人對象Dictionary<string,PhoneBook> phoneBooks;?
然后在Form的加載完的方法中對ListView進行賦值
?private void MainForm_Load(object sender, EventArgs e){phoneBooks = new Dictionary<string, PhoneBook>();PhoneBook zhang = new PhoneBook("張三", "Nokia5300", "13810043231");PhoneBook li = new PhoneBook("李四", "MotoV3i", "13915441146");PhoneBook wang = new PhoneBook("王五", "SumSungD908", "13641259670");phoneBooks.Add(zhang.Name,zhang);phoneBooks.Add(li.Name,li);phoneBooks.Add(wang.Name,wang);FillList(phoneBooks);}上面先封裝三個聯(lián)系人對象嗎,然后以聯(lián)系人的名字為key存進全局電話本對象。
?private void FillList(Dictionary<string, PhoneBook> phoneBooks){lvPhones.Items.Clear();foreach (PhoneBook phoneBook in phoneBooks.Values){ListViewItem item = new ListViewItem(phoneBook.Name);item.SubItems.AddRange(new string[] { phoneBook.PhoneType, phoneBook.PhoneNumber });lvPhones.Items.Add(item);}}效果
?
實現(xiàn)添加效果
在添加按鈕的點擊事件中
?private void btnAdd_Click(object sender, EventArgs e){if (String.IsNullOrEmpty(txtName.Text) ||String.IsNullOrEmpty(txtPhoneNum.Text) ||String.IsNullOrEmpty(txtPhoneType.Text)){MessageBox.Show("電話信息不能為空");return;}PhoneBook phone = new PhoneBook(txtName.Text, txtPhoneType.Text, txtPhoneNum.Text);phoneBooks.Add(phone.Name,phone);FillList(phoneBooks);this.txtName.Text = "";this.txtPhoneNum.Text = "";this.txtPhoneType.Text = "";}效果
?
實現(xiàn)刪除效果
? private void btnDel_Click(object sender, EventArgs e){if (this.lvPhones.SelectedItems.Count == 0){MessageBox.Show("沒有選擇刪除項。");return;}string key = this.lvPhones.SelectedItems[0].Text;phoneBooks.Remove(key);FillList(phoneBooks);this.txtName.Text = "";this.txtPhoneNum.Text = "";this.txtPhoneType.Text = "";}效果
?
實現(xiàn)詳情效果
?private void btnDetail_Click(object sender, EventArgs e){if (this.lvPhones.SelectedItems.Count == 0){MessageBox.Show("沒有選擇。");return;}string key = this.lvPhones.SelectedItems[0].Text;this.txtName.Text = phoneBooks[key].Name;this.txtPhoneNum.Text = phoneBooks[key].PhoneNumber;this.txtPhoneType.Text = phoneBooks[key].PhoneType;FillList(phoneBooks);}效果
?
實現(xiàn)統(tǒng)計效果
? private void btnTotal_Click(object sender, EventArgs e){string count = string.Format("總共有{0}個電話!", phoneBooks.Count.ToString());MessageBox.Show(count);}效果
?
實現(xiàn)退出效果
?private void btnExit_Click(object sender, EventArgs e){this.Dispose();}效果
?
源碼下載
https://download.csdn.net/download/badao_liumang_qizhi/11566183
?
?
?
?
總結(jié)
以上是生活随笔為你收集整理的C#中教你一步步实现一个电话本窗体程序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#中实现窗体程序的退出按钮功能
- 下一篇: C# Winform程序中DataGri