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

推荐订阅源

罗磊的独立博客
Cisco Talos Blog
Cisco Talos Blog
C
Check Point Blog
博客园_首页
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Martin Fowler
Martin Fowler
Recorded Future
Recorded Future
S
Security @ Cisco Blogs
L
LINUX DO - 最新话题
博客园 - 司徒正美
P
Privacy International News Feed
G
Google Developers Blog
I
Intezer
Hacker News - Newest:
Hacker News - Newest: "LLM"
博客园 - 聂微东
The GitHub Blog
The GitHub Blog
C
Cybersecurity and Infrastructure Security Agency CISA
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Scott Helme
Scott Helme
K
Kaspersky official blog
I
InfoQ
Y
Y Combinator Blog
T
The Blog of Author Tim Ferriss
Webroot Blog
Webroot Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
大猫的无限游戏
大猫的无限游戏
D
Docker
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
Microsoft Azure Blog
Microsoft Azure Blog
Spread Privacy
Spread Privacy
量子位
H
Hacker News: Front Page
Simon Willison's Weblog
Simon Willison's Weblog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
SecWiki News
SecWiki News
S
Security Affairs
Latest news
Latest news
人人都是产品经理
人人都是产品经理
C
CERT Recently Published Vulnerability Notes
S
Security Archives - TechRepublic
V
Visual Studio Blog
T
Troy Hunt's Blog
S
Secure Thoughts
F
Fortinet All Blogs
V
V2EX
The Register - Security
The Register - Security
J
Java Code Geeks
MongoDB | Blog
MongoDB | Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO

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