如何让事件成为一种形式 [英] how to get events a form

查看:65
本文介绍了如何让事件成为一种形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

i有一个名为Form2的表格



hi everyone
i have a form by name "Form2"

Public Class Form2

    Private Sub Form2_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        MsgBox("Close")
    End Sub

    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        MsgBox("Load")
    End Sub
End Class





现在我想得到委托Form2_FormClosing in form1

i找到此函数:



now i want to get delegate Form2_FormClosing in form1
i found this function :

Private Function GetEventHandler(ByVal ctrl As Control, ByVal eventname As String) As [Delegate]
      Dim propInfo As System.Reflection.PropertyInfo = GetType(System.ComponentModel.Component).GetProperty("Events", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance Or Reflection.BindingFlags.Static)
      Dim handlerList As System.ComponentModel.EventHandlerList = CType(propInfo.GetValue(ctrl, Nothing), System.ComponentModel.EventHandlerList)
      Dim controlEventInfo As System.Reflection.FieldInfo = GetType(Control).GetField(eventname, Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Static)

      If controlEventInfo Is Nothing Then
          Throw New ArgumentException("Specified event does not exist.")
      End If

      Dim eventKey As Object = controlEventInfo.GetValue(ctrl)
      Dim EventHandlerDelegate As [Delegate] = handlerList.Item(eventKey)
      Return EventHandlerDelegate
  End Function





我测试它是这样的:



and i test it like this :

Dim form As Form = New Form2
  GetEventHandler(form, "EVENT_FormClosing")



但它有错误:指定的事件不存在。



我的问题在哪里?

谢谢


but it has error : "Specified event does not exist."

where is my problem?
thank you

推荐答案

没有EVENT_FormClosing这样的东西。事件名称是FormClosing。



然而,整个想法都是错误的。我打赌你永远不需要使用 GetEvenHandler 。为什么找到你已经知道的东西,因为你是谁定义了事件处理程序?此方法用于完全不同的目的,与您的问题无关。相反,您需要编写一些从事件处理程序中调用的方法。这样,您可以从其他地方调用此方法。我相信这就是你最初的目标。







如果看起来像这样:

There is not such thing as "EVENT_FormClosing". The event name is "FormClosing".

However, the whole idea is wrong. I bet you never need to use GetEvenHandler. Why "finding" something which you already know, because this is you who defined the event handler? This method is used for completely different purposes which have nothing to do with your problem. Instead, you need to write some method to be called from your event handler. This way, you can call this method from some other place. I believe this is what was your original goal.



If can look like this:
using System.Windows.Forms;

//...

enum FormClosingDecision { Close, Cancel, Hide, }

public partial class MyForm {
 
   public MyForm() {
      //...
      FormClosing += (sender, eventArgs) => {
          FormClosingDecision decision = CanCloseForm(eventArgs.Reason);
          switch (decision)) {
              case .Cancel:
                   eventArgs.Cancel = true;
                   break;
              case FormClosingDecision.Hide:
                   eventArgs.Cancel = true;
                   this.Hide();
                   break;
          } // switch
      }; // FormClosing handler
   } //MyForm

   bool CanCloseForm(CloseReason reason) {
      return // something depending on reason, user confirmation, whatever...
   } //CanCloseForm

   // you can call CanCloseForm somewhere else, without any concern to its use
   // in implementation of the even FormClosing
   
} //class MyForm





I证明只是因为处理这个事件你关注的是事件。在实践中,对于表单,更容易覆盖虚拟方法 OnFormClosing





I demonstrated handling this event only because you are concerned with events. In practice, for a form, it''s easier to override the virtual method OnFormClosing:

using System.Windows.Forms;

//...

enum FormClosingDecision { Close, Cancel, Hide, }

public partial class MyForm {
 
   // ...

   protected override void OnFormClosing(FormClosingEventArgs eventArgs) {
      FormClosingDecision decision = CanCloseForm(eventArgs.Reason);
      switch (decision) {
          case FormClosingDecision.Cancel:
               eventArgs.Cancel = true;
               break;
          case FormClosingDecision.Hide:
               eventArgs.Cancel = true;
               this.Hide();
               break;
      } // switch
   } //OnFormClosing

   bool CanCloseForm(CloseReason reason) {
      return // something depending on reason, user confirmation, whatever...
   } //CanCloseForm

   // you can call CanCloseForm somewhere else, without any concern to its use
   // in implementation of the even FormClosing
   
} //class MyForm







-SA

这篇关于如何让事件成为一种形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆