我希望能够退出我创建的新表单,但是由于我手动创建表单,因此在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 结束 Class
AFTER:
公开 类 Form1 私人 Sub Form1_KeyDown(sender As Object ,e As KeyEventArgs)句柄 MyBase .KeyDown NewMethod() 结束 Sub Sub NewMethod() ' 代码在这里做了一些事情! 结束 Sub Sub OtherMethod() NewMethod() 结束 Sub 结束 类更新了新要求:对于新要求,您需要使用 WithEvents 来捕获KeyDown事件。这是一个反映这一点的新示例。 公共 类 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 IfWhat 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 ClassAFTER:
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 ClassUpdated with new requirement: For the new requirement, you need to use WithEvents 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 ClassSo 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 SubJust figured out the rest, thanks for the help. now I know how to call forth an withevent handler for future reference.
更多推荐
如何在另一个事件中创建一个keydown事件?
发布评论