Calling methods from previous form

  • Thread starter Thread starter Ryu
  • Start date Start date
R

Ryu

I have a form that calls another form....Is there any way to call method in
the old form from the new form?
 
Either you pass the reference of the old form at the constructor call of the
other form (dirty design that can lead to memory leak) or you use delegates.
 
Inside Form1.cs file Declare a Delegate like
public delegate void MyDelegate(); // it should be defined outside Form1

then in side Form1
--------------------------------------------------
private void doTest()
{
MessageBox.Show("Hi from Form1");
}

private void Form1_Load(object sender, System.EventArgs e)
{

Form2 frmTest = new Form2();
frmTest.MyDelegate = new MyDelegate(doTest);
frmTest.Show();

}

Inside Form2
-----------------------------------------------------
private MyDelegate testdelegate;

public MyDelegate MyDelegate
{
get
{
return testdelegate;
}
set
{
testdelegate = value;
}
}

private void button1_Click(object sender, System.EventArgs e)
{
testdelegate();

}

HTH
Shishir Kumar Mishra
Manhattan Associates
 
Back
Top