Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I expose events of a DataRepeater control's repeated control to its client form?

VB Programming Concepts

How do I expose events of a DataRepeater control's repeated control to its client form?

by  BobRodes  Posted    (Edited  )
The problem is that a form which uses a DataRepeater (DR) control can't write event handlers for the repeated control (RC) in the normal way, since the DR doesn't expose them. Raising events in the RC only raises them to the DR, which ignores them.

This can be solved by creating a class which brokers the event handling between the RC and the form. YouÆll need to work with the RC control, so it has to be an ActiveX control (not, say, a text box) so youÆll have a code module. The steps are:

1. Create a class, say RCEvents. This class declares a bunch of events and raises the one it's told to raise via a method. In this class:
a. Declare all the RC events you want to expose.
b. Add one method, e. g. RaiseDREvent, which takes a whichEvent string argument for the name of the event and optional arguments to hold any arguments that the events need to pass.
c. In the method, evaluate whichEvent and raise the appropriate event.

2. In the RC project, set a reference to your RCEvents class.

3. In the RC code module:
a. Dim a variable of type RCEvents.
b. Instantiate it in the Initialize event.
c. Create an Events property. The events property returns an instance of the RCEvents class.
d. For each event you want to expose, create an event handler. In this handler, add a line which calls the (e.g.) RaiseDREvent method in the RCEvents class, and passes as an argument the name of the event you are handling.

4. In the form project, set a reference to the RCEvents class.

5. In the form's code module:
a. Dim a variable of type RCEvents. Make sure you use the WithEvents keyword.
b. In the Form_Load event handler, set this variable equal to the RCÆs Events property that you created (dr.RepeatedControl.Events).
c. Write event handlers for the RCEvents classÆs events.

LetÆs take a simple example, which probably wonÆt come up in real life but will illustrate this. Suppose our RC has a text box called txtBox, and we want to allow the form that the DR is on to write an event handler for txtBoxÆs Keypress event, letÆs say to force alpha input to caps. We could do this in the RCÆs code window, by just writing a txtBox_KeyPress event handler. But then, the user of the DR control wouldnÆt have any choice about how it worked; the behavior would be, as it were, hard coded in the control. What we want to do is have a choice of how to work with txtBoxÆs keypress event, from the context of the form containing the DR control. ThereÆs the problem that this technique addresses.

Now, the way to work this is to have a single object that both the RC and the Form can see broker the events. When an event happens in the RC, the RC tells the object to raise an analogous event. Since the form is an observer of the same object (since it instantiated it with the WithEvents keyword), it is notified when the object raises this event, and can call its local event handler. ThatÆs what we have done here. We have our broker object, instantiated by the RC. We then expose the class in the RCÆs Events property. When we declare a class of the same type in the form, we set it equal to the return value of the RCÆs events property, which in effect means that both the RC and the form have a pointer to the broker object. In response to an event in its own context, the RC calls a method of the broker object and tells it to raise a particular event, which the Form context sees and handles.

Bob Rodes
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top