Click here to Skip to main content
15,887,425 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi folks
I wrote a program which opens my second UserControl in the main window. The program runs in visual studio but the result doesn't work. I mean when you click on the button, it doesn't open the second window.
Any kindly help please?
Thanks.

What I have tried:

The code of MainWindow in XAML
XML
<Window x:Name="TheMainWindow" 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:SRT2Nuendo"
        mc:Ignorable="d"
        Title="MyMainWindow"
        Width="1000"
        Height="600"
        WindowStartupLocation="CenterScreen"
        HorizontalAlignment="Center"
        VerticalAlignment="Center"
        WindowStyle="None"
        AllowsTransparency="True"
        BorderThickness="3"
        AllowDrop="False">
    <Grid
        <ContentControl x:Name="CC"/>
    </Grid>
</Window>



The code of MainWindow in VB.NET
VB.NET
Class MainWindow
    Sub New()
        InitializeComponent()
        CC.Content = New UserControlMain1
    End Sub
End Class



This is code of UserControlPlatform in XAML
XML
<UserControl x:Name="ucMain1" x:Class="UserControlMain1"
        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:MainWindow"
        mc:Ignorable="d"
        Width="1000"
        Height="600"
        HorizontalAlignment="Center"
        VerticalAlignment="Center"
        BorderThickness="3"
        AllowDrop="False">
    <Grid
        <Button x:Name="btnOpenNewUC"/>
    </Grid>
</UserControl>



This is code of UserControlMain1 in VB.NET
VB.NET
Public Class UserControlMain1
   Private Sub btnOpenNewUC_Click(sender As Object, e As RoutedEventArgs) Handles btnOpenNewUC.Click
        Dim NewUC = New MainWindow
        NewUC.CC.Content = New UserControlMain2
   End Sub
End Class



This is Code of UserControlMain2 in XAML
XML
<UserControl x:Name="ucMain2" x:Class="UserControlMain2"
        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:MainWindow"
        mc:Ignorable="d"
        Width="1000"
        Height="600"
        HorizontalAlignment="Center"
        VerticalAlignment="Center"
        BorderThickness="3"
        AllowDrop="False">
    <Grid
        <Label x:Name="lbHello" Text="HELLO WORLD!"/>
    </Grid>
</UserControl>
Posted
Updated 6-Dec-23 21:44pm
v2

1 solution

Quote:
VB.NET
Public Class UserControlMain1
   Private Sub btnOpenNewUC_Click(sender As Object, e As RoutedEventArgs) Handles btnOpenNewUC.Click
        Dim NewUC = New MainWindow
        NewUC.CC.Content = New UserControlMain2
   End Sub
End Class
Your code creates a new MainWindow, sets its CC.Content to a new UserControlMain2, and then... does nothing.

It doesn't show the new window. It just throws that instance away.

If you want to show the window, then you need to call its Show or ShowDialog method.
Window.Show Method (System.Windows) | Microsoft Learn[^]
Window.ShowDialog Method (System.Windows) | Microsoft Learn[^]

If you want to change the content shown in the existing window, then you need to update the existing instance, not create a new instance.
Transferring information between two forms, Part 2: Child to Parent[^]
 
Share this answer
 
Comments
Sh.H. 7-Dec-23 8:57am    
Thanks Richard Deeming
I read the article Part2: Child to Parent.
But I couldn't understand the relation of it and my code.
So may I ask you kindly please let me know how may I change the content in he existing window?
Please kindly help me with this code.
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900