所以我有一些我正在上学的东西,但我一直在遇到问题并无法解决.我几乎完成了项目,但我一直在遇到这个简单的问题.首先,我必须创建一个包含5个联系人的文件,包括每个联系人的信息.然后我将不得不使用类将联系人放入列表中.然后,当从列表中选择任何联系人时,将显示新表单以及每个人的信息.以下是我到目前为止的代码:MAIN FORM
public Form1()
{
InitializeComponent();
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
lstNames.Items.Clear();
}
private void btnGetInfo_Click(object sender, EventArgs e)
{
{
// Call methods
FileRead();
DisplayNameList();
}
}
private void FileRead()
{
try
{
StreamReader inputFile;
string line;
char[] deliminator = { ',' };
inputFile = File.OpenText("Accounts.txt");
//While loop to move through the entries.
while (!inputFile.EndOfStream)
{
//Use class
PersonEntry entry = new PersonEntry();
//Variable to hold line.
line = inputFile.ReadLine();
//Tokenize the line.
string[] tokens = line.Split(deliminator);
//Store the tokens in the entry object.
entry.Name = tokens[0];
entry.Email = tokens[1];
entry.PhoneNumber = tokens[2];
//Get the names in the list!
nameList.Add(entry);
}
}
catch (Exception ex)
{
MessageBox.Show("ERROR: Something went WRONG! " + " " + ex.Message);
}
}
private void DisplayNameList()
{
//Add the entry objects to the List
foreach (PersonEntry nameDisplay in nameList)
{
lstNames.Items.Add(nameDisplay.Name);
}
}
private void lstNames_SelectedIndexChanged(object sender, EventArgs e)
{
if (lstNames.SelectedIndex != -1)
{
//Get full info for the selected item in the list
string name = nameList[lstNames.SelectedIndex].Name;
string email = nameList[lstNames.SelectedIndex].Email;
string phone = nameList[lstNames.SelectedIndex].PhoneNumber;
//Create second form for these details
Informationform form2 = new Informationform(name, email, phone);
form2.ShowDialog();
}
else
{
MessageBox.Show("Please pick a name!");
}
}
}
Run Code Online (Sandbox Code Playgroud)
}
我有第二种形式!
public partial class Informationform : Form
{
public Informationform()
{
InitializeComponent();
}
public Informationform(string name, string email, string phone)
{
lblName.Text = name;
lblEmail.Text = email;
lblPhone.Text = phone.tostring();
}
private void Informationform_Load(object sender, EventArgs e)
{
}
}
Run Code Online (Sandbox Code Playgroud)
}
这就是我创建的课程
class PersonEntry
{
// Fields
private string _name; // The phone's brand
private string _email; // The phone's model
private string _phonenumber; // Retail price
// Constructor
public PersonEntry()
{
_name = "";
_email = "";
_phonenumber = "";
}
// Brand property
public string Name
{
get { return _name; }
set { _name = value; }
}
// Model property
public string Email
{
get { return _email; }
set { _email = value; }
}
// Price property
public string PhoneNumber
{
get { return _phonenumber; }
set { _phonenumber = value; }
}
}
Run Code Online (Sandbox Code Playgroud)
}
请问这让我发疯了,问题是我每次点击列表中的名字时,都会收到一个异常错误,表示它是空的(假设在标签中看到每个人的信息)!请看一下代码!提前致谢!
在接受参数的构造函数中,您需要调用InitializeComponent();:
public Informationform(string name, string email, string phone)
{
InitializeComponent();
lblName.Text = name;
lblEmail.Text = email;
lblPhone.Text = phone.tostring();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
89 次 |
| 最近记录: |