listening to events causes null reference

  • Thread starter Thread starter Torbjorn Stavas
  • Start date Start date
T

Torbjorn Stavas

I have a form with a few panels added to it. Each of these panels are
instances
of different classes, that has inherited System.Windows.Form.Panel,
such as

class Panel1 : System.Windows.Forms.Panel
class Panel2 : System.Windows.Forms.Panel
class Panel3 : System.Windows.Forms.Panel

Each of these panels are initiated in the main form, and added to the
form with Controls.add();

So, to the problem. In panel1, i want to listen to an event in panel2.
I'm solving this by having a reference (called frmRef) to the main
form in panel1.
In panel1, i try to listen to an event in panel2 with the help of the
following code:

(Code in Panel1)
this.frmRef.panel2.AnEvent += new AnEvent(pnl2_AnEvent);

this.line gives me a null reference when starting upp the program. I
tried to initiate panel2 in panel1, and adding it to panel1's control.
Then the listening to the event in panel2 works just fine. But this
isn't what i want to do.

What am i missing here?

//Torbjorn
 
hi,

The cleaner design here seems to be to raise the events to Panel raise an
event to a main form or panel manager class. This class should then pass the
arguments to the panels that are interested in this event through a
property.

hth,

sai
 
In the design pattern below you will have to instantiate panel 2 before
panel 1 within your main form. Otherwise the null reference is that there is
no panel, therfore no event!
 
Assuming my last post was correct, you can spot things like this by adding a
breakpoint above the problem aread and stepping through looking at locals.
 
Back
Top