0

I have managed to get Form1 to open in my grid on the MainWindow but now I need to close Form1 and show Form2 in the same grid on the MainWindow after I click a button on Form1.

How can this be done?

MainWindow Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.Configuration;
using Application = System.Windows.Application;
using System.Windows.Forms.Integration;

namespace SolAquaPro
{
    /// <summary>
    /// Interaction logic for winHomeDash.xaml
    /// </summary>
    public partial class WinHomeDash : Window
    {
        public WinHomeDash()
        {
            InitializeComponent();
        }

        public void BtnFuel_Click(object sender, RoutedEventArgs e)
        {
            FuelDash fd1 = new FuelDash();
            fd1.TopLevel = false;

            WindowsFormsHost host1 = new WindowsFormsHost();
            host1.Child = fd1;

            stackForms.Children.Add(host1);            
        }
     }
}

The MainWindow Design Code is:

<Window x:Class="SolAquaPro.WinHomeDash"
        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:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
        xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
        xmlns:local="clr-namespace:SolAquaPro"
        mc:Ignorable="d"
        Title="winHomeDash" Height="650" Width="950"
        WindowStartupLocation="CenterScreen"
        WindowStyle="None"
        materialDesign:ThemeAssist.Theme="Dark"
        Background="{x:Null}" Loaded="BtnMenuShort_Click">

    <materialDesign:Card UniformCornerRadius="15" Margin="25" Width="900" Height="600" Background="{DynamicResource ResourceKey=MaterialDesignDarkBackground}">
        
        <materialDesign:Card UniformCornerRadius="15" HorizontalAlignment="Left" Width="225" Background="#0A1828">
            
            <DockPanel Width="225" Height="600">
                <StackPanel Height="155" VerticalAlignment="Top" Width="225">
                    <Image Height="50" Width="50" Margin="0 15 0 15" Source="/UserPic.png"/>
                    <Label Name="lblName" BorderThickness="0" FontFamily="Arial" FontWeight="Bold" FontSize="17" Margin="0 0 0 5" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Foreground="#FF2372FA" Height="24" Loaded="Window_Loaded"/>
                    <Label x:Name="lblDateTime" Content="" FontFamily="Arial" FontWeight="SemiBold" FontSize="12" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Foreground="#FF2372FA" Margin="0 0 0 18" Height="22" Width="225" Loaded="LblDateTime_Loaded"/>
                    <Rectangle Name="rec1" Height="7" Fill="#FF2372FA"/>
                </StackPanel>
                <materialDesign:Card Name="card2" Width="210" UniformCornerRadius="15" Height="445" Background="{DynamicResource ResourceKey=MaterialDesignDarkBackground}" Foreground="White" Margin="-210,155,0,0">

                    <StackPanel VerticalAlignment="Center" Height="445" Name="stpnlMenu">
                        <Button x:Name="btnCollapse" Width="15" Height="30" BorderBrush="{x:Null}" Foreground="{x:Null}" FlowDirection="RightToLeft" Margin="195,207.5,0,0" Click="BtnMenuShort_Click">
                            <Button.Background>
                                <ImageBrush ImageSource="/more_than_25px.png"/>
                            </Button.Background>
                        </Button>
                        <Button x:Name="btnMenu" Content="MENU" HorizontalContentAlignment="Left" Width="160" Height="20" Margin="0,-227.5,0,0" FontFamily="Arial" FontSize="10" FontWeight="SemiBold" Background="{x:Null}" BorderBrush="{x:Null}" Click="BtnMenuShort_Click" VerticalAlignment="Top" Cursor="Hand" Tag="tagMenu"/>
                        <Button x:Name="btnHome" Content="HOME" HorizontalContentAlignment="Left" Width="160" Height="20" Margin="0,-187.5,0,0" FontFamily="Arial" FontSize="10" FontWeight="SemiBold" Background="{x:Null}" BorderBrush="{x:Null}" VerticalAlignment="Top" Cursor="Hand" Tag="tagMenu"/>
                        <Button x:Name="btnFuel" Content="FUEL" HorizontalContentAlignment="Left" Width="160" Height="20" Margin="0,-147.5,0,0" FontFamily="Arial" FontSize="10" FontWeight="SemiBold" Background="{x:Null}" BorderBrush="{x:Null}" RenderTransformOrigin="-0.836,17.714" VerticalAlignment="Top" Cursor="Hand" Tag="tagMenu" Click="BtnFuel_Click"/>
                        <Button x:Name="btnTyres" Content="TYRES" HorizontalContentAlignment="Left" Width="160" Height="20" Margin="0,-122.5,0,0" FontFamily="Arial" FontSize="10" FontWeight="SemiBold" Background="{x:Null}" BorderBrush="{x:Null}" RenderTransformOrigin="-0.836,17.714" VerticalAlignment="Top" Cursor="Hand" Tag="tagMenu"/>
                        <Button x:Name="btnService" Content="SERVICE" HorizontalContentAlignment="Left" Width="160" Height="20" Margin="0,-97.5,0,0" FontFamily="Arial" FontSize="10" FontWeight="SemiBold" Background="{x:Null}" BorderBrush="{x:Null}" RenderTransformOrigin="-0.836,17.714" VerticalAlignment="Top" Cursor="Hand" Tag="tagMenu"/>
                        <Button x:Name="btnBags" Content="WASTE BAGS" HorizontalContentAlignment="Left" Width="160" Height="20" Margin="0,-72.5,0,0" FontFamily="Arial" FontSize="10" FontWeight="SemiBold" Background="{x:Null}" BorderBrush="{x:Null}" RenderTransformOrigin="-0.836,17.714" VerticalAlignment="Top" Cursor="Hand" Tag="tagMenu"/>
                        <Button x:Name="btnExpand" Width="15" Height="30" BorderBrush="{x:Null}" Foreground="{x:Null}" FlowDirection="LeftToRight" Margin="25,-30,0,0" Click="BtnMenuShort_Click" Visibility="Hidden">
                            <Button.Background>
                                <ImageBrush ImageSource="/more_than_25px.png"/>
                            </Button.Background>
                        </Button>
                        <Button x:Name="btnChat" Content="QUICK CHAT" HorizontalContentAlignment="Left" Width="160" Height="20" Margin="0,122.5,0,0" FontFamily="Arial" FontSize="10" FontWeight="SemiBold" Background="{x:Null}" BorderBrush="{x:Null}" RenderTransformOrigin="-0.836,17.714" VerticalAlignment="Top" Cursor="Hand" Tag="tagMenu"/>
                        <Button x:Name="btnHelp" Content="SUPPORT" HorizontalContentAlignment="Left" Width="160" Height="20" Margin="0,5,0,0" FontFamily="Arial" FontSize="10" FontWeight="SemiBold" Background="{x:Null}" BorderBrush="{x:Null}" RenderTransformOrigin="-0.836,17.714" VerticalAlignment="Top" Cursor="Hand" Tag="tagMenu"/>
                        <Button x:Name="btnLogOut" Content="LOG OUT" HorizontalContentAlignment="Left" Width="160" Height="20" Margin="0,5,0,20" FontFamily="Arial" FontSize="10" FontWeight="SemiBold" Background="{x:Null}" BorderBrush="{x:Null}" RenderTransformOrigin="-0.836,17.714" Click="BtnLogOut_Click" VerticalAlignment="Top" Cursor="Hand" Tag="tagMenu"/>

                    </StackPanel>
                </materialDesign:Card>

                <StackPanel Width="30" Height="445" Margin="-400,155,0,0" Background="#0A1828">
                    <Rectangle Name="rec2" Height="10"/>
                    <Button Name="btnMenuShort" BorderBrush="{x:Null}" Foreground="{x:Null}" Height="20" Click="BtnMenuShort_Click" Cursor="Hand">
                        <Button.Background>
                            <ImageBrush ImageSource="/menu_30px.png" Stretch="Uniform"/>
                        </Button.Background>
                    </Button>

                    <Rectangle Name="rec3" Height="20"/>
                    <Button Name="btnHomeShort" BorderBrush="{x:Null}" Foreground="{x:Null}" Height="20" Width="20" Cursor="Hand">
                        <Button.Background>
                            <ImageBrush ImageSource="/home_30px.png" Stretch="Fill"/>
                        </Button.Background>
                    </Button>

                    <Rectangle Name="rec4" Height="20"/>
                    <Button Name="btnFuelShort" BorderBrush="{x:Null}" Foreground="{x:Null}" Height="20" Width="20" Margin="0,0,0,5" Cursor="Hand" Click="BtnFuel_Click">
                        <Button.Background>
                            <ImageBrush ImageSource="/gas_station_30px.png" Stretch="Fill"/>
                        </Button.Background>
                    </Button>

                    <Button Name="btnTyresShort" BorderBrush="{x:Null}" Foreground="{x:Null}" Height="20" Width="20" Margin="0,0,0,5" Cursor="Hand">
                        <Button.Background>
                            <ImageBrush ImageSource="/wheel_30px.png" Stretch="Fill"/>
                        </Button.Background>
                    </Button>

                    <Button Name="btnServiceShort" BorderBrush="{x:Null}" Foreground="{x:Null}" Height="20" Width="20" Margin="0,0,0,5" Cursor="Hand">
                        <Button.Background>
                            <ImageBrush ImageSource="/maintenance_30px.png" Stretch="Fill"/>
                        </Button.Background>
                    </Button>

                    <Button Name="btnBagsShort" BorderBrush="{x:Null}" Foreground="{x:Null}" Height="20" Width="20" Margin="0,0,0,5" Cursor="Hand">
                        <Button.Background>
                            <ImageBrush ImageSource="/waste_30px.png" Stretch="Fill"/>
                        </Button.Background>
                    </Button>

                    <Rectangle Name="rec5" Height="170"/>
                    <Button Name="btnChatShort" BorderBrush="{x:Null}" Foreground="{x:Null}" Height="20" Width="20" Margin="0,0,0,5" Cursor="Hand">
                        <Button.Background>
                            <ImageBrush ImageSource="/chat_30px.png" Stretch="Fill"/>
                        </Button.Background>
                    </Button>

                    <Button Name="btnHelpShort" BorderBrush="{x:Null}" Foreground="{x:Null}" Height="20" Width="20" Margin="0,0,0,5" Cursor="Hand">
                        <Button.Background>
                            <ImageBrush ImageSource="/help_30px.png" Stretch="Fill"/>
                        </Button.Background>
                    </Button>

                    <Button Name="btnLogOutShort" BorderBrush="{x:Null}" Foreground="{x:Null}" Height="20" Width="20" Margin="0,0,0,5" Click="BtnLogOut_Click" Cursor="Hand">
                        <Button.Background>
                            <ImageBrush ImageSource="/exit_sign_30px.png" Stretch="Fill"/>
                        </Button.Background>
                    </Button>
                    <Rectangle Name="rec6" Height="10"/>

                </StackPanel>

                <materialDesign:Clock Name="clClock" Width="675" Height="22" FontFamily="Arial" FontWeight="SemiBold" FontSize="10" Foreground="White" Margin="0,578,0,0"/>
                <Grid  Width="625" Height="550" Margin="25,25,25,25" Name="stackForms" Background="#FF292929">
                    
                </Grid>

            </DockPanel>            
                
        </materialDesign:Card>      

    </materialDesign:Card>
</Window>

Form1(FuelDash) loads perfectly into stackForms but now I need to Load Form2(FuelEntry) into stackForms when I click BtnFuelLoad .

Form1 code is:

using System;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
using static SolAquaPro.WinHomeDash;

namespace SolAquaPro
{
    public partial class FuelDash : Form
    {
        public FuelDash()
        {
            InitializeComponent();

        }

        private void Timer1_Tick(object sender, EventArgs e)
        {

        }

        private void BtnFuelLoad_Click(object sender, EventArgs e)
        {
            this.Close();
            


        }

        private void BtnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

I know I need to reference an object in WinHomeDash but I keep getting need to reference non static object.

Filburt
  • 17,626
  • 12
  • 64
  • 115
TravO
  • 27
  • 5
  • 1
    Do you really need to mix WPF and Winforms? Just asking. You could publish an event from FuelDash and react on it in WinHomeDash. – Klaus Gütter Dec 23 '21 at 12:50
  • If you're wrting a new app, i would also wonder why not choose just one ui technology. – Andy Dec 23 '21 at 13:19
  • https://stackoverflow.com/questions/58048031/how-to-access-a-mainwindow-variable-from-a-page-in-c-sharp-wpf – ASh Dec 23 '21 at 13:34
  • so while I'm creating this app instead of right-clicking 'References' -> 'Add New' -> 'Window (WPF)' -> 'Windows Forms' -> 'Forms (Windows Forms)' What should I rather be adding each time I want to go to a new DashBoard or Entry Form? – TravO Dec 23 '21 at 13:42
  • WPF is not windows forms. Two different things. Don't add any reference to windows forms. Use new wpf windows. Or host new wpf usercontrols in the one wpf main window for a single window app. – Andy Dec 23 '21 at 15:26
  • Alternatively. Create a new windows forms app and use windows forms. And not WPF. But just choose one. – Andy Dec 23 '21 at 15:26

1 Answers1

0

Window Forms has nothing to do with a form in sense of GUI. It's a platform. WPF is the successor of Windows Forms.

In WPF you extend Window instead of Form. Just delete all references to Windows Forms.
Since you are trying ti implement a singe page application, you would replace the Grid with a Frame. Then let all your pages extend Page.

WinHomeDash.xaml

<Window>
  <StackPanel>
    <!-- Navigation pane -->
    <StackPanel Orientation="Horizontal">
     
      <!-- Example how to navigate from outside the pages -->

      <Button Click="LoadForm1_OnClick" Content="Form 1" />
      <Button Click="LoadForm2_OnClick" Content="Form 2" />
    </StackPanel>

    <Frame x:Name="stackForms" 
           NavigationUIVisibility="Hidden" />
  </StackPanel>
</Window>

WinHomeDash.xaml.cs

partial class WinHomeDash : Window
{
  private void LoadForm1_OnClick(object sender, RoutedEventArgs e)
  {
    Page form1 = new Form1();
    this.stackForms.Navigate(form1);
  }

  private void LoadForm2_OnClick(object sender, RoutedEventArgs e)
  {
    Page form2 = new Form2();
    this.stackForms.Navigate(form2);
  }
}

Then add a new item the project and select Page. This creates a .xaml file with a nested file containig the partial class code-behind file:

Form1.xaml

<Page>
  <StackPanel>
    <TextBlock Text="This is the first page." />

    <!-- Example how to navigate from within the page -->
    <Button Content="Navigate to next page" 
            Click="LoadForm2_OnClick" />
</Page>

Form1.xaml.cs

public partial class Form1 : Page
{
  public Form1()
  {
    InitializeComponent();
  }

  private void LoadForm2_OnClick(object sender, RoutedEventArgs e)
  {
    Page form2 = new Form2();
    this.NavigationService.Navigate(form2);
  }
}

Form2.xaml

<Page>
  <StackPanel>
    <TextBlock Text="This is the first page." />

    <!-- Example how to navigate from within the page -->
    <Button Content="Navigate to previous page" 
            Click="LoadPreviousForm_OnClick" />
</Page>

Form2.xaml.cs

public partial class Form2 : Page
{
  public Form1()
  {
    InitializeComponent();
  }

  private void LoadPreviousForm_OnClick(object sender, RoutedEventArgs e)
  {    
    // Navigate to previous page using the history
    this.NavigationService.GoBack()

    // Navigate to previous page using an explicit content
    Page form1 = new Form1();
    this.NavigationService.Navigate(form1);
  }
}

Since Frame is a heavyx control as it also supports to render web sources, you should use a ContentControl an DataTemplate to implement the page hosting:
C# WPF Navigation Between Pages (Views)
How do I toggle between pages in a WPF application?

BionicCode
  • 1
  • 4
  • 28
  • 44