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

推荐订阅源

Google Online Security Blog
Google Online Security Blog
G
Google Developers Blog
云风的 BLOG
云风的 BLOG
阮一峰的网络日志
阮一峰的网络日志
L
Lohrmann on Cybersecurity
Security Latest
Security Latest
博客园 - 【当耐特】
Microsoft Security Blog
Microsoft Security Blog
N
Netflix TechBlog - Medium
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The GitHub Blog
The GitHub Blog
Vercel News
Vercel News
The Register - Security
The Register - Security
T
Tenable Blog
D
Docker
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
有赞技术团队
有赞技术团队
C
CXSECURITY Database RSS Feed - CXSecurity.com
Simon Willison's Weblog
Simon Willison's Weblog
S
Schneier on Security
Scott Helme
Scott Helme
C
Cyber Attacks, Cyber Crime and Cyber Security
C
CERT Recently Published Vulnerability Notes
罗磊的独立博客
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
H
Hackread – Cybersecurity News, Data Breaches, AI and More
K
Kaspersky official blog
Know Your Adversary
Know Your Adversary
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
The Blog of Author Tim Ferriss
aimingoo的专栏
aimingoo的专栏
Spread Privacy
Spread Privacy
P
Proofpoint News Feed
博客园 - 聂微东
NISL@THU
NISL@THU
T
Threat Research - Cisco Blogs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
A
Arctic Wolf
V2EX - 技术
V2EX - 技术
H
Heimdal Security Blog
C
Check Point Blog
D
DataBreaches.Net
H
Help Net Security
www.infosecurity-magazine.com
www.infosecurity-magazine.com
L
LangChain Blog
宝玉的分享
宝玉的分享
V
Vulnerabilities – Threatpost
PCI Perspectives
PCI Perspectives
Recent Announcements
Recent Announcements
Martin Fowler
Martin Fowler

博客园 - 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