none
使用FlowLayoutPanel.Controls.Clear 清除了FlowLayoutPanel上的所有控件。但是上面的控件使用AddHandler绑定了自定义事件。我该在执行Clear之前。如何先RemoveHandler上面的自定义事件,以防止内存泄露。 RRS feed

  • 问题

  • 使用FlowLayoutPanel.Controls.Clear  清除了FlowLayoutPanel上的所有控件。但是上面的控件使用AddHandler绑定了自定义事件。我该在执行Clear之前。如何先RemoveHandler上面的自定义事件,以防止内存泄露。Thanks!
    2012年2月21日 2:57

答案

  • Hi , 你可以用反射获取其handler

        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

    希望有用. 

    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • 已标记为答案 可推 2012年2月23日 9:25
    2012年2月22日 10:24
    版主

全部回复

  • 您好

    使用 FlowLayoutPanel.Controls.Clear 後,假如有將控件進行釋放,則不用特別去處理绑定了自定义事件


    歡迎參觀我的Blog.NET菜鳥自救會

    • 已建议为答案 HUA11111335 2014年4月29日 21:01
    2012年2月21日 3:09
  • 这是真的吗?资料显示AddHandler和RemoveHandler需要成对使用。否则可能会造成内存泄露。

    • 已建议为答案 HUA11111335 2014年4月29日 21:01
    2012年2月21日 3:44
  • Clear 方法不会从内存中移除控件句柄。 Dispose 方法才能避免内存泄漏。

    2012年2月21日 4:02
  • Hi , 你可以用反射获取其handler

        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

    希望有用. 

    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • 已标记为答案 可推 2012年2月23日 9:25
    2012年2月22日 10:24
    版主
  • Hi , 你可以用反射获取其handler

        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

    希望有用. 

    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    很好的方法。谢谢。我现在的做法:定义一个List字段保存了每个变量。然后枚举list字段变量清除绑定。
    2012年2月23日 9:24
  • 使用FlowLayoutPanel.Controls.Clear  清除了FlowLayoutPanel上的所有控件。但是上面的控件使用AddHandler绑定了自定义事件。我该在执行Clear之前。如何先RemoveHandler上面的自定义事件,以防止内存泄露。Thanks!

    2014年4月29日 20:58
  • Hi , 你可以用反射获取其handler

        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

    希望有用. 

    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    很好的方法。谢谢。我现在的做法:定义一个List字段保存了每个变量。然后枚举list字段变量清除绑定。

    2014年4月29日 20:59
  • Hi , 你可以用反射获取其handler

        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

    希望有用. 

    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    很好的方法。谢谢。我现在的做法:定义一个List字段保存了每个变量。然后枚举list字段变量清除绑定。



    2014年4月29日 21:00