Multiple observers -- panels

E

Eric

For some reason I am having a problem notifying multiple observers of
an event. Here's the basic idea:
MyForm contains two panels LeftPanel and RightPanel (both inherit
Panel). The left one is has a treeview and the right one will have
whatever depending on what is selected on the left. Both panels
register with an event in MyController (something like
SelectedChanged). The problem is only the left SelectedChanged is
being notified of the event. Nothing happens in RightPanel at all.

In panels:
---------------------
private MyController.MyControllerHandler ctrlDelegate;
private MyController ctrl;
....
public Left(and Right)Panel() {
ctrl = new MyController();
ctrlDelegate = new
MyController.MyControllerHandler(this.SelectedChanged);
ctrl.SelectedChanged += ctrlDelegate;
....}
....
public void SelectedChanged(object sender){
//do something
}
---------------------

In just LeftPanel:
---------------------
private void treeView_AfterSelected(object sender, TreeViewEventArgs e)
{
....
ctrl.SelectedIndex = something;
}
---------------------

In MyController:
---------------------
public delegate void MyControllerHandler(object sender);
public event MyControllerHandler SelectedChanged;

public int SelectedIndex {
set { //some stuff
OnSelectedChanged(); }
}

public void OnSelectedChanged() {
SelectedChanged();
}
---------------------
 
L

Larry Lard

Eric said:
For some reason I am having a problem notifying multiple observers of
an event. Here's the basic idea:
MyForm contains two panels LeftPanel and RightPanel (both inherit
Panel). The left one is has a treeview and the right one will have
whatever depending on what is selected on the left. Both panels
register with an event in MyController (something like
SelectedChanged). The problem is only the left SelectedChanged is
being notified of the event. Nothing happens in RightPanel at all.

In panels:

So each panel has its own MyController.
...
public Left(and Right)Panel() {
ctrl = new MyController();
ctrlDelegate = new
MyController.MyControllerHandler(this.SelectedChanged);
ctrl.SelectedChanged += ctrlDelegate;
...}

So each panel listens to the SelectedChanged event of *its own*
MyController.
...
public void SelectedChanged(object sender){
//do something
}
---------------------

In just LeftPanel:
---------------------
private void treeView_AfterSelected(object sender, TreeViewEventArgs e)
{
...
ctrl.SelectedIndex = something;
}

So selecting a treeview node fires the SelectedChanged event *on the
left panel's MyController*.

The right panel is listening to a completely different object's event.

I can't say for sure, but it sounds like there should be only one
MyController, at form level.
 

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