Delegate Implementation Questions

G

Greg Wilkerson

I hope I can explain this in a way that can be understood.

I have a custom control, I'll call it navigator, used to navigate
"things". I has, among other controls, two standard buttons, previous
and next. This control is implemented by other custom controls that
displays "things" in different ways. What happens when the previous
or next buttons are clicked depends on the type of parent control. To
me, this seems like a classic delegate scenario. What I'm struggling
with is the proper way to tell the navigator control which method to
use (each parent control has a method that calculates what previous or
next actually are).

What I'd like to do is create and assign the delegate inside the
navigator control with something like this:

delegate NextDelegate = new delegate(this.parent.NextThing);

This fails to compile, as the compiler has no clue how to find
"NextThing". I can deal with that.

My first inclination is to create public properties in the navigator
control and set those properties from the parent to the desired
methods when the navigator control is instantiated.

My second is to have the parent subscribe to the click event on the
navigator control buttons and handle the entire process from the
parent control. But, this forces me to put code common to all
instances of the navigator control in each parent control the
implements the navigator control (a lot of duplicate code).

The more I think about it, the more I like the first method. But,
something tells me there is some type of slick casting/typeof method
that might allow me to implement this as above. I'm interested in
other options and opinions.

Thanks,

Greg
 
N

Nicholas Paldino [.NET/C# MVP]

Greg,

What I would have is an event, "Navigating", with a specific event args
instance "NavigatingEventArgs". This event args class would expose the
address of the current thing being shown (something to uniquely identify
what is being shown), as well as the direction (forward or back, or first or
last, if you want to add those things), and the address of what the next
item should be (it would be able to be set, so the event handler could
change it).

Then, when your forward and back buttons are clicked, you fire the
event. Your parent control would then find out what to show, and show it,
or change what should be shown (you would have to handle when new entries
are added, etc, etc).

It shouldn't be too hard really. You could also expose a method which
will take an identifier and you can move to that location in the stack.

Hope this helps.
 

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