Current.MainWindow
MessageBox.Show("The main window is " & main.Title)
Of course, if you want to access any methods, properties, or events that you??™ve added to
your custom main window class, you need to cast the window object to the right type. If the
main window is an instance of a custom MainWindow class, you can use code like this:
Dim main As MainWindow = CType(Application.Current.MainWindow, MainWindow)
main.DoSomething()
A window can also examine the contents of the Application.Windows collection, which
provides references to all the currently open windows:
For Each window As Window In Application.Current.Windows
MessageBox.Show(window.Title & " is open.")
Next
CHAPTER 3 n THE APPLICATION 66
In practice, most applications prefer to use a more structured form of interaction between
windows. If you have several long-running windows that are open at the same time and they
need to communicate in some way, it makes more sense to hold references to these windows
in a custom application class. That way you can always find exactly the window you need.
Similarly, if you have a document-based application, you might choose to create a collection
that tracks document windows but nothing else. The next section considers this technique.
nNote Windows (including the main window) are added to the Windows collection as they??™re shown, and
they??™re removed when they??™re closed.
Pages:
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182