如何将信息从一种形式发送到另一种形式

时间:2017-05-01 02:56:13

标签: c# winforms

所以我有一些我正在上学的东西,但我一直在遇到问题并无法解决。我几乎完成了项目,但我一直在遇到这个简单的问题。首先,我必须创建一个包含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)
    {

    }
}

}

这里是我创建的课程

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; }
    }
}

}

请问这让我发疯了,问题是我每次点击列表中的名字时,都会收到一个异常错误,表示它是空的(假设在标签中看到每个人的信息)!请看一下代码!提前致谢!

2 个答案:

答案 0 :(得分:5)

在接受参数的构造函数中,您需要调用InitializeComponent();

public Informationform(string name, string email, string phone)
{
    InitializeComponent();

    lblName.Text = name;
    lblEmail.Text = email;
    lblPhone.Text = phone.tostring();
}

答案 1 :(得分:2)

让我们简化一些事情,只处理一个列表。首先,将ToString覆盖添加到PersonEntry类(将其添加到类中):

public override string ToString()
{
    return Name;
}

如果要在调用ToString时显示的不仅仅是Name值,还可以使用String.Format或类似的。

有了这个,您可以将PersonEntry项添加到ListBox的Items集合中,这将在显示对象时调用ToString方法。

然后,当在ListBox中选择一个项目时,您只需检查它是否为空,然后将其强制转换为PersonEntry。

private void lstNames_SelectedIndexChanged(object sender, EventArgs e)
{
    if (lstNames.SelectedItem != null)
    {
        PersonEntry person = lstNames.SelectedItem as PersonEntry;

        if(person != null)
        {
            //Create second form for these details
            Informationform form2 = new Informationform(person.Name, person.Email, person.Phone);

            form2.ShowDialog();
        }
    }
    else
    {
        MessageBox.Show("Please pick a name!");
    }
}

请参阅MSDN,了解as如何运作的概述。

另外,正如@Idle_Mind所说,你需要在重载的构造函数中调用InitializeComponent();。您也可以将调用链接到默认构造函数:

public Informationform(string name, string email, string phone) : this()
{
    lblName.Text = name;
    lblEmail.Text = email;
    lblPhone.Text = phone.tostring();
}