TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Sending any value from one form to another form in WPF
Purushottam Rathore
Dec 03, 2012
104.9
k
0
1
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
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:
Figure 1:
Enter the input in to text box and click on Submit button value will show on next page as follows:
Figure 2:
Sending any value from one form to another form in WPF
Next Recommended Reading
WPF Binding One Way and Two Way