如何在另一个事件中创建一个keydown事件? [英] How do I make a keydown event inside another event?
问题描述
我希望能够退出我创建的新表单,但是由于我手动创建表单,因此在withevents中没有处理程序。所以,既然我无法弄清楚我的生活是如何接近我的形式,我提出了另一种可能的解决方案。
我认为我可以在填充新表单的事件中创建一个keydown事件。现在它不起作用,但我希望问一个人我是否在正确的轨道上,可能他们知道我需要改变什么?
这是我的代码:
Dim dk As System.Windows.Forms.KeyEventArgs
dk = sender
if dk.KeyCode = Keys.Escape然后
picform.Close()
结束如果
我尝试了什么:
我尝试将dk值更改为整数并尝试将其更改为键值,没有人帮助过。
将代码重构(提取)为新方法(Sub / Function)。然后,您可以从代码中的多个位置调用它。例如:
之前:
公开 类 Form1
私有 Sub Form1_KeyDown(发件人作为 对象,e As KeyEventArgs)句柄 MyBase .KeyDown
' 代码在这里做了一些事情!
结束 Sub
结束 < span class =code-keyword> Class
AFTER:
公开 类 Form1
私人Sub Form1_KeyDown(sender As Object ,e < span class =code-keyword> As KeyEventArgs)句柄 MyBase .KeyDown
NewMethod()
结束 Sub
Sub NewMethod()
' 代码在这里做了一些事情!
结束 Sub
Sub OtherMethod()
NewMethod()
结束 Sub
结束 类
更新了新要求:对于新要求,您需要使用WithEvents
来捕获KeyDown事件。这是一个反映这一点的新示例。
< pre lang =vb> 公共 类 Form1
私有 Sub Button1_Click(发件人 As 对象,e As EventArgs)句柄 Button1.Click
OpenForm()
结束 Sub
私人 WithEvents picform 作为 新表格
Sub OpenForm()
picform.Height = 800
p icform.Width = 600
' picform。 FormBorderStyle = FormBorderStyle.None
picform.Show()
结束 Sub
私有 Sub picform_KeyDown(sender 作为 对象,e As KeyEventArgs)句柄 picform.KeyDown
MsgBox( Picformn以父表单,MsgBoxStyle.Exclamation获取的keydown事件
结束 Sub
结束 类
非常感谢到Graeme_Grants解决方案我能够弄清楚其余部分,我只需要一种方法来获得这个处理器。
Private WithEvents picform As New表格
Dim picform As New Form
picform.Controls.Add(expandpic)
picform.Height = 800
picform.Width = 600
'picform .FormBorderStyle = FormBorderStyle.None
picform.Show()
AddHandler picform.KeyDown,AddressOf picform_convert
End Sub
Private Sub picform_convert(ByVal sender As System.Object,ByVal e As KeyEventArgs)
picform = CType(sender,Form)
如果e.KeyCode = Keys.Escape那么
picform.Close()
picform.Dispose()
End if
End Sub
只是想出了其余的,谢谢你的帮助。现在我知道如何调用一个withevent处理程序以供将来参考。
I want to be able to exit a new form I created, but since I manually made the form its does not have a handler in the withevents. So since I can not figure out for the life of me how to approach getting my form into the withevents, I came up with another possible solution.
I figured that I could make a keydown event inside the event that populates the new form. Right now it's not working, but I was hoping to ask someone if I am on the right track, and possibly if they know what I need to change?
Here is my code:
Dim dk As System.Windows.Forms.KeyEventArgs dk = sender If dk.KeyCode = Keys.Escape Then picform.Close() End If
What I have tried:
I have tried changing the dk value into an integer and also tried changing it to a key value, none have helped.
Refactor (extract) the code into a new method (Sub/Function). Then you can call it from multiple places in your code. Example:
BEFORE:
Public Class Form1 Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown ' code does something here! End Sub End Class
AFTER:
Public Class Form1 Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown NewMethod() End Sub Sub NewMethod() ' code does something here! End Sub Sub OtherMethod() NewMethod() End Sub End Class
Updated with new requirement: For the new requirement, you need to useWithEvents
to capture the KeyDown event.Here is a new example to reflect this.
Public Class Form1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click OpenForm() End Sub Private WithEvents picform As New Form Sub OpenForm() picform.Height = 800 picform.Width = 600 ' picform.FormBorderStyle = FormBorderStyle.None picform.Show() End Sub Private Sub picform_KeyDown(sender As Object, e As KeyEventArgs) Handles picform.KeyDown MsgBox("Picformn keydown event caputred in parent form", MsgBoxStyle.Exclamation) End Sub End Class
So thanks to Graeme_Grants solution I was able to figure out the rest, I just needed a way to get the withevent handler.
Private WithEvents picform As New Form Dim picform As New Form picform.Controls.Add(expandpic) picform.Height = 800 picform.Width = 600 ' picform.FormBorderStyle = FormBorderStyle.None picform.Show() AddHandler picform.KeyDown, AddressOf picform_convert End Sub Private Sub picform_convert(ByVal sender As System.Object, ByVal e As KeyEventArgs) picform = CType(sender, Form) If e.KeyCode = Keys.Escape Then picform.Close() picform.Dispose() End If End Sub
Just figured out the rest, thanks for the help. now I know how to call forth an withevent handler for future reference.
这篇关于如何在另一个事件中创建一个keydown事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!