Mega Code Archive

 
Categories / VB.Net / WPF
 

Create Window and add window closing event handler

<Window x:Class="WpfApplication1.Window1"   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   xmlns:system="clr-namespace:System.Windows;assembly=PresentationFramework"   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   Title="Main Window" Height="310" Width="280" Loaded="Window1_Loaded"> </Window> //File:Window.xaml.vb Imports System Imports System.Collections.Generic Imports System.ComponentModel Imports System.Windows Imports System.Windows.Controls Imports System.Windows.Documents Imports System.Windows.Media Namespace WpfApplication1   Public Partial Class Window1     Inherits Window     Public Sub New()       InitializeComponent()     End Sub     Private Sub Window1_Loaded(sender As Object, e As RoutedEventArgs)       Dim backgrounds As Brush() = New Brush(4) {Brushes.Red, Brushes.Blue, Brushes.Green, Brushes.Yellow, Brushes.HotPink}       For i As Integer = 1 To 5         Dim window As New Window()         SetupWindow(window, "Window " & i, backgrounds(i - 1))         window.Show()       Next     End Sub     Private Sub SetupWindow(window As Window, title As String, background As Brush)       AddHandler window.Closing, New CancelEventHandler(AddressOf Window_Closing)       AddHandler window.Closed, New EventHandler(AddressOf Window_Closed)       window.Title = title       window.Width = 100.0       window.Height = 100.0       Dim viewBox As New Viewbox()       Dim textBlock As New TextBlock()       window.Background = background       viewBox.Child = textBlock       textBlock.Text = window.Title       window.Content = viewBox     End Sub     Private Sub Window_Closed(sender As Object, e As EventArgs)       Console.WriteLine("closed")     End Sub     Private Sub Window_Closing(sender As Object, e As CancelEventArgs)       Dim w As Window = TryCast(sender, Window)       If w Is Nothing Then         Return       End If     End Sub   End Class End Namespace