1

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.

0

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

4 Comments

This is correct, marked as answered. Any idea why the new form isn't popping up for SelectedIndexChanged? Nevermind -- I didn't get the Event. Everything works as it should now. Thank ya!
@Exulansis Are you sure it's not popping up? I'd guess the SelectIndexChanged event it's not firing at all or you are not seeing it.
Edited that comment - I didn't set the event. It works now. Thanks for the speedy response!
Glad to be of help. As a little further tip, look up into override ToString() for this PersonEntry class
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.