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

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
小众软件
小众软件
阮一峰的网络日志
阮一峰的网络日志
Recent Announcements
Recent Announcements
雷峰网
雷峰网
Last Week in AI
Last Week in AI
I
InfoQ
Google DeepMind News
Google DeepMind News
GbyAI
GbyAI
The Cloudflare Blog
aimingoo的专栏
aimingoo的专栏
量子位
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Blog — PlanetScale
Blog — PlanetScale
F
Full Disclosure
D
DataBreaches.Net
S
SegmentFault 最新的问题
Hugging Face - Blog
Hugging Face - Blog
MyScale Blog
MyScale Blog
美团技术团队
V
V2EX
Jina AI
Jina AI
T
The Blog of Author Tim Ferriss
T
Tailwind CSS Blog
MongoDB | Blog
MongoDB | Blog
腾讯CDC
Vercel News
Vercel News
A
About on SuperTechFans
J
Java Code Geeks
Martin Fowler
Martin Fowler
V
Visual Studio Blog
博客园_首页
酷 壳 – CoolShell
酷 壳 – CoolShell
Recorded Future
Recorded Future
M
MIT News - Artificial intelligence
WordPress大学
WordPress大学
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
U
Unit 42
Engineering at Meta
Engineering at Meta
F
Fortinet All Blogs
Microsoft Azure Blog
Microsoft Azure Blog
P
Proofpoint News Feed
人人都是产品经理
人人都是产品经理
Y
Y Combinator Blog
云风的 BLOG
云风的 BLOG
博客园 - 三生石上(FineUI控件)
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The GitHub Blog
The GitHub Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

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