Adam Zeidan
Adam Zeidan

Reputation: 21

Reading textfile to listbox c#

I am having trouble finding out why my textfile is not being displayed to my listbox. This program was built using Vusial Studio. I have no syntax errors, and I believe that my logic is sound. Can you help me find out why?

Here is the code for my 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();

        }

    }
}

This is my class sheet:

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;
            }
        }
    }
}

Upvotes: 0

Views: 386

Answers (1)

sujith karivelil
sujith karivelil

Reputation: 29036

Here is few points to make it work;

  1. Bind the list of PersonEntry, nameList to the listbox instead for name alone
  2. Specify the displayMember and ValueMember
  3. So that you will get a PersonEntry-Object from the selected item and rest of properties can be collected from that. no need to iterate the collection again.
  4. if there are two same names your code will give you the details of the last;

Code for binding the listbox;

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";

And the selection change will be :

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();

}

Upvotes: 1

Related Questions