程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> 關於ASP.NET >> c#自定義控件中事件的處理

c#自定義控件中事件的處理

編輯:關於ASP.NET

     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;  
            }  
        }  
    } 
    1. 上一頁:
    2. 下一頁:
    Copyright © 程式師世界 All Rights Reserved