C#通讯录——Windows Form Contact List

简介: C#通讯录 Windows Form Contact List 主窗口 using System; using System.Collections.Generic; using System.

C#通讯录

Windows Form Contact List

主窗口

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private Contact[] phoneBook = new Contact[1];
        private void Write(Contact obj)
        {

                StreamWriter sw = new StreamWriter("contact.txt");
                sw.WriteLine(phoneBook.Length + 1);
                sw.WriteLine(obj.FirstName);
                sw.WriteLine(obj.LastName);
                sw.WriteLine(obj.Phone);

                for (int x = 0; x < phoneBook.Length; x++)
                {
                    sw.WriteLine(phoneBook[x].FirstName);
                    sw.WriteLine(phoneBook[x].LastName);
                    sw.WriteLine(phoneBook[x].Phone);
                }
                sw.Close();

        }
        private void Read()
        {
            

                StreamReader sr = new StreamReader("contact.txt");
                phoneBook = new Contact[Convert.ToInt32(sr.ReadLine())];

                for (int x = 0; x < phoneBook.Length; x++)
                {
                    phoneBook[x] = new Contact();
                    phoneBook[x].FirstName = sr.ReadLine();
                    phoneBook[x].LastName = sr.ReadLine();
                    phoneBook[x].Phone = sr.ReadLine();
                }
                sr.Close();

        }
        private void Display()
        {
           
                lstContacts.Items.Clear();
                for (int x = 0; x < phoneBook.Length; x++)
                {
                    lstContacts.Items.Add(phoneBook[x].ToString());
                }
            
        }
        private void ClearForm()
        {
            textFirstName.Text = string.Empty;
            textLastName.Text = string.Empty;
            textPhone.Text = string.Empty;

        }
        private void btnAddContact_Click(object sender, EventArgs e)
        {
            Contact obj = new Contact();
            obj._Contact(textFirstName.Text,textLastName.Text,textPhone.Text);

            //lstContacts.Items.Add(obj.ToString());
            BubbleSort();
            FileIf();
            Write(obj);
            Read();
            Display();
            ClearForm();

        }
        private void FileIf()
        {
            if (File.Exists("contact.txt"))
            {
                return;
            }else
            {
                FileStream NewText = File.Create("contact.txt");
                NewText.Close();
            }

        }
        private void Form1_Load(object sender, EventArgs e)
        {
            FileIf();
            Read();
            Display();
        }
        private void  BubbleSort()
        {
            Contact temp;
            bool swap;
            do
            {
                swap = false;
                for(int x = 0; x<(phoneBook.Length -1);x++)
                {
                    if (phoneBook[x].LastName.CompareTo(phoneBook[x+1].LastName)>0){
                        temp = phoneBook[x];
                        phoneBook[x]=phoneBook[x+1];
                        phoneBook[x+1]=temp;
                        swap =true;
                    }
                }
            }while(swap == true);
        }

        private void btnSort_Click(object sender, EventArgs e)
        {
            BubbleSort();
            Display();
        }
       
    }//end of class
}//end of namespace

 

Contact类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApplication2
{

    class Contact
    {

        //public Contact()
        //{
        //    FirstName = "landv";
        //    LastName = "li";
        //    Phone = "13903120312";

        //}
        //public Contact(string _FirstName, string _LastName, string _Phone)
        //{
        //    FirstName = _FirstName;
        //    LastName = _LastName;
        //    Phone = _Phone;
        //}
       
        public void  _Contact(string _FirstName, string _LastName, string _Phone)
        {
            FirstName = _FirstName;
            LastName = _LastName;
            Phone = _Phone;
        }

        private string _FirstName;
        private string _LastName;
        private string _Phone;

        public string FirstName
        {
            get { return _FirstName; }
            set { _FirstName = value; }
        }
        public string LastName
        {
            get { return _LastName; }
            set { _LastName = value; }
        }
        public string Phone
        {
            get { return _Phone; }
            set 
            {
                if(value.Length == 11)
                {
                    _Phone = value;
                }
                else
                {
                    _Phone = "11111111111";
                }
            }
        }

    
       public override string ToString()
       {
           string output = string.Empty;
           output += string.Format("{0},{1}", LastName, FirstName);
           output += string.Format(",{0} {1} {2}", Phone.Substring(0, 3), Phone.Substring(3, 4), Phone.Substring(7, 4));
           return output;

       }
    

    }// end of class
}//end of namespace

源码下载地址: 

http://files.cnblogs.com/files/landv/WindowsFormContactList.zip

网名:浩秦; 邮箱:root#landv.pw; 只要我能控制一個國家的貨幣發行,我不在乎誰制定法律。金錢一旦作響,壞話隨之戛然而止。
目录
相关文章
|
10月前
|
自然语言处理 C# Windows
C#开源免费的Windows右键菜单管理工具
C#开源免费的Windows右键菜单管理工具
125 5
|
10月前
|
安全 C#
C# List基本用法
C# List基本用法
|
10月前
|
Java 数据库 C#
C#winforms实现windows窗体人脸识别
C#winforms实现windows窗体人脸识别
|
人工智能 搜索推荐 C#
C#开源且免费的Windows桌面快速预览神器 - QuickLook
C#开源且免费的Windows桌面快速预览神器 - QuickLook
230 0
|
5月前
|
C# 开发工具 Windows
C# 获取Windows系统信息以及CPU、内存和磁盘使用情况
C# 获取Windows系统信息以及CPU、内存和磁盘使用情况
134 0
|
5月前
|
数据可视化 程序员 C#
C#中windows应用窗体程序的输入输出方法实例
C#中windows应用窗体程序的输入输出方法实例
94 0
|
5月前
|
安全 API C#
C# 如何让程序后台进程不被Windows任务管理器强制结束
C# 如何让程序后台进程不被Windows任务管理器强制结束
152 0
|
7月前
|
C# Windows
C# 创建 Windows Service 项目
C# 创建 Windows Service 项目
54 1
|
6月前
|
关系型数据库 数据库 PostgreSQL
在C#中获取与设置Windows的字符编码方式
通过以上步骤,你可以在Docker环境下有效地重启PostgreSQL服务。这对于维护数据库健康、应用更新或环境配置更改后确保数据库服务正常运行至关重要。根据你的具体需求和环境设置,选择合适的方法来执行重启操作。
36 0
|
安全 C# Windows
C#开源的一个能利用Windows通知栏背单词的软件 - ToastFish
C#开源的一个能利用Windows通知栏背单词的软件 - ToastFish
141 0