Plohej Plohej - 1 year ago 60
C# Question

How to get info from another form?

I have a problem where I can't get information from a second form in C#.

I made a easy program to add some names to a

ListView
. And somehow I cannot get the name from second form.

Here's code:

// this is second form
private String name { get; set; }
private void button1_Click(object sender, EventArgs e)
{
name = textBox1.Text;

this.Close();
}




// and first form on button click AddPerson()
Form2 basicPerson = new Form2();
basicPerson.ShowDialog();

ListViewItem item = new ListViewItem(basicPerson.Name);
listView1.Items.Add(item);


With this code, result I am getting is
ListViewItem
with "Form2" name. Does
.Close();
somehow kill the
Name
variable? How to make this work?

Thanks for help!

Answer Source

The basicPerson.Name is not the same as the basicPerson.name.

C# is case-sensitive.

The basicPerson.Name is the Control.Name property (the Form class descends from the Control class). And the Control.Name indeed returns a form name.

Read the basicPerson.name. Though you better change the property name to avoid future misunderstandings.

And once you switch to name (or to a better property name), you will find out that you need to change an accessibility level of the property to public or internal to be able to read it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download