开发者

From listbox to labels using two forms, C#

Hello i´m coding in C# and have a problem.

开发者_开发技巧

I have two Forms (Form1 and Form2), and the first form (Form1) contains a listbox and some labels like this:

Name: label1
Phone: label2
City: label3

And so on

And the second form (Form2) is a form were the user can fill in Name, Phone, city...

And when the user Press OK The name will only show up in the listbox in Form1, but i want to make a method when the user presses a name in the listbox the other information that the user typed in shall beacome visible in the labels.

So if the user opens Form2 and types in:

Name: John
Phone: 0011223344
City: New York

And then press ok the name John will beacome visible in the listbox but when the user selects John from the listbox the lables will show:

Name: John
Phone: 0011223344
City: New York.

Hope you can help me, thanks.


I'm assuming that your using strings to populate the ListBox here (you don't actually tell us how you are passing the data). Instead of passing a string from Form2 back to Form1, pass a data object:

class Person
{
    string Name { get; set; }
    string PhoneNumber { get; set; }  // perhaps not best as a string
    string City { get; set; }
}

Now expose a "Person" property from Form2:

class Form2
{
    public Person Person
    {
        get { return new Person() { Name = txtName.Text, PhoneNumber = txtPhone.Text, City = txtCity.Text }; }
    }
}

So, in Form1 you can use that property like so:

using( Form2 frm = new Form2() )
{
    if( frm.ShowDialog() == DialogResult.OK )
    {
        Person p = frm.Person;
        list.Items.Add( p.Name );
        lblName.Text = p.Name;
        lblPhone.Text = p.PhoneNumber;
        lblName.City = p.City;
    }
}


you have to write the code to update the lables in the SelectedIndexChanged event of the list box.May be some thing like this:

  private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            form2.label1.text = getname(listBox1.SelectedItem.ToString());
            form2.label2.text = getPhone(listBox1.SelectedItem.ToString());
            form2.label3.text = getCity(listBox1.SelectedItem.ToString());
        }


I'm not 100% certain I understand the layout on your form1, but it sounds like maybe you want some databinding and that might make your life easier.

If you had a class, say called Person, that had a Name, Phone, City property. Then rather than using a listbox, use a datagrid on your main form (styling it appropriately), and it can be bound to a List.

Then when you can use your labels to add new Person's to the datasource, but you can also select a person in the datagrid, and have your labels bounds to the columns, e.g.

this.lblName.Text = form1.datagrid.SelectedRows[0].Cell["Name"];

Doesn't directly answer your question but maybe a slightly nicer approach.


When the user clicks OK from Form2, you will have to retrieve the data they just entered and store it somewhere in Form1.

My advice would be to have a struct type called Person or something containing Name, Phone and City fields. Also, override the ToString method, and from that, simply return the Name field. Then you can add Person objects to your ListBox. Only the name will appear in the ListBox, and the SelectedItem property of the ListBox will return a Person struct, which will give you access all the information you need for the selected item.

Now all that's left is to handle the SelectedIndexChanged event, from which you can populate the labels with the data from the SelectedItem property.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜