Close WPF window that is running within another WPF's grid.-VBForums
Results 1 to 2 of 2

Thread: Close WPF window that is running within another WPF's grid.

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2013
    Posts
    58

    Close WPF window that is running within another WPF's grid.

    Hello,

    I have a Wpf window(MainWindow) with 1 grid (Grid1) that contains 1 button(button that when clicked launches another WPF in Grid2) and a second grid (Grid2).

    The second window (Window1) is launched inside Grid2 of the MainWindow.

    Inside Window1 there are 3 buttons.

    Button1 starts a messagebox (works fine)
    Button2 changes the contents of the Label (works fine)
    Button3 should close down the Window1 but doesn't.

    My question is. What is the best way under these circumstances to close down Window1 and leave MainWindow open?



    MainWindow.xaml
    Code:
    <Window x:Class="MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfInsideWpf"
            mc:Ignorable="d"
            Title="MainWindow" Height="350" Width="525">
        <Grid x:Name="Grid1" Background="#FFCCD424">
            <Grid x:Name="Grid2" HorizontalAlignment="Left" Height="234" Margin="43,51,0,0" VerticalAlignment="Top" Width="438" Background="#FFDC1B1B"/>
            <Button x:Name="button" Content="Run" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="43,10,0,0"/>
    
        </Grid>
    </Window>
    MainWindow.xaml.vb

    Code:
    Class MainWindow
        Private Sub button_Click(sender As Object, e As RoutedEventArgs) Handles button.Click
            Dim PO = New Window1()
    
            Dim Content As Object = PO.Content
    
            PO.Content = Nothing
    
            Grid2.Children.Add(Content)
        End Sub
    End Class
    Window1.xaml

    Code:
    <Window x:Class="Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:WpfInsideWpf"
            mc:Ignorable="d"
            Title="Window1" Height="300" Width="300">
        <Grid Background="#FF40AE45">
            <Button x:Name="ButtonMessageBox" Content="Message Box" HorizontalAlignment="Left" Margin="24,27,0,0" VerticalAlignment="Top" Width="92"/>
            <Button x:Name="ButtonChangeLabel" Content="Change Label" HorizontalAlignment="Left" Margin="24,70,0,0" VerticalAlignment="Top" Width="92"/>
            <Label x:Name="label1" Content="" HorizontalAlignment="Left" Height="22" Margin="157,70,0,0" VerticalAlignment="Top" Width="103" Background="#FF9C4E4E"/>
            <Button x:Name="ButtonExit" Content="Exit" HorizontalAlignment="Left" Margin="24,112,0,0" VerticalAlignment="Top" Width="92"/>
    
        </Grid>
    </Window>
    Window1.xaml.vb

    Code:
    Public Class Window1
        Private Sub button_Click(sender As Object, e As RoutedEventArgs) Handles button.Click
            MessageBox.Show("Hello World")
        End Sub
    
        Private Sub ButtonChangeLabel_Click(sender As Object, e As RoutedEventArgs) Handles ButtonChangeLabel.Click
    
            If label1.Content = "" Then
                label1.Content = "Hello World"
                Return
            End If
    
            If label1.Content = "Hello World" Then
                label1.Content = ""
                Return
            End If
        End Sub
    
        Private Sub ButtonChangeLabel_Copy_Click(sender As Object, e As RoutedEventArgs) Handles ButtonChangeLabel_Copy.Click
            'Exit Programe
            Dim ExitMessage As Integer
            ExitMessage = MsgBox("Do you want to quit?", 1)
            If ExitMessage = 1 Then
                Me.Close()
    
            End If
            Return
        End Sub
    End Class
    Thank you Tony.

  2. #2

    Thread Starter
    Member
    Join Date
    Jan 2013
    Posts
    58

    Re: Close WPF window that is running within another WPF's grid.

    Correction ...

    Window1.xaml.vb should be like this (I changed names for 2 buttons and posted wrong file) Soz.

    Code:
    Public Class Window1
    
        Private Sub ButtonMessageBox_Click(sender As Object, e As RoutedEventArgs) Handles ButtonMessageBox.Click
            MessageBox.Show("Hello World")
        End Sub
    
        Private Sub ButtonChangeLabel_Click(sender As Object, e As RoutedEventArgs) Handles ButtonChangeLabel.Click
    
            If label1.Content = "" Then
                label1.Content = "Hello World"
                Return
            End If
    
            If label1.Content = "Hello World" Then
                label1.Content = ""
                Return
            End If
        End Sub
    
    
    
        Private Sub ButtonExit_Click(sender As Object, e As RoutedEventArgs) Handles ButtonExit.Click
            'Exit Programe
            Dim ExitMessage As Integer
            ExitMessage = MsgBox("Do you want to quit?", 1)
            If ExitMessage = 1 Then
    
    
                Me.Close()
    
            End If
            Return
        End Sub
    
    
    End Class

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.