views:

548

answers:

1

I have a search usercontrol that I'd like to make generic.

The control itself will contain a different collection of controls dependent on its context. For example it could be stock items, people, address as the context of the search..

How can I make it generic enough that based upon the search context it knows exactly what user controls it needs on the form?

Any programmer can then just drag the user control onto their form, set it's context and we are good to go.

My first thought is to create a base control for all the individual user controls and the search dialog accepts these in the constructor so it knows which ones to show at runtime. You can create inherited versions of the base controls and pass these in. Or maybe just set the search context (enum) and it showhow works out what the user controls are at runtime.

It's all .net 2.0 Winform

Edited for readibilty. Q was far too lenghty and detailed before.

+1  A: 

On our project, we did this by adding an ISearchable interface with CanSearch and Search properties. These took a direction argument specifying forwards or backwards, and the Search method also took a string for performing the search and an enumeration specifying the type of matching (start of phrase, end of phrase, included in phrase, etc).

We then implemented this interface on all controls in a hierarchy. Each control then delegated to the child controls as it saw fit. Our container application would contain a textbox and find next/previous buttons. The container would query the first view in the hierarchy for the interface, which would then chain down to the target control as seen fit by each control in the hierarchy.

We implemented an alternative approach for our clipboard operations whereby we checked the actively focused control directly first to see if it supported our ISupportEdit interface. If it did not, we then used the hierarchy approach.

Jeff Yates