Activate window, close window, bring window to front : Window Event « Windows Presentation Foundation « VB.Net Tutorial

Home
VB.Net Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statements
5.Date Time
6.Class Module
7.Development
8.Collections
9.Generics
10.Attributes
11.Event
12.LINQ
13.Stream File
14.GUI
15.GUI Applications
16.Windows Presentation Foundation
17.2D Graphics
18.I18N Internationlization
19.Reflection
20.Regular Expressions
21.Security
22.Socket Network
23.Thread
24.Windows
25.XML
26.Database ADO.net
27.Design Patterns
VB.Net
VB.Net by API
VB.Net Tutorial » Windows Presentation Foundation » Window Event 
16.58.5.Activate window, close window, bring window to front
<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.Resources>
    <DataTemplate DataType="{x:Type Window}" x:Key="WindowTemplate">
      <StackPanel>
        <Rectangle Height="50" Width="50">
          <Rectangle.Fill>
            <VisualBrush Visual="{Binding}" />
          </Rectangle.Fill>
        </Rectangle>
        <TextBlock Text="{Binding Path=Title}" />
      </StackPanel>
    </DataTemplate>
  </Window.Resources>
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="100" />
      <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <ListBox x:Name="lbxWindows" ItemTemplate="{StaticResource WindowTemplate}">
      <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
          <WrapPanel />
        </ItemsPanelTemplate>
      </ListBox.ItemsPanel>
    </ListBox>

    <StackPanel Grid.Row="1">
      <CheckBox x:Name="cbxIsVisibleInTaskBar" Content="IsVisibleInTaskbar" IsChecked="{Binding ElementName=lbxWindows, Path=SelectedItem.ShowInTaskbar}"/>
      <CheckBox x:Name="cbxIsVisible" Content="IsVisible" 
      IsChecked="{Binding ElementName=lbxWindows, Path=SelectedItem.IsVisible, Mode=OneWay}" 
      Checked="CheckBox_Checked_Changed" 
        Unchecked="CheckBox_Checked_Changed"/>
      <CheckBox x:Name="cbxCanClose" Content="CanClose" IsChecked="True"/>
      <Button Content="Bring To Front" Click="btnBringToFront_Click" />
      <Button Content="Close" Click="btnClose_Click"/>
    </StackPanel>
  </Grid>
</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)
      For i As Integer = To 5
        Dim window As New Window()

        SetupWindow(window, "Window " & i)

        window.Show()
      Next

      RebuildWindowList()
    End Sub
    Private Sub SetupWindow(window As Window, title As String)
      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
      window.Content = title
    End Sub
    Private Sub RebuildWindowList()
      Dim windows As New List(Of Window)()

      For Each window As Window In Application.Current.Windows
        If window Is Me Then
          Continue For
        End If

        windows.Add(window)
      Next

      lbxWindows.ItemsSource = windows
    End Sub

    Private Sub Window_Closed(sender As Object, e As EventArgs)
      RebuildWindowList()
    End Sub

    Private Sub Window_Closing(sender As Object, e As CancelEventArgs)
      Dim As Window = TryCast(sender, Window)

      If w Is Nothing Then
        Return
      End If

      e.Cancel = Not cbxCanClose.IsChecked = True
    End Sub

    Private Sub CheckBox_Checked_Changed(sender As Object, e As RoutedEventArgs)
      Dim window As Window = TryCast(lbxWindows.SelectedItem, Window)

      If window Is Nothing Then
        Return
      End If

      If cbxIsVisible.IsChecked = True Then
        window.Show()
      Else
        window.Hide()
      End If
    End Sub

    Private Sub btnBringToFront_Click(sender As Object, e As RoutedEventArgs)
      Dim window As Window = TryCast(lbxWindows.SelectedItem, Window)

      If window IsNot Nothing Then
        window.Activate()
      End If
    End Sub

    Private Sub btnClose_Click(sender As Object, e As RoutedEventArgs)
      Dim window As Window = TryCast(lbxWindows.SelectedItem, Window)

      If window IsNot Nothing Then
        window.Close()
      End If
    End Sub
  End Class
End Namespace
WPF Activate Window Close Window Bring Window To Front
16.58.Window Event
16.58.1.Window.CommandBindingsWindow.CommandBindings
16.58.2.Window.DragMoveWindow.DragMove
16.58.3.Listen to Window loaded eventListen to Window loaded event
16.58.4.Create Window and add window closing event handlerCreate Window and add window closing event handler
16.58.5.Activate window, close window, bring window to frontActivate window, close window, bring window to front
16.58.6.Window Preview Key EventsWindow Preview Key Events
16.58.7.Window On Mouse move eventWindow On Mouse move event
16.58.8.Window On Mouse up eventWindow On Mouse up event
16.58.9.Window mouse down eventWindow mouse down event
16.58.10.Window Preview mouse down eventWindow Preview mouse down event
16.58.11.Window mouse up eventWindow mouse up event
16.58.12.Close a window with Escape key pressedClose a window with Escape key pressed
16.58.13.Window Closing and Closed eventWindow Closing and Closed event
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.