Re: Form1 and Form2 Scope
You need to use events.
Declare a event in form2, when you show form2, form1 should subscribe to this event. In the btnAccept_Click event, you should trigger the event and do something with the data in form1.
When you are new to events, do some googling and you will find lots of good examples
Re: Form1 and Form2 Scope
Set the reference to other form, through the use of constructor or by Set calls:
Code:
class Form1
{
Form2 form;
public void Form1(Form2 frm2)
{
form=frm2;
}
...
...
}
Then use 'form'!
Re: Form1 and Form2 Scope
Thanks guys! I will give these a go when I install VS and my project onto Windows 7 again.
Cheers!
Re: Form1 and Form2 Scope
Hey, sorry to be a pain. I managed to get around to giving this a go. I seem to be doing something wrong though.. Not quite sure what, as I've never touched Forms before.
Same problem as above really..
I have a frmMain and a frmAdd.
I want frmMain to update something (run code) if an event in frmAdd has been triggered (Button Press).
I cant get my head around it and I tried the above mentioned method by the posters, but.. I must be doing it wrong. :/
Re: Form1 and Form2 Scope
Code:
class frmMain : Form
{
void ShowAddForm( )
{
using ( frmAdd form = new frmAdd( ) )
{
form.SomeEvent += form_SomeEvent;
form.ShowDialog( );
}
}
void form_SomeEvent;( object sender, EventArgs e )
{
// handle event from your add form here
}
}
Re: Form1 and Form2 Scope
If you don't need to use the data until after you return from Form2, then you don't need to use an event.
Just expose the data from Form2 as a public property and get the data from the property after you return from form2.ShowDialog().
An event is only required if you need to "send" the data to Form1 while form2 is opened (providing Form2 is a modal dialog).
You may also need an event if you open Form2 as a modeless dialog.