电话列表c#(从文件中读取)

时间:2017-07-03 00:21:21

标签: c#

Phonebook

这是一个非常简单的程序。这是一个家庭作业项目。程序应该启动,读取.txt文件,填充名称列表,当您单击名称时,将显示电话号码。我有它编码,没有错误,没有警告。它不会读取.txt文件,我无法弄清楚原因。我一直在搜索我的书,youtube,甚至在这里,但不能把它固定下来。任何帮助,将不胜感激。这是我目前的代码。

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

namespace Phonebook
{
    struct PhoneBookEntry
    {
        public string name;
        public string phone;
    }
    public partial class Form1 : Form
    {
        // FIeld to hold a list of PhoneBookEntry objects.
       private List<PhoneBookEntry> phoneList = new List<PhoneBookEntry>();

        public Form1()
        {
            InitializeComponent();
       }

        // The ReadFile method reads the contents of the
        //PhoneList.txt file and tores it as PhoneBokeEntry
        // objects in the phoneList.
        private void ReadFile()
        {
            try
            {
                StreamReader inputFile;  // To read the file
                string line;             // To hold a line from the file

                // Create an instance of the PhoneBookEntry structure.
                PhoneBookEntry entry = new PhoneBookEntry();

                // Create a delimiter array.
                char[] delim = { ',' };

                // Open the PhoneList file.
                inputFile = File.OpenText("PhoneList.txt");

                // Read the lines from the file.
                while (!inputFile.EndOfStream)
                {

                    // Read a line from the file.
                    line = inputFile.ReadLine();

                    // Tokenize the line
                    string[] tokens = line.Split(delim);

                   // Store the tokens in the entry object.
                    entry.name = tokens[0];
                    entry.phone = tokens[1];

                // Add the entry object to the List.
                phoneList.Add(entry);
                }
            }
            catch (Exception ex)
            {
              // Display an error message.    
               MessageBox.Show(ex.Message);
            }
        }
        // The DisplayNames method displays the list of names
        // in the namesListBox conrol.
        private void DisplayNames()
        {
            foreach (PhoneBookEntry entry in phoneList)
        {
            nameListBox.Items.Add(entry.name);
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            // Read the PhoneList.txt file.
            ReadFile();

            // DIsplay the names.
            DisplayNames();
        }

        private void nameListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Get the index of the selected item.
            int index = nameListBox.SelectedIndex;

            // Display the corresponding phone number.
            phoneLabel.Text = phoneList[index].phone;
        }

         private void exitButton_Click(object sender, EventArgs e)
        {
            // close the form.
            this.Close();
        }

    private void Form1_Load_1(object sender, EventArgs e)
    {

    }
}

}

0 个答案:

没有答案