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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - aroundall

挪窝了 Upgrade Zoomr to Pro 最近越来越喜欢玩游戏了 正在看《经济学原理》和《代码大全》 老了 哎,人都怎么了 还有一个月就要回去了 下午就要走了,出差,发一首齐豫的《菊叹》 纽曼的东西和服务都太次了,可以说是垃圾! Ms-Dos7.10安装盘 星期四就要走了 [转]IT开发工程师的悲哀 学点什么好呢? - aroundall 移动硬盘坏了 - aroundall 今天去办护照了 我很无奈 要学习COBOL了 Where are we go? - aroundall What is RAM Disk?
关于Form KeyDown事件的处理代码执行两次的问题
aroundall · 2006-01-12 · via 博客园 - aroundall

一、背景
        同事叫帮忙看看程序,程序是vb.net程序,描述:一个Form(有KeyDown事件处理代码Form1_KeyDown())上面有一些按钮,这些按钮都注册了KeyDown事件。按理说程序启动起来,界面出来后,即一特定的键只有按钮的KeyDown事件发生。不幸的是,击键一次会执行两次事件处理代码。主要代码如下:

 1
 2Public Class Form1
 3    Inherits System.Windows.Forms.Form
 4
 5    
 6
 7    Public Sub RegistEvent(ByVal ctrTarget As Control)
 8        AddHandler ctrTarget.KeyDown, AddressOf Me.Form1_KeyDown
 9
10        Me.RegistEvent(ctrTarget.Controls)
11
12    End Sub

13
14    Public Sub UnRegistEvent(ByVal ctrTargets As Control.ControlCollection)
15        Dim ctrTarget As Control
16
17        If (ctrTargets Is NothingThen
18            Return
19        End If
20
21        For Each ctrTarget In ctrTargets
22            Me.UnRegistEvent(ctrTarget)
23        Next
24    End Sub

25
26    Private Sub Form1_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles MyBase.Load
27
28        
29
30        If (Me._autoRegistEvent) Then
31            Me.RegistEvent(Me.Controls)
32        End If
33
34        
35
36    End Sub

37
38    Private Sub Form1_KeyDown(ByVal sender As ObjectByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
39
40        
41
42    End Sub

43
44    
45
46End Class

47


二、徒劳的探索
        由于是初接触.NET我也不知道该怎么办,只好step-by-step呗。昨天一个下午都没有跟踪出来,发现一切好像都很合适。这中间还搞了一个闹剧,说是把“Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown”中的“Handles MyBase.KeyDown”给去掉,呵呵。但是从这里我已经看出来了:该子程序仍然在处理Form1的KeyDown事件,这是关键中的关键,结果还是没有找出来为什么。
三、夜间的思考
        思考了好久,仍然想不出来新的见解……
四、问题的解决
        今天上班以来就把问题锁定在Form1上,不知道哪里出错了,估计是属性设置上有点问题。突然间发现
       
        将该项设为false时,发现一切都好了……哭笑不得
注:Form.KeyPreview 属性——获取或设置一个值,该值指示在将键事件传递到具有焦点的控件前,窗体是否将接收此键事件。