所以我正在为学校做一些事情,但我一直遇到问题,无法解决。我几乎完成了项目,但是我一直遇到这个简单的问题。所以首先我必须创建一个包含 5 个联系人的文件,包括每个联系人的信息。然后我将不得不使用类将联系人放入列表中。然后,当从列表中选择任何联系人时,将显示一个新表格以及每个人的信息。这是我到目前为止的代码: 主窗体
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!");
}
}
}
我这里有第二种形式!
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)
{
}
}
这是我创建的类</p>
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; }
}
}
拜托,这让我发疯了,问题是每次我点击列表中的名字时,我都会得到一个异常错误,它是空的(应该是在标签中看到每个人的信息)!请看一下代码!提前致谢!
最佳答案
在接受参数的构造函数中,您需要调用 InitializeComponent();
:
public Informationform(string name, string email, string phone)
{
InitializeComponent();
lblName.Text = name;
lblEmail.Text = email;
lblPhone.Text = phone.tostring();
}
关于c# - 我如何将信息从一种形式发送到另一种形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43713682/