简体   繁体   中英

How do i send the information from one form to another

so I have something i am working on for school but i keep running in a problem and can't solve it. I am almost done with project, however i keep running into this simple problem. So first of all I have to create a file with 5 contacts in it, including information about each contact. then I will have to get the contacts into a list using a class. Then when selecting any contact from the list, a new form will be displayed along with each person's information. Here are the codes I have so far: 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!");
        }
    }
}

}

Here I have the second form !

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)
    {

    }
}

}

and here goes the class I created

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

}

Please this is driving me crazy, the problem is every time I click on the name in the list, i get an exception error that it is empty (Supposed to be seeing everyone's information in labels ) ! Have a look at the code please!Thanks in advance!

In your Constructor that accepts arguments, you need to call InitializeComponent(); :

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

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

Let's simplify things, and only deal with one list. First, add a ToString override to your PersonEntry class (add this to the class):

public override string ToString()
{
    return Name;
}

You can also use String.Format or similar if you want to display more than just the Name value when ToString is called.

With that in place, you can add your PersonEntry items to the Items collection of your ListBox, which will call your ToString method when displaying the objects.

Then, when an item is selected in the ListBox, you just have to check that it's not null, and then cast it to a 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!");
    }
}

See the MSDN for an overview of how as works.

Also, as @Idle_Mind notes, you need to call InitializeComponent(); in your overloaded constructor. You could also just chain a call to the default constructor:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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