C # Processing events in custom controls

Source: Internet
Author: User

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. define the delegate public delegate void NewsClickEventHandle (object sender, NewsEventArg args); public partial class NewsStage: Control {// 2. define the event public event NewsClickEventHandle NewsClicked; private Graphics g; private bool isMouse On = false; public NewsStage () {InitializeComponent (); // 3. event trigger. If event registration is missing, you only need to register the event and edit the event handler when referencing the control in other forms. You can compare this with the previous blog. 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_M OuseMove (object sender, MouseEventArgs e) {isMouseOn = true; this. invalidate () ;}// Method for triggering a news Click Event 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 ("News ID"), Category ("Appearance")] public int NewsID {get {return _ NewsID;} set {_ NewsID = value; this. invalidate () ;}/// <summary> // news title /// </summary> private string _ NewsTitle = ""; [Description ("news title"), Category ("Appearance")] public string NewsTitle {get {return _ NewsTitle;} set {_ NewsTitle = value; this. invalidate () ;}} private Color _ MouseOnColor = new Color (); [Description ("Mouse Color"), 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 ;}}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.