zoukankan      html  css  js  c++  java
  • 事件应用于子窗体传递验证数据给主窗体,子窗体用于授权

    概述:事件使用流程

    定义事件委托

    →定义事件所包含的数据类(相当于实体类,用于存放数据)

    →定义引发事件的方法(可在一个方法中定义,也可在多个方法中通过链式调用实现)

    →定义处理事件的方法(可定义事件的类中实现,也可在其它类中实现)

    →实例化事件委托

    注意:以上每一个步骤可根据需要在不同的类中分别实现,也可一个类全部实现(比如自定义控件中的事件处理)。

    以下代码是事件应用于授权子窗体传递验证数据给主窗体的案例,常见事件应用还可用于ERP系统中单据流中的数据传递

    一、事件定义类,即子窗体

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using DataMaintenance.Services;
    using DataMaintenance.Modle;
    using Utility;
    
    namespace DataMaintenance.UI
    {
        public partial class Frm_authorization : Form
        {
            public Frm_authorization()
            {
                InitializeComponent();
                initializeDatasource();
            }
    
         
    
    
            /// <summary>
            /// 初始化控件数据源
            /// </summary>
            private void initializeDatasource()
            {
                cmb_user.DataSource = new UserService().getUserList().Where<UserModle>(c => c.DateOfCancellation == null).Select((c) => new { c.userID, c.name,c.pwd,c.RegistrationDate }).ToList();
    
                cmb_user.DisplayMember = "name";
                cmb_user.ValueMember = "userID";
    
               
            }
    
            #region 事件
    
            public event EventHandler<AuthorizationEventArgs> authorizPass;
    
            /// <summary>
            /// 事件处理方法,调用端实例化事件委托后就执行该方法
            /// authorizPass?本质上相当于执行方法时的第二次判断
            /// 引发事件是执行事件处理方法的第一次判断
            /// </summary>
            /// <param name="authorizationEventArgs"></param>
            protected virtual void onAuthorizPass(AuthorizationEventArgs authorizationEventArgs)
            {
                authorizPass?.Invoke(this, authorizationEventArgs);
            }
    
            #endregion
    
            private void btn_certain_Click(object sender, EventArgs e)
            {
                //this.DialogResult = DialogResult.OK;
                if (txt_pwd.Text!="")
                {
                    string pwd = Encrypt.Encode(txt_pwd.Text);
                  
    
                    if (new UserService().loginCheck(Convert.ToInt32(cmb_user.SelectedValue), pwd))
                    {
                        //引发事件并传递事件包含的数据,该事件检验用户名密码是否正确
                        AuthorizationEventArgs authorizationEventArgs = new AuthorizationEventArgs();
                        authorizationEventArgs.userAndPwdRight = true;
                        onAuthorizPass(authorizationEventArgs);
                        
                        this.Close();
                    }
    
                    
                    else
                    {
                       
                        lbl_information.Text = "";
                        lbl_information.Text = lbl_information.Text + "您输入受权密码不正确,请重新输入";
    
                    }
    
                }
                else
                {
                    lbl_information.Text = "";
                    lbl_information.Text = "请输入受权密码";
                   
                }
            
            }
    
            private void btn_cancel_Click(object sender, EventArgs e)
            {
                this.DialogResult = DialogResult.Cancel;
               
            }
        }
    
    
        /// <summary>
        /// 事件数据类,传递验证成功数据,类似于模型类,用于存储数据
        /// </summary>
        public class AuthorizationEventArgs : EventArgs
    
        {
            public bool userAndPwdRight { get; set; }
        }
    }
    View Code

    二、事件订阅类,即主窗体

       /// <summary>
            /// 更新ERP数据库中的模板参数
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btn_mend_Click(object sender, EventArgs e)
            {
                Frm_authorization f = new Frm_authorization();
                f.authorizPass += modifyTemple;
    
                f.ShowDialog();
    
                         
                    if (f.DialogResult == DialogResult.Cancel)
                    {
                        f.Close();
                    }
                  
                
               
    
            }
    
    
            private void modifyTemple( object sender,AuthorizationEventArgs authorizationEventArgs)
            {
                int editAble;
                List<SqlParameter> sqlParameterList = new List<SqlParameter>();
                SqlParameter sqlParameter1 = new SqlParameter("@VT_ID", "8173");
                SqlParameter sqlParameter2 = new SqlParameter("@FieldName", "itaxprice");
    
                SqlParameter sqlParameter3 = new SqlParameter("@vt_cardnumber", "88");
                SqlParameter sqlParameter4 = new SqlParameter("@VT_TemplateMode", "0");
    
                string sql = "update voucheritems_base set CanModify = @editAble where VT_ID = @VT_ID and FieldName =@FieldName; ";
                string sql2 = "update vouchertemplates_base set VT_TemplateMode=@VT_TemplateMode where vt_cardnumber =@vt_cardnumber and vt_id = @VT_ID ";
    
                if (rb_noEedit.Checked)
    
                {
                    editAble = 0;
                    SqlParameter sqlParameterEditAble = new SqlParameter("@editAble", editAble);
                    sqlParameterList.Add(sqlParameterEditAble);
    
                };
                if (rb_edit.Checked)
                {
                    editAble = 1;
                    SqlParameter sqlParameterEditAble = new SqlParameter("@editAble", editAble);
                    sqlParameterList.Add(sqlParameterEditAble);
                }
    
                sqlParameterList.Add(sqlParameter1);
                sqlParameterList.Add(sqlParameter2);
                sqlParameterList.Add(sqlParameter3);
                sqlParameterList.Add(sqlParameter4);
    
    
                try
                {
                    int influnceRows1 = Sqlhelper.UpdateWithparameters(sql + sql2, sqlParameterList.ToArray());
                    //int influnceRows2=Sqlhelper.UpdateWithparameters(sql2, sqlParameterList.ToArray());
                    //authorizationLog();
                    MessageBox.Show("更新" + influnceRows1 + "条记录,授权成功", "授权提示");
                }
                catch (Exception ex)
                {
    
                    MessageBox.Show("授权出错:" + ex.Message + ex.InnerException, "授权提示");
                }
            }
    View Code
  • 相关阅读:
    本地复现Zabbix v2.2.x, 3.0.0-3.0.3 jsrpc 参数 profileIdx2 SQL 注入漏洞
    本地搭建复现st2-045漏洞
    Ubuntu安装Vulapps漏洞靶场
    如何在腾讯云Ubuntu服务器安装kali下的神器
    nginx 跳转配置
    Chocolatey 的安装
    MySQL 5.1 主从同步配置
    针对Windows Server 2008 Web 服务 IIS+php 配置的一些心得
    解决IIS7+php的组合上传限制30M的问题
    ssh 文件权限影响登录
  • 原文地址:https://www.cnblogs.com/windy3417/p/13945912.html
Copyright © 2011-2022 走看看