• 事件委托应用:在父控件中创建子控件,并接收值


    传值过程使用委托方法

    定义一个打开按扭,一个文本框

     1    /// <summary>
     2         /// 增加父控件
     3         /// </summary> 
     4       public void AddParent(BaseControl ctl)
     5         {
     6             foreach (Control ct2 in this.Parent.Controls) 
     7             { 
     8                 if (ct2.Name == ctl.Name) {
     9                     ct2.Focus();
    10                     return;
    11                 } 
    12             } 
    13             ctl.Width = 400; 
    14             ctl.Dock = DockStyle.Right;
    15             ctl.DisposedPassingChanged += Ctl_DisposedPassingChanged;//用于在【父>子】控件关闭时接收值
    16             //this.textEdit1.Text = ( ctl.Passing).Text;
    17               Console.WriteLine("方法4:开始创建子窗体");
    18             //ctl.Parent = this; 
    19             this.Parent.Controls.Add(ctl); 
    20         }
    21 
    22         //用于在【父>子】控件关闭时接收值
    23         private void Ctl_DisposedPassingChanged(object sender, PassingEventArgs e)
    24         {
    25             ShowSunControl source = sender as ShowSunControl;
    26             if(source!=null) 
    27             Console.WriteLine("方法5.1:已调用:" + source.Name.ToString());
    28             this.textEdit1.Text = ((DevExpress.XtraEditors.TextEdit)source.Passing).ToString();
    29             Console.WriteLine("方法5.2:已接受返回来的数据:" + source.Name.ToString());
    30         }

    定义一个关闭按扭,一个文本框

     1    public ShowSunControl()
     2         {
     3             InitializeComponent();
     4             this.barButtonItem1.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(this.barButtonItem1_ItemClick);
     5             this.Passing = this.textEdit1;//传值绑定
     6             Console.WriteLine("方法1:窗体加载"  ); 
     7         }
     8      
     9         // 关闭的时候执行
    10         private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
    11         {
    12             Console.WriteLine("方法2:您单击了关闭窗体。");
    13               this.Dispose();//触发传值
    14         }
    15 
    16         // 操作的时候执行
    17         private void textEdit1_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
    18         { 
    19             Console.WriteLine("方法3:您正在输入值。");
    20 
    21         }

    BaseControl.cs

    定义委托、自定义事件

     1  public partial class BaseControl :  UserControl 
     2     {
     3         object passing;
     4         public object Passing
     5         {
     6             get { return passing; }
     7             set { passing = value; }
     8         }
     9         public object LogoPassing
    10         {
    11             get { return passing; }
    12             set { passing = value; }
    13         }
    14         public BaseControl():this(null)  
    15         {
    16             InitializeComponent();
    17         }
    18 
    19         public BaseControl(object value)
    20         {
    21             InitializeComponent();
    22             this.Disposed += BaseControl_Disposed1; 
    23             Passing = value; 
    24         }
    25         //事件发行者 关闭调用的时候先执行
    26         private void BaseControl_Disposed1(object sender, EventArgs e)
    27         {
    28             DisposedPassingChanged?.Invoke(this, new PassingEventArgs(Passing));
    29                Console.WriteLine("方法0:DisposedPassingChanged 已发生事件,目标已传出,查看调用窗体是否接收到数据。");
    30                this.Dispose();
    31         }
    32          
    33         public delegate void PassingEventHandler(object sender, PassingEventArgs e);
    34         public event PassingEventHandler DisposedPassingChanged;
    35        
    36     }
    37     public class PassingEventArgs :   EventArgs
    38     {
    39         private object passing = string.Empty;
    40         public PassingEventArgs(object args) 
    41         {
    42             passing = args;
    43         }
    44         public object Passing 
    45         {
    46             get { return passing; }
    47         }
    48     
    49     }

    程序调用后,Console.WriteLine 输入结果如下:

    1 //方法1:窗体加载
    2 //方法4:开始创建子窗体
    3 //方法3:您正在输入值。
    4 //方法2:您单击了关闭窗体。
    5 //方法5:已调用:ShowSunControl
    6 //方法5:已接受返回来的数据:ShowSunControl
    7 //方法0:DisposedPassingChanged 已发生事件,目标已传出,查看调用窗体是否接收到数据。

    窗体中也接收到了数据,

  • 相关阅读:
    Netty ChannelHandler组件作用
    Netty Channel组件作用
    Netty NioEventLoop自定义任务处理
    NIO与BIO
    JDK ByteBuffer与Netty ByteBuf
    linux-源码软件管理-yum配置
    typora使用快捷键
    远程连接mysql库问题
    MVC 后台处理 json格式的日期
    使用 SqlClient 创建数据库连接并获取数据
  • 原文地址:https://www.cnblogs.com/endv/p/7152832.html
Copyright © 2020-2023  润新知