C#中自定义控件中带参数事件的实现

时间:2022-08-31 07:57:42

C#中自定义控件中带参数事件的实现
最近在项目开发中要写一个自定义控件来实现一些功能:单击这个控件的时候要实现通过_Click(object sender, ClickEventArgs e)中的e传出一些参数,具体代码如下所示:

/*---------------------------------------------------------------
 * 名称:信息框
 * 功能:信息框
 * 说明:
 * 作者:刘心愿
 * 版本:2008-07-30       
 * ---------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Mesnac.Common.Win.Controls
{
    /// <summary>
    /// 名    称:可以带参数的事件类
    /// 功    能:可以带参数的事件类
    /// 说    明:
    /// 作    者:刘心愿
    /// 创建时间:
    /// 最近修改:
    /// </summary>
    public partial class ClickEventArgs : EventArgs
    {
        public String KeyValue;
    }

    /// <summary>
    /// 名    称:事件委托
    /// 功    能:事件委托
    /// 说    明:
    /// 作    者:刘心愿
    /// 创建时间:
    /// 最近修改:
    /// </summary>
    public delegate void ClickEventHandler(object sender, ClickEventArgs e);

    public partial class InfoFrame : UserControl
    {
        /// <summary>
        /// 名    称:定义一个事件
        /// 功    能:定义一个事件
        /// 说    明:
        /// 作    者:刘心愿
        /// 创建时间:
        /// 最近修改:
        /// </summary>
        public new event ClickEventHandler Click;

        /// <summary>
        /// 名    称:定义事件的实现函数
        /// 功    能:定义事件的实现函数
        /// 说    明:
        /// 作    者:刘心愿
        /// 创建时间:
        /// 最近修改:
        /// </summary>
        public void OnClickEvent(ClickEventArgs e)
        {
            if (Click != null)
            {
                Click(this, e);
            }
        }

        private string keyValue = "";

        /// <summary>
        /// 名    称:当前控件是否有滚动条
        /// 功    能:当前控件是否有滚动条
        /// 说    明:
        /// 作    者:刘心愿
        /// 创建时间:
        /// 最近修改:
        /// </summary>
        private bool autoScroll = false;
        public new bool AutoScroll
        {
            get
            {
                return autoScroll;
            }
            set
            {
                autoScroll = value;
                if (value)
                    ctrlValue.ScrollBars = ScrollBars.Both;
            }
        }

        /// <summary>
        /// 名    称:使用状态是否可见
        /// 功    能:使用状态是否可见
        /// 说    明:
        /// 作    者:刘心愿
        /// 创建时间:
        /// 最近修改:
        /// </summary>
        public bool UseStateVisible
        {
            get
            {
                return lbUseState.Visible;
            }
            set
            {
                lbUseState.Visible = value;
            }
        }

        /// <summary>
        /// 名    称:使用状态框的背景色
        /// 功    能:使用状态框的背景色
        /// 说    明:
        /// 作    者:刘心愿
        /// 创建时间:
        /// 最近修改:
        /// </summary>
        public Color UseStateColor
        {
            get
            {
                return lbUseState.BackColor;
            }
            set
            {
                lbUseState.BackColor = value;
            }
        }

        /// <summary>
        /// 名    称:控件标题
        /// 功    能:控件标题
        /// 说    明:
        /// 作    者:刘心愿
        /// 创建时间:
        /// 最近修改:
        /// </summary>
        public string Caption
        {
            get
            {
                return ctrlCaption.Text;
            }
            set
            {
                ctrlCaption.Text = value;
            }
        }

        /// <summary>
        /// 名    称:控件的值
        /// 功    能:控件的值
        /// 说    明:
        /// 作    者:刘心愿
        /// 创建时间:
        /// 最近修改:
        /// </summary>
        public string Value
        {
            set
            {
                if (value != "")
                {
                    string strValue = value;
                    string[] strArr = strValue.Split("|".ToCharArray());
                    ctrlValue.ColumnCount = 1;
                    ctrlValue.RowCount = strArr.GetUpperBound(0) + 1;
                    for (int i = 0; i < strArr.GetUpperBound(0) + 1; i++)
                    {
                        ctrlValue[0, i].Value = strArr[i];
                    }
                }
                else
                {
                    ctrlValue.ColumnCount = 1;
                    ctrlValue.RowCount = 0;
                }
            }
        }

        /// <summary>
        /// 名    称:控件的关键值
        /// 功    能:控件的关键值
        /// 说    明:
        /// 作    者:刘心愿
        /// 创建时间:
        /// 最近修改:
        /// </summary>
        public string KeyValue
        {
            set
            {
                keyValue = value;
            }
            get
            {
                return keyValue;
            }
        }

        public InfoFrame()
        {
            InitializeComponent();
            this.ctrlCaption.Click += new EventHandler(ctrlCaption_Click);
            this.ctrlValue.Click += new EventHandler(ctrlValue_Click);
            this.lbUseState.Click += new EventHandler(lbUseState_Click);
        }

        /// <summary>
        /// 名    称:相关联的Click事件
        /// 功    能:相关联的Click事件
        /// 说    明:
        /// 作    者:刘心愿
        /// 创建时间:
        /// 最近修改:
        /// </summary>
        void ctrlValue_Click(object sender, EventArgs e)
        {
            ClickEventArgs f = new ClickEventArgs();
            f.KeyValue = keyValue;
            OnClickEvent(f);
        }

        /// <summary>
        /// 名    称:相关联的Click事件
        /// 功    能:相关联的Click事件
        /// 说    明:
        /// 作    者:刘心愿
        /// 创建时间:
        /// 最近修改:
        /// </summary>
        void ctrlCaption_Click(object sender, EventArgs e)
        {
            ClickEventArgs f = new ClickEventArgs();
            f.KeyValue = keyValue;
            OnClickEvent(f);
        }

        /// <summary>
        /// 名    称:相关联的Click事件
        /// 功    能:相关联的Click事件
        /// 说    明:
        /// 作    者:刘心愿
        /// 创建时间:
        /// 最近修改:
        /// </summary>
        void lbUseState_Click(object sender, EventArgs e)
        {
            ClickEventArgs f = new ClickEventArgs();
            f.KeyValue = keyValue;
            OnClickEvent(f);
        }

    }

  
}