简体   繁体   English

从文本文件填充列表框

[英]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. 当我运行脚本时,将显示Windows窗体,但列表框为空白,但其中似乎有五个项目可供选择。 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. 问题是您的PersonEntry构造函数。 You are assigning empty strings where you should (presumably) be assigning the supplied parameters. 您正在分配空字符串,您应该(大概)在其中分配提供的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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