Home > Software engineering >  Get data from another window
Get data from another window

Time:11-03

Project Informations

  • Windows Presentation Foundation Project
  • C# as programming language

Description

Is it possible to receive data from an user input in Window1 and show this input in MainWindow?

I will open the user input as a file from Window1 and will show the content of it in the RichTextBox of MainWindow.

Window1

Licensed under my license on Github

<Window x:Name="window1" x:Class="Writer.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:Writer"
        mc:Ignorable="d"
        Title="New" Height="130" Width="600" WindowStyle="None" WindowStartupLocation="CenterScreen" ResizeMode="CanMinimize">
    <Grid Margin="0,0,0,-2">
        <Rectangle HorizontalAlignment="Center" Height="30" Stroke="Black" VerticalAlignment="Top" Width="600" Fill="Black"/>
        <Label Content="Open" HorizontalAlignment="Left" Margin="0,2,0,0" VerticalAlignment="Top" Foreground="White"/>
        <Button Content="X" Margin="579,5,10,0" VerticalAlignment="Top" Background="Black" Foreground="White" BorderBrush="Black" ToolTip="Exit" Focusable="False" IsTabStop="False" Click="Button_Click"/>
        <Label Content="Select the path" HorizontalAlignment="Left" Margin="0,35,0,0" VerticalAlignment="Top"/>
        <TextBox HorizontalAlignment="Left" Margin="5,61,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" IsTabStop="False"/>
        <Button Content="Select" HorizontalAlignment="Left" Margin="5,84,0,0" VerticalAlignment="Top" BorderBrush="White" Background="#FFADADAD" Width="45"/>
    </Grid>
</Window>

MainWindow

Licensed under my license on Github

<Window x:Name="MainWindow1" x:Class="Writer.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:Writer"
        mc:Ignorable="d"
        Title="Writer" Height="450" Width="800" WindowStyle="None" WindowStartupLocation="CenterScreen" ResizeMode="CanMinimize">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="164*"/>
            <RowDefinition Height="83*"/>
            <RowDefinition Height="203*"/>
        </Grid.RowDefinitions>
        <Rectangle HorizontalAlignment="Center" Height="30" Stroke="Black" VerticalAlignment="Top" Width="800" Fill="Black"/>
        <Label Content="Writer" HorizontalAlignment="Left" Margin="0,2,0,0" VerticalAlignment="Top" Foreground="White"/>
        <Button Content="_" HorizontalAlignment="Left" Margin="765,5,0,0" VerticalAlignment="Top" Background="Black" Foreground="White" BorderBrush="Black" Focusable="False" ToolTip="Minimize" IsTabStop="False" ClickMode="Press" Click="Button_Click_1"/>
        <Button Content="X" Margin="779,5,10,0" VerticalAlignment="Top" Background="Black" Foreground="White" BorderBrush="Black" ToolTip="Exit" Focusable="False" IsTabStop="False" Click="Button_Click"/>
 
        <Menu Margin="0,30,0,114" Height="20">
            <MenuItem Header="File">
                <MenuItem Header="New">
                    <MenuItem.Icon>
                        <Image Source="/file-added.svg"/>
                    </MenuItem.Icon>
                </MenuItem>
                <MenuItem Header="Open" Click="MenuItem_Click"/>
                <MenuItem Header="Open from server"/>
                <MenuItem Header="Save"/>
                <MenuItem Header="Save as"/>
                <MenuItem Header="Close file"/>
                <MenuItem Header="Close folder"/>
            </MenuItem>
            <MenuItem Header="Start">
                <MenuItem Header="Font">
                    <MenuItem Header="Font"/>
                    <MenuItem Header="Family"/>
                    <MenuItem Header="Size"/>
                </MenuItem>
                <Separator/>
                <MenuItem Header="Bold"/>
                <MenuItem Header="Italic"/>
                <MenuItem Header="Underline"/>
                <MenuItem Header="Strikethrough"/>
            </MenuItem>
            <MenuItem Header="Insert">
                <MenuItem Header="New site"/>
            </MenuItem>
            <MenuItem Header="Layout">
                
            </MenuItem>
            <MenuItem Header="View">
                
            </MenuItem>
            <MenuItem Header="Help"/>
        </Menu>
        <RichTextBox x:Name="RichTextBox1" Margin="0,50,0,0" BorderBrush="White" Cursor="Arrow" IsTabStop="False" Grid.RowSpan="3" FontFamily="Segoe UI" BorderThickness="0,0,0,0">
            <FlowDocument>
                <Paragraph>
                    <Run Text=""/>
                </Paragraph>
            </FlowDocument>
        </RichTextBox>
    </Grid>
</Window>

CodePudding user response:

Yes, there is a way.

Implement a public method in Window1 that gives you the data. Then use the reference to Window1 and call that method. If MainWindow does not have a reference to Window1, give it the reference.

Here's an example. It assumes that you open the second window from within the main window.

In addition to the CC-BY-SA license of Stack Overflow, I license this as CC-0 for anyone who needs this code in one of his projects.

MainWindow.xaml

<Window x:Class="GetDataFromOtherWindow.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"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <StackPanel>
        <Button Content="Open other window" Click="Button_Click" />
        <Button Content="Get data from other window" Click="Button_Click_1" />
    </StackPanel>
</Window>

MainWindow.xaml.cs

using System.Windows;

namespace GetDataFromOtherWindow
{
    public partial class MainWindow : Window
    {
        private Window1? otherWindow;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            otherWindow = new Window1();
            otherWindow.Show();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (otherWindow != null)
            {
                MessageBox.Show(otherWindow.GetData());
            }
        }
    }
}

Window1.xaml

<Window x:Class="GetDataFromOtherWindow.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"
        mc:Ignorable="d"
        Title="Window1" Height="141" Width="400">
    <StackPanel>
        <RichTextBox Name="richBox">
            <FlowDocument>
                <Paragraph>
                    <Run Text="This is some text"/>
                </Paragraph>
            </FlowDocument>
        </RichTextBox>
    </StackPanel>
</Window>

Window1.xaml.cs

using System.Windows;
using System.Windows.Documents;

namespace GetDataFromOtherWindow
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        public string GetData()
        {
            TextRange textRange = new TextRange(
                richBox.Document.ContentStart,
                richBox.Document.ContentEnd
            );
            return textRange.Text;
        }
    }
}

Note that this is not very MVVM-friendly, because this code has no model which would know about the business logic.

As per the comments, if you want to show the data after closing Window1, you can do this:

MainWindow.xaml.cs

using System.Windows;

namespace GetDataFromOtherWindow
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var otherWindow = new Window1();
            otherWindow.ShowDialog();
            MessageBox.Show(otherWindow.GetData());
        }
    }
}

Code for getting the data just before closing the window:

using System.Windows;

namespace GetDataFromOtherWindow
{
    public partial class MainWindow : Window
    {
        private Window1? otherWindow;

        public MainWindow()
        {
            InitializeComponent();
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            if (otherWindow != null)
            {
                MessageBox.Show(otherWindow.GetData());
            }

            base.OnClosing(e);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            otherWindow = new Window1();
            otherWindow.Show();
        }
    }
}
  • Related