简体   繁体   中英

Populate listbox from text file

I am making an contact book to try to learn classes, and opening new forms.

I am trying to get items from a text document to populate a list box, using only the string before the first delimiter from each line. When I run the script, the Windows form appears, but the listbox is blank but there appears to be five items in it that are selectable. Clicking on them has no effect.

Here is my code for the form:

namespace AddressBook
    {
public partial class formMain : Form
    {
    //Pub vars
    string selectedName = "";
    List<string> values = new List<string>();

    public formMain()
    {
        InitializeComponent();
    }

    public void formMain_Load (object sender, EventArgs e)
    {
        //load values from file
        try
        {
        StreamReader inputFile;
        inputFile = File.OpenText("EmpRoster.txt");

        string lines;

        while (!inputfile.EndOfSteam)
        {
            lines = inputFile.ReadLine();
            string[] tokens = lines.Split(',');

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

            values.Add(person.Name + ";" + person.Email + ";" + person.Phone);

            listOuput.Items.Add(person.Name);
        }
        }

        catch (Exception ex)
        {
        MessageBox.Show(ex.Message);
        }
    }


    //Selected index change
    private void listOutput_SelectedIndexChanged(object sender, EventArgs e)
    {
        selectedName = listOutput.SelectedItem.ToString();
        Form newForm = new Form();

        Label label1 = new Label();
        label1.Size = new Size(270, 75);
        label1.Location = new Point(10, 10);

        foreach (string str in values)
        {
        if (str.Containes(selectedName))
        {
            string[] tokens = str.Split(';');
            label1.text += "Name: " + tokens[0] + "\n" + "Email: " + tokens[1] + "\n" + "Phone Number: " + tokens[2] + "\n";
        }
        }
        newForm.Controls.Add(label1);
        newForm.ShowDialog();
    }
}

and here is my code for the class:

namespace AddressBook
{
    public class PersonEntry
    {
    private string _name;
    private string _email;
    private string _phone;

    public PersonEntry(string name, string email, string phone)
    {
        _name = "";
        _email = "";
        _phone = "";
    }

    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }

    public string Email
    {
        get { return _email; }
        set { _email = value; }
    {

    public string Phone
    {
        get { return _phone; }
        set { _phone = value; }
    }
    }
}

I cannot seem to get this to show at run; however, I did try adding a button and populating the listbox on click, and that seemed to work.

I'd appreciate some fresh eyes on this.

The problem lies on how you are instantiating your class. If you look at the constructor:

public PersonEntry(string name, string email, string phone)
{
    _name = "";
    _email = "";
    _phone = "";
}

You are not storing the received values, but ignoring them completely. Just simplify your class to this:

public string Name { get; set; }
public string Email { get; set; }
public string Phone { get; set; }

public PersonEntry(string name, string email, string phone)
{
    Name = name;
    Email = email;
    Phone = phone;
}

You don't need to generate the backing fields, that's done automatically for you.

Your PersonEntry constructor is the issue. You are assigning empty strings where you should (presumably) be assigning the supplied parameters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM