网站首页  汉语字词  英语词汇  考试资料  写作素材  旧版资料

请输入您要查询的考试资料:

 

标题 c#自定义控件中事件的处理
内容
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    namespace ClientControl
    {
    //1.定义委托
    public delegate void NewsClickEventHandle(object sender,NewsEventArg args);
    public partial class NewsStage : Control
    {
    //2.定义事件
    public event NewsClickEventHandle NewsClicked;
    private Graphics g;
    private bool isMouseOn = false;
    public NewsStage()
    {
    InitializeComponent();
    //3.事件触发,这样少了事件注册,我们在其他窗体中引用控件时只需要注册事件和编辑事件处理程序即可,可以对比上一篇博客
    this.Click += new EventHandler(NewsStage_Click);
    this.MouseMove += new MouseEventHandler(NewsStage_MouseMove);
    this.MouseLeave += new EventHandler(NewsStage_MouseLeave);
    }
    void NewsStage_MouseLeave(object sender, EventArgs e)
    {
    isMouseOn = false;
    this.Invalidate();
    }
    void NewsStage_MouseMove(object sender, MouseEventArgs e)
    {
    isMouseOn = true;
    this.Invalidate();
    }
    //新闻被点击 事件触发方法
    void NewsStage_Click(object sender, EventArgs e)
    {
    if (_NewsID>=0&&_NewsTitle!="")
    {
    NewsEventArg myArgs = new NewsEventArg(_NewsID,_NewsTitle);
    NewsClicked(this, myArgs);
    }
    }
    private int _NewsID = 0;
    [Description("新闻ID"), Category("Appearance")]
    public int NewsID
    {
    get { return _NewsID; }
    set
    {
    _NewsID = value;
    this.Invalidate();
    }
    }
    /// <summary>
    /// 新闻标题
    /// </summary>
    private string _NewsTitle = "";
    [Description("新闻标题"), Category("Appearance")]
    public string NewsTitle
    {
    get { return _NewsTitle; }
    set
    {
    _NewsTitle = value;
    this.Invalidate();
    }
    }
    private Color _MouseOnColor = new Color();
    [Description("鼠标划上的样色"), Category("Appearance")]
    public Color MouseOnColor
    {
    get { return _MouseOnColor; }
    set
    {
    _MouseOnColor = value;
    }
    }
    protected override void OnPaint(PaintEventArgs pe)
    {
    base.OnPaint(pe);
    g = this.CreateGraphics();
    if (isMouseOn)
    {
    g.DrawString(_NewsTitle, this.Font, new SolidBrush(this._MouseOnColor), new PointF(0, 0));
    }
    else
    {
    g.DrawString(_NewsTitle, this.Font, new SolidBrush(this.ForeColor), new PointF(0, 0));
    }
    }
    protected void Dispose()
    {
    g.Dispose();
    }
    }
    public partial class NewsEventArg : EventArgs
    {
    public int NewsID = 0;
    public string NewsTitle = "";
    public NewsEventArg(int newsID,string newsTitle){
    NewsID = newsID;
    NewsTitle = newsTitle;
    }
    }
    }
随便看

 

在线学习网考试资料包含高考、自考、专升本考试、人事考试、公务员考试、大学生村官考试、特岗教师招聘考试、事业单位招聘考试、企业人才招聘、银行招聘、教师招聘、农村信用社招聘、各类资格证书考试等各类考试资料。

 

Copyright © 2002-2024 cuapp.net All Rights Reserved
更新时间:2025/5/19 20:43:35