listening to events causes null reference

  • Thread starter Torbjorn Stavas
  • 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
 
S

Sai

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
 
D

Duncan Mole

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!
 
D

Duncan Mole

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top