Blog

Sending any value from one form to another form in WPF

By Purushottam Rathore Sep 15 2010
How to send any value from one form to another form in WPF?
    • Like
    • Love It
    • Awesome
    • Interesting
    • It's Okay
    • Thumbs Down
  • 49.6k
  • 0
Suppose I want to send some values from one form to another form in wpf application, then what to do?

In my application i have two window forms name as
Test.xaml and Welcome.xaml like as follows:

Test.xaml

<
Window x:Class="Login_WPF.Test" mlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
Title="Test" Height="300" Width="300">
<Grid>
   <Button Content="Submit" Height="23" HorizontalAlignment="Left" Margin="30,0,0,150" Name="Ok" VerticalAlignment="Bottom" Width="75" Click="Ok_Click" />
   <TextBox Height="23" HorizontalAlignment="Left" Margin="30,46,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
   <Label Content="Enter your name here" Height="28" HorizontalAlignment="Left" Margin="26,12,0,0" Name="label1" VerticalAlignment="Top" />
</Grid>
</
Window>

Welcome.xaml:

<
Window x:Class="Login_WPF.Welcome"
xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
Title="Welcome" Height="300" Width="350">
<Grid>
   <TextBlock Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" x:Name="WelcomeHeading" Text="Welcome:" VerticalAlignment="Top" FontSize="17" FontStretch="ExtraCondensed"/>
   <TextBlock Height="23" HorizontalAlignment="Left" Margin="90,10,0,0" x:Name="TextBlockName" VerticalAlignment="Top" FontSize="15" FontStretch="ExtraCondensed" />
</Grid>
</
Window>

My first page is Test.xaml. write the following lines of code on button's click event as follows:

Test.xaml.cs:

using
System;
sing System.Collections.Generic;
using System.Linq;
using System.Text;
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;
namespace
Login_WPF
{
   /// <summary>
   /// Interaction logic for Test.xaml
   /// </summary>

   public partial class Test : Window
   {
      public Test()
      {
         InitializeComponent();
      }

      private void Ok_Click(object sender, RoutedEventArgs e)
      {
         Welcome objWelcome = new Welcome();
         objWelcome.TextBlockName.Text = textBox1.Text;
         objWelcome.Show();
//Sending value from one form to another form.
         Close();
      }
   }
}

Output:

Image1.png

Figure 1:

Enter the input in to text box and click on Submit button value will show on next page as follows:

img2.png

Figure 2: