forms and communication between object

  • Thread starter Thread starter jake
  • Start date Start date
J

jake

Hi everyone,

I have a form and on it I have place two different object derived from
panel. When someone clicks on the first object I want to do a
calculation and then call a method in the second object which does
another calculation and then displays the result.

So when the first object is clicked I need to call a method in the
second object. To do this I am making the second class a property of
the first class and then passing the second object into the
constructor. By my understanding this will not make a second copy of
the object but holds a reference (<- not sure what the correct term
is) to the original object.

public class FirstObject : System.Windows.Forms.Panel
{
Panel thePanel;
SecondObject theSecondObject;

Here are my questions firstly is the term reference in the last line
correct and secondly is this the best solution to the problem.


thanks

jake
 
Maybe calling the parent and have the parent pass the paramters on to the
other object?
 
Hi

Thanks for the reply I tried that but I had problems.

If I add a method to my form such as

public void TryToFireMe()
{
MessageBox.Show("fired");
}

how do I call it from the object pasted on it. I tried

this.Parent.TryToFireMe();

but I get a compiler error

C:\Documents and Settings\Admin\My Documents\Visual Studio
Projects\WindowsApplication1\MyPanel.cs(55):
'System.Windows.Forms.Control' does not contain a definition for
'TryToFireMe'

My form is derived as below
public class Form1 : System.Windows.Forms.Form

Hope this is enough information.

Any thoughts

jake
 
You pass the parent instance as a parameter when you create the objects.

MyPanel panel1 = new MyPanel(this);
MyOtherPanel panel2 = new MyOtherPanel(this);

public class MyPanel
{
Parentclass parent;

public MyPanel(Parentclass somename)
{
parent = somename;
}
}

Then when you need to you call
parent.TryToFireMe();
 
Back
Top