C#通讯录——Windows Form Contact List

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

转载于:https://www.cnblogs.com/landv/p/6366312.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Windows Forms 是一个用于创建 Windows 桌面应用程序的图形用户界面(GUI)框架。它是 C# 语言中的一部分,可以帮助开发人员快速构建功能丰富的窗体应用程序。 Windows Forms 提供了一组丰富的控件,例如按钮、文本框、标签、列表框等,开发人员可以使用这些控件来设计应用程序的用户界面。通过事件处理机制,开发人员可以为控件添加交互逻辑,并响应用户的操作。 在 Visual Studio 中创建 Windows Forms 应用程序非常简单。你可以选择新建一个 Windows Forms 应用程序项目,然后通过拖拽和放置控件来设计界面,使用 C# 代码来编写控件的事件处理逻辑。 以下是一个简单的示例代码,演示了如何在 Windows Forms 应用程序中显示一个窗体: ```csharp using System; using System.Windows.Forms; namespace WindowsFormsApp { public partial class MainForm : Form { public MainForm() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { MessageBox.Show("Hello, World!"); } } } ``` 上述代码创建了一个名为 `MainForm` 的窗体,并在窗体中添加了一个按钮。当按钮被点击时,会弹出一个包含 "Hello, World!" 文本的消息框。 这只是一个简单的示例,Windows Forms 还提供了更多功能和控件,以满足不同的应用程序需求。你可以通过学习更多关于 Windows Forms 的文档和教程,深入了解其更多特性和使用方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值