Blog

Sending any value from one form to another form in WPF

Posted by Purushottam Rathore Blogs | WPF Sep 15, 2010
How to send any value from one form to another form in WPF?
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:
post comment
     

doesnt work

Posted by sumit hingu Dec 03, 2012

Ho this is really a nice article. I have found one more please checkhttp://dotnetpools.com/Article/ArticleDetiail/?articleId=47&title=Code%20To%20Pass%20Value%20From%20One%20Windows%20Form%20To%20Another%20Windows20%20Form%20In%20Windows%20Form%20Using%20C#

Posted by vinay singh Oct 20, 2012

Good one.................

Posted by Jamil Akhtar Feb 02, 2012

Good one.................

Posted by Jamil Akhtar Feb 02, 2012
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter