I want to show you a quick
example of something, and you try to figure out how I got this to work.
I am going to do New, File and
I am going to build a new ProcessBook display. And look at this--
there is a script that gets involved and it asks what color background do you want to make it?
I think I will choose blue.
There is a ProcessBook display that now has a
blue background. How did I get access to that
application event? Well that seems simple
enough, let me show you what you might think
off the top of your head. This is
of course what I thought the first time I used it, and I was wrong.
I thought this is not a problem,
just go into the editor here and let's look for the
application object and get access to its
events. And then of course I look here and I see that there is no
application object there. I look in the
properties window, and there is no application object there.
How do you get to the application object?
And then I thought some more and said hold on a second,
this is a display. And if I were to add another
one, here is another display.
Let's go back to the Visual Basic Editor.
These are two different displays, how can
there be an application object
that represents the entire application
when basically our code is being
written in the display, not in the application.
How can you get an object that is at a higher
level than the place where we are working right now
which is actually the code. Well the way you do that is
you go to the application
object using a variable that is
defined with events. See I can build
a ProcessBook, I can add a ProcessBook,
and I can look at that ProcessBook in the immediate window.
I can do that right now, I can say let's print out
the application.ProcBook
account. This tells me currently that there are zero
ProcessBooks open. And if I were to open a PIW file, I would
find some ProcessBooks.
I can go into the displays collection, and I can say how many
displays are open? And there are two displays.
But how do we get to this object itself? The application
object and get to
its properties or its procedures.
Well for that you need to dimension with events. And if I can
demonstrate briefly, I will just say
let's dimension X.
I have got the syntax wrong-- dimension with events X
as ProcessBook library application.
And at this point, I now have
an object that has
all those events that are associated with the
application itself. That's how we get to the
application object. So it has very important ramifications
when it comes to doing add-ins.
Because with add-ins, we generally need to get to the application object.
Now that we know about using
with events and application objects, let's take a look
at that last example. This was an
example called AtEvent and
if we look at its
code, as you can see, in the general
declarations, what we are stating
here-- this is where we define our variables.
What we are stating is we are dimensioning with events,
it's a variable called
MyClassObjectVar. And of course I could have
defined that as X, as that's not a meaningful word, it's just a name.
MyClassObjectVar is defined
as a ProcessBook application.
Now that gives us
events that are associated
with that object variable.
The next thing I have to do
is I have to somehow associate
MyClassObjectVar with my current
application. Now for this, I am using something called
the Display Open method.
This is the first we have seen this. There is a display object in ProcessBook
-- that's the current display. And
that object has an open method.
The open event
is what happens when you first open the display.
When this display opens, I am going to use
the set command to set a reference
in this variable to the ProcessBook object library.
In other words,
we are basically returning a dispatch interface into
the object library
of the ProcessBook application. So
MyClassObjectVar in other words has been made a pointer
to that object right there. Now because that
object, or this variable was defined
with events that means that we will pick up all of the events that are
associated with that application. This is a very important step because
without this, basically you
have a variable but it's not point to anything.
Microsoft likens this to a cookie cutter.
When you dimension a variable, like we did back here,
dimensioning a variable is like
saying I am going to have an object that is a certain type.
Where you do not actually stamp the cookie until
you use the set command. That actually creates the instance
of the object. So this creates a pointer
of a certain class of object that is pointing
to that particular object.
Since we are using WithEvents, events
do show up in this list from MyClassObjectVar
And here is the event that got interfered with.
This is the NewDisplay event. If you recall when
I built a new display,
we had a little prompt-- this was an input box
that said background color did you want? And of course
at that point I just changed the background color.
The NewDisplay event
actually returns
a variable called ANewDisplay which is a pointer
to the display that is being created.
So this is how we get access to
the application events.