| View previous topic :: View next topic |
| Author |
Message |
jason744 Grandmaster Cheater
Reputation: 0
Joined: 25 Nov 2007 Posts: 638
|
Posted: Fri Mar 28, 2008 9:22 am Post subject: [C#]Accesing Objects in Form1 from Form2 controls. |
|
|
Right, so I have a button (form2) and a listview(form1)
I need to access the listview from button click.
Can someone clear up a bit.
|
|
| Back to top |
|
 |
Symbol I'm a spammer
Reputation: 0
Joined: 18 Apr 2007 Posts: 5094 Location: Israel.
|
Posted: Fri Mar 28, 2008 9:41 am Post subject: |
|
|
| Make it public.
|
|
| Back to top |
|
 |
Estx Expert Cheater
Reputation: 0
Joined: 04 Mar 2008 Posts: 172
|
Posted: Fri Mar 28, 2008 9:56 am Post subject: |
|
|
Make the control public in the Windows Designer code.
Then add the following:
| Code: | private void button1_Click(object sender, EventArgs e)
{
Form1 frm = Form1.ActiveForm;
frm.listView1.Items.Add("test");
} | or use something along the lines of: | Code: | private void button1_Click(object sender, EventArgs e)
{
ListView tLV = (ListView)Application.OpenForms["Form1"].Controls["listView1"];
tLV.Items.Add("test");
} |
edit: just added the extra example lol.
|
|
| Back to top |
|
 |
jason744 Grandmaster Cheater
Reputation: 0
Joined: 25 Nov 2007 Posts: 638
|
Posted: Fri Mar 28, 2008 10:44 am Post subject: |
|
|
Thanks Estx.
Also, do you know how to close a form with a button on the form I want to close?
|
|
| Back to top |
|
 |
Symbol I'm a spammer
Reputation: 0
Joined: 18 Apr 2007 Posts: 5094 Location: Israel.
|
Posted: Fri Mar 28, 2008 12:44 pm Post subject: |
|
|
this.Close()?
|
|
| Back to top |
|
 |
jason744 Grandmaster Cheater
Reputation: 0
Joined: 25 Nov 2007 Posts: 638
|
Posted: Fri Mar 28, 2008 1:15 pm Post subject: |
|
|
Thanks, I used this.close;
Without brackets so It didn't work.
|
|
| Back to top |
|
 |
Symbol I'm a spammer
Reputation: 0
Joined: 18 Apr 2007 Posts: 5094 Location: Israel.
|
Posted: Fri Mar 28, 2008 1:28 pm Post subject: |
|
|
| () means its a call to a function, the parameters should be in the brackets, but since there are no parameters then its simply (); remember that.
|
|
| Back to top |
|
 |
|