Delegate event Definition

Source: Internet
Author: User

Close the Child Window and tell the parent form

[Csharp]
Parent form
Previous paragraph:
<Window x: Class = "RefreshFatherWindow. WindowClose"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "WindowClose" Height = "300" Width = "300">
<StackPanel>
<TextBox Name = "tb"/>
<Button Content = "submit" Click = "Button_Click"/>
</StackPanel>
</Window>
 
Viewcode:
Using System;
Using 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 RefreshFatherWindow
{
/// <Summary>
/// Interaction logic for WindowClose. xaml
/// </Summary>
Public partial class WindowClose: Window
{
Public WindowClose ()
{
InitializeComponent ();
}
 
Private void Button_Click (object sender, RoutedEventArgs e)
{
SonWindow son = new SonWindow ();
 
Son. Closed + = new EventHandler (son_Closed );
Son. Show ();
}
 
Public void son_Closed (object obj, EventArgs e)
{
Tb. Text = "The son window is closed! ";
}
}
}
 
Subform:
Directly draw a form named SonWindow. xaml,
Click the close button X to pass the value to the parent form.

 

Sub-form click to pass the value to the parent form

[Csharp]
Parent form
Design:
<Window x: Class = "RefreshFatherWindow. MainWindow"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "MainWindow" Height = "350" Width = "525">
<StackPanel>
<TextBox Name = "tb"/>
<Button Content = "submit" Click = "Button_Click"/>
</StackPanel>
</Window>
 
ViewCode:
Using System;
Using 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. Navigation;
Using System. Windows. Shapes;
 
Namespace RefreshFatherWindow
{
/// <Summary>
/// Interaction logic for MainWindow. xaml
/// </Summary>
Public partial class MainWindow: Window
{
Public MainWindow ()
{
InitializeComponent ();
}
 
Private void Button_Click (object sender, RoutedEventArgs e)
{
SonWindow son = new SonWindow ();
Son. ChangeTextEvent + = new SonWindow. ChangeTextHandler (ChangeTextEvent );
Son. Show ();
}
 
Void ChangeTextEvent (string text)
{
Tb. Text = text;
}
 
}
}
 
Subform:
Design
<Window x: Class = "RefreshFatherWindow. SonWindow"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "SonWindow" Height = "300" Width = "300">
<Grid>
<Button Content = "Click window effects" Click = "Button_Click"/>
</Grid>
</Window>
 
Viewcode:
Using System;
Using 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 RefreshFatherWindow
{
/// <Summary>
/// Interaction logic for SonWindow. xaml
/// </Summary>
Public partial class SonWindow: Window
{
// Define the delegate
Public delegate void ChangeTextHandler (string text );
 
// Define the event
Public event ChangeTextHandler ChangeTextEvent;
 
 
Public SonWindow ()
{
InitializeComponent ();
}
 
Private void Button_Click (object sender, RoutedEventArgs e)
{
ChangeTextEvent ("I am the effect! ");
}
}
}
 

From pengshumin89

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.