惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Heimdal Security Blog
C
Check Point Blog
Jina AI
Jina AI
T
Tailwind CSS Blog
IT之家
IT之家
Stack Overflow Blog
Stack Overflow Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
人人都是产品经理
人人都是产品经理
S
SegmentFault 最新的问题
U
Unit 42
Cyberwarzone
Cyberwarzone
T
The Blog of Author Tim Ferriss
I
Intezer
V
Visual Studio Blog
月光博客
月光博客
A
Arctic Wolf
L
LINUX DO - 热门话题
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
Threatpost
F
Fortinet All Blogs
Simon Willison's Weblog
Simon Willison's Weblog
酷 壳 – CoolShell
酷 壳 – CoolShell
K
Kaspersky official blog
L
Lohrmann on Cybersecurity
T
Tenable Blog
爱范儿
爱范儿
大猫的无限游戏
大猫的无限游戏
博客园_首页
Recent Announcements
Recent Announcements
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
C
Cybersecurity and Infrastructure Security Agency CISA
P
Privacy International News Feed
The Register - Security
The Register - Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
J
Java Code Geeks
I
InfoQ
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Webroot Blog
Webroot Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
The Last Watchdog
The Last Watchdog
AWS News Blog
AWS News Blog
Last Week in AI
Last Week in AI
Spread Privacy
Spread Privacy
云风的 BLOG
云风的 BLOG
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Apple Machine Learning Research
Apple Machine Learning Research
F
Full Disclosure
Vercel News
Vercel News
The Hacker News
The Hacker News

博客园 - dn

[2008年]毕业论文格式要求 《面向.NET的XML程序设计》MAC模拟题 《基于C#的Windows应用程序设计》MCP认证考试模拟题 第六章课后习题及答案 第五章课后习题及答案 第四章课后习题及答案 毕业设计论文封面模板 在C#程序设计中使用Win32 API .NET中常用的连接字符串 第三章课后习题和答案 第三章例题:文件许可 第三章例题:为控件添加设计时支持 第三章例题:自定义控件 毕业论文格式 第二章课后习题及答案 第二章例题:验证处理 第二章例题:动态添加控件 [2007年]毕业设计:bluehill人事管理系统 快速导航
第三章例题:重载控件属性
dn · 2007-04-15 · via 博客园 - dn

对教材3.2.2部分进行演示,该例题包括自定义控件事件的代码

 1using System;
 2
 3namespace override_attribute_event
 4{
 5    
 6    public class NumericTextBox : System.Windows.Forms.TextBox
 7    {
 8        public NumericTextBox()
 9        {
10            //
11            // TODO: 在此处添加构造函数逻辑
12            //
13        }

14
15        //重载TextBox的Text属性
16        public override string Text
17        {
18            get
19            {
20                return base.Text;
21            }

22            set
23            {
24                try
25                {
26                    int.Parse(value);
27                    base.Text = value;
28                    return;
29                }

30                catch
31                {
32                    //如果输入文本部为数值,则不作任何操作
33                }

34                //输入文本为空的时候也可行
35                if (value == null)
36                {
37                    base.Text = value;
38                    return;
39                }

40            }

41        }

42
43        //重载了TextBox的OnKeyPress事件,不让用户点击数字键和Backspace键以外的键
44        protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
45        {
46            //判断用户的按键
47            int asciiInteger = Convert.ToInt32(e.KeyChar);
48            if (asciiInteger >= 47 && asciiInteger <= 57)
49            {
50                e.Handled = false;//将TextBox自身的OnKeyPress触发,实现了数字键的文本输入
51                return;
52            }

53            if (asciiInteger == 8)
54            {
55                e.Handled = false;
56                return;
57            }

58            e.Handled = true;
59            //如果用户点击了非数字键,则调用自定义的事件InvalidUserEntry
60            if (InvalidUserEntry != null)
61            {
62                InvalidUserEntry(this,e);
63            }

64        }

65
66        //自定义事件
67        public delegate void MyEvent(object sender,System.Windows.Forms.KeyPressEventArgs e);
68
69        public event MyEvent InvalidUserEntry;
70
71
72        
73    }

74}

75

完整源代码下载:override_attribute_event.rar