Gary Kjos
garykjos at gmail.com
Fri Feb 8 11:36:30 CST 2013
Here's another. On Thu, Feb 7, 2013 at 1:10 PM, John Colby <jwcolby at gmail.com> wrote: > OK, let's discuss what is going on. > > clsDNDText is a class which I call an object wrapper. It wraps an access > object (a text box in this case) and then uses event sinks to service the > events of the wrapped object. So this class wraps a text box. Notice in > the header of the class the line: > > Private WithEvents mtxt As TextBox > > This dimensions a text box variable private (cannot be seen outside of the > class) and informs VBA that the class will be sinking events (the > WithEvents keyword). > > Next we have a constant which holds the text "[Event Procedure]" > > Private Const cEvProc As String = "[Event Procedure]" > > It is good programming practice to as much as possible have a class self > contained, not depending on outside resources to perform its function. > > Next we have termination code. When a class unloads it calls its terminate > event if one exists. In this case it simply "unhooks" the text box, > setting the pointer to nothing. We do that because it is possible to > prevent a form from closing when objects on the form have pointers to them > that are not cleaned up. > > Private Sub Class_Terminate() > Set mtxt = Nothing > End Sub > > We then have an Init function which is public, and allows us to pass in a > pointer to a text box. mInit then stores that pointer in our class header. > This allows the class to sink any and all events for the passed in object > (text box). Notice that we immediately place the cEvProc into the > OnDblClick property of the text box. It is a little known fact that it is > the very existence of that text in the property that causes VBA to start > raising events for the object. By specifically placing that code in that > property in our code, we do not need the developer to double click the > property for us. In this case we have only hooked the OnDblClick event > however we could add others by adding a similar line of code for other > events. > > Function mInit(ltxt As TextBox) > Set mtxt = ltxt > mtxt.OnDblClick = cEvProc > End Function > > And finally, the guts of the thing, the very reason for doing all this, we > have the event sink itself. This code is entered when the event fires for > the specific text box which this class instance wraps. IOW if we have > several instances of this class, one for TextBox 2, TextBox4 and TextBox6, > then when you double click in TextBox4, the event in the instance servicing > that text box will sink the event and do something with it. In this case I > am simply poppigg up a message box to display the name of the control. > > Private Sub mtxt_DblClick(Cancel As Integer) > 'Do something here > MsgBox mtxt.Name & ".DblClick" > End Sub > > John W. Colby > > Reality is what refuses to go away > when you do not believe in it > -- > AccessD mailing list > AccessD at databaseadvisors.com > http://databaseadvisors.com/mailman/listinfo/accessd > Website: http://www.databaseadvisors.com -- Gary Kjos garykjos at gmail.com