程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 委托事件定義

委托事件定義

編輯:C#入門知識

關閉子窗口告訴父窗體

[csharp]
父窗體 
前段: 
<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 = "兒子窗口已關閉!"; 
        } 
    } 

 
子窗體: 
直接畫個名為SonWindow.xaml的窗體, 
點擊關閉按鈕X就會傳值到父窗體 

 

子窗體點擊後傳值到父窗體

[csharp]
父窗體 
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; 
        } 
 
    } 

 
子窗體: 
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="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 
    { 
        //定義委托 
        public delegate void ChangeTextHandler(string text); 
 
        //定義事件 
        public event ChangeTextHandler ChangeTextEvent; 
 
 
        public SonWindow() 
        { 
            InitializeComponent(); 
        } 
 
        private void Button_Click(object sender, RoutedEventArgs e) 
        { 
           ChangeTextEvent("我就是效果!"); 
        } 
    } 

 

摘自  pengshumin89 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved