将文本文件读取到列表框c#

时间:2016-04-29 01:29:03

标签: c# listbox streamwriter display

我无法找到为什么我的文本文件没有显示到我的列表框中。该程序是使用Vusial Studio构建的。我没有语法错误,我相信我的逻辑是合理的。你能帮我找出原因吗?

以下是我的Form1.cs的代码:

namespace Jason_T_READER_
{
    public partial class Form1 : Form
    {
        string selectedName = "";
        List<PersonEntry> nameList = new List<PersonEntry>();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                StreamReader inputfile;
                inputfile = File.OpenText("Personlist.txt");

                string inRecord;

                while (!inputfile.EndOfStream)
                {
                    inRecord = inputfile.ReadLine();
                    string[] tokens = inRecord.Split(',');

                    PersonEntry person = new PersonEntry(tokens[0],tokens[1],tokens[2]);

                    listBox1.Items.Add(person.Name);

                    PersonEntry friendObj = new PersonEntry(tokens[0],tokens[1],tokens[2]);

                    nameList.Add(friendObj);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                MessageBox.Show("Exception in try/catch. ");
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectedName = listBox1.SelectedItem.ToString();
            PersonInfoForm myPerInfoForm = new PersonInfoForm();
            Label label1 = new Label();
            label1.Size = new Size(270, 75);
            label1.Location = new Point(10, 10);

            foreach (PersonEntry PersonEntry in nameList)
            {
                if (PersonEntry.Name == selectedName)
                {
                    label1.Text += "Name: " + PersonEntry.Name + "\n" +
                                   "Email: " + PersonEntry.Email + "\n" +
                                   "Phone number: " + PersonEntry.PhoneNum;
                }
            }

            myPerInfoForm.Controls.Add(label1);
            myPerInfoForm.ShowDialog();

        }

    }
}

这是我的课程表:

namespace Jason_T_READER_
{
    class PersonEntry
    {
        private string _name;
        private string _email;
        private string _phoneNum;

        public PersonEntry(string name, string email, string phoneNum)
        {
            _name = name;
            _email = email;
            _phoneNum = phoneNum;
        }
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }
        public string Email
        {
            get
            {
                return _email;
            }
            set
            {
                _email = value;
            }
        }
        public string PhoneNum
        {
            get
            {
                return _phoneNum;
            }
            set
            {
                _phoneNum = value;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

以下几点可以使它发挥作用;

  1. 将PersonEntry列表,nameList绑定到列表框而不是仅用于名称
  2. 指定displayMember和ValueMember
  3. 这样您就可以从所选项目中获取PersonEntry-Object,并可以从中收集其余属性。无需再次迭代集合。
  4. 如果有两个相同的名称,您的代码将为您提供最后一个的详细信息;
  5. 用于绑定列表框的代码;

    List<PersonEntry> nameList = new List<PersonEntry>();
    ListBox listBox1 = new ListBox();
    foreach (string line in File.ReadAllLines("Personlist.txt"))
    {
        string[] tokens = line.Split(',');
        nameList.Add(new PersonEntry(tokens[0], tokens[1], tokens[2]));
    }
    listBox1.DataSource = nameList;
    listBox1.DataTextField = "name";
    listBox1.DataValueField = "name";
    

    选择更改将是:

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        var boundList = (IList<PersonEntry>)listBox1.DataSource;
        PersonEntry selected = boundList[listBox1.SelectedIndex];
        Label label1 = new Label();
        label1.Size = new Size(270, 75);
        label1.Location = new Point(10, 10);
    
        label1.Text += "Name: " + selected.Name + "\n" +
                               "Email: " + selected.Email + "\n" +
                               "Phone number: " + selected.PhoneNum;
    
        myPerInfoForm.Controls.Add(label1);
        myPerInfoForm.ShowDialog();
    
    }