Fire an event from form-3, something like
DataUpdated
. Handle this in form-1 and act accordingly.
[Edit]
~~~~~~~~
In response to your comment, here's some sample code that shows what I mean (you should be able to adapt this pattern into your project):
class Form1 : Form
{
Form3 form3 = new Form3();
void LoadInternal()
{
form3.DataUpdated += Form3_DataUpdated;
}
void Form3_DataUpdated(object sender, EventArgs e)
{
ModifyData();
}
void ModifyData()
{
Form2 login = new Form2();
if (login.ShowLogin())
{
ModifyInternal();
}
}
private void ModifyInternal()
{
}
}
class Form2 : Form
{
internal bool ShowLogin()
{
return true;
}
}
class Form3 : Form
{
public event EventHandler DataUpdated;
private void FireDataUpdated()
{
var temp = this.DataUpdated;
if (temp != null)
{
temp(this, EventArgs.Empty);
}
}
public void Foo()
{
FireDataUpdated();
}
}
[Edit 2]
~~~~~~~~
In response to your comment, it's an event that
you define on your
Form3
class. And it's handled from
Form1
. This is a very popular approach that's used for inter-form communication in Winforms.
[Edit 3]
~~~~~~~~
In response to your comment,
var
is just a shortcut for implicitly typed local variables. In my code example, that will resolve to
EventHandler
at compile-time. Seeing that you are rather new to C#, that may sound odd to you. But the reason I did that is for thread safety. Since delegates are immutable, and since the backing store for an event is a delegate, you may run into a race condition where an event handler is unhooked in one thread even as another thread is firing those events. To avoid this situation, a local variable is used to create a copy. For now just ignore that and use the code directly.