【发布时间】:2017-11-29 14:52:32
【问题描述】:
我正在制作一本通讯录以尝试学习课程并打开新表格。
我正在尝试从文本文档中获取项目以填充列表框,仅使用每行第一个分隔符之前的字符串。当我运行脚本时,会出现 Windows 窗体,但列表框是 空白,但其中似乎有五个项目可供选择。点击它们没有任何效果。
这是我的表单代码:
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();
}
}
这是我的课程代码:
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; }
}
}
}
我似乎无法在运行时显示它;但是,我确实尝试添加一个按钮并在单击时填充列表框,这似乎有效。
我希望能有新的眼光。
【问题讨论】: