Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 为所有相同的控件处理UserControl事件_.net_Vb.net_Winforms_Events_User Controls - Fatal编程技术网

.net 为所有相同的控件处理UserControl事件

.net 为所有相同的控件处理UserControl事件,.net,vb.net,winforms,events,user-controls,.net,Vb.net,Winforms,Events,User Controls,问题是,我有一个具有自己事件和两个堆栈的ListView 需要启用这样的属性来处理这些事件: Public property Enable_Something as boolean 好的,我将我的用户控件添加到UI并启用属性,但是如果我在UI中再添加一次相同的控件,那么两个控件的事件都会得到处理!堆栈由两个控件推动/弹出 因此属性冲突,事件和堆栈也冲突,因为第二个控件将一些内容添加到第一个控件堆栈中 我想为每个控件分离操作/事件/堆栈 这是form1类(阅读注释): 这是用户控件的重要部分:

问题是,我有一个具有自己事件和两个堆栈的ListView

需要启用这样的属性来处理这些事件:

Public property Enable_Something as boolean
好的,我将我的用户控件添加到UI并启用属性,但是如果我在UI中再添加一次相同的控件,那么两个控件的事件都会得到处理!堆栈由两个控件推动/弹出

因此属性冲突,事件和堆栈也冲突,因为第二个控件将一些内容添加到第一个控件堆栈中

我想为每个控件分离操作/事件/堆栈

这是form1类(阅读注释):

这是用户控件的重要部分:

Public Class ListView_Elektro : Inherits ListView

    ''' <summary>
    ''' Enable or disble the Undo/Redo monitoring.
    ''' </summary>
    Public Property Enable_UndoRedo_Manager As Boolean = False

    Public Shared Event ItemAdded As EventHandler(Of ItemAddedEventArgs)
    Public Class ItemAddedEventArgs : Inherits EventArgs
        Public Property Item As ListViewItem
    End Class

   Public Function AddItem(ByVal Item As ListViewItem) As ListViewItem
        MyBase.Items.Add(Item)
        RaiseEvent ItemAdded(Me, New ItemAddedEventArgs With {.Item = Item})
        Return Item
    End Function

    ''' <summary>
    ''' Creates a Undo/Redo Action.
    ''' </summary>
    Class ListView_Action

        ''' <summary>
        ''' Names the Undo/Redo Action.
        ''' </summary>
        Property Name As String

        ''' <summary>
        ''' Points to a method to excecute.
        ''' </summary>
        Property Operation As [Delegate]

        ''' <summary>
        ''' Method of the Undo/Redo operation.
        ''' </summary>
        Property Method As UndoRedoManager.Method

        ''' <summary>
        ''' Data Array for the method to excecute.
        ''' </summary>
        Property Data As ListViewItem

    End Class

  Public Class UndoRedoManager

        Private WithEvents LV As ListView_Elektro

        ' Delegate to Add an Item for Undo/Redo operations.
        Delegate Sub AddDelegate(item As ListViewItem)

        ' Delegate to Remove an Item for Undo/Redo operations.
        Delegate Sub RemoveDelegate(item As ListViewItem)

        ' The Undo/Redo action.
        Private action As ListView_Action = Nothing

        ' The operation.
        Public Enum Operation As Short
            Undo = 0
            Redo = 1
        End Enum

        ' The method for the Undo/Redo operation.
        Public Enum Method As Short
            Add = 0
            Remove = 1
        End Enum

        ''' <summary>
        ''' This event is raised after an Undo/Redo action is performed.
        ''' </summary>
        Event UndoRedo_IsPerformed As EventHandler(Of UndoneRedoneEventArgs)
        Class UndoneRedoneEventArgs : Inherits EventArgs
            Public Property Operation As Operation
            Public Property Method As Method
            Public Property Item As ListViewItem
            Public Property UndoStack As Stack(Of ListView_Action)
            Public Property RedoStack As Stack(Of ListView_Action)
        End Class

        ''' <summary>
        ''' This event is raised when Undo/Redo Stack size changed.
        ''' </summary>
        Event UndoRedo_StackSizeChanged As EventHandler(Of StackSizeChangedEventArgs)
        Class StackSizeChangedEventArgs : Inherits EventArgs
            Public Property UndoStack As Stack(Of ListView_Action)
            Public Property RedoStack As Stack(Of ListView_Action)
            Public Property UndoStackIsEmpty As Boolean
            Public Property RedoStackIsEmpty As Boolean
        End Class

        Public Sub New(ByVal ListView As ListView_Elektro)
            LV = ListView
            ' MsgBox(LV.ToString)
        End Sub

        ''' <summary>
        ''' Undo the last action.
        ''' </summary>
        Sub UndoLastAction()

            If LV.Undostack.Count = 0 Then Exit Sub ' Nothing to Undo.

            LV.IsDoingUndo = True
            action = LV.Undostack.Pop ' Get the Action from the Stack and remove it.
            action.Operation.DynamicInvoke(action.Data) ' Invoke the undo Action.
            LV.IsDoingUndo = False

            Raise_UndoRedo_IsPerformed(Operation.Undo, action.Method, action.Data)

        End Sub

        ''' <summary>
        ''' Redo the last action.
        ''' </summary>
        Sub RedoLastAction()

            If LV.Redostack.Count = 0 Then Exit Sub ' Nothing to Redo.

            LV.IsDoingRedo = True
            action = LV.Redostack.Pop() ' Get the Action from the Stack and remove it.
            action.Operation.DynamicInvoke(action.Data) ' Invoke the redo Action.
            LV.IsDoingRedo = False

            Raise_UndoRedo_IsPerformed(Operation.Redo, action.Method, action.Data)

        End Sub

        ' Raises the "UndoRedo_IsPerformed" Event
        Private Sub Raise_UndoRedo_IsPerformed(ByVal Operation As Operation, _
                                               ByVal Method As Method, _
                                               ByVal Item As ListViewItem)

            RaiseEvent UndoRedo_IsPerformed(Me, New UndoneRedoneEventArgs _
                       With {.Item = Item, _
                             .Method = Method, _
                             .Operation = Operation, _
                             .UndoStack = LV.Undostack, _
                             .RedoStack = LV.Redostack})

            Raise_UndoRedo_StackSizeChanged()

        End Sub

        ' Raises the "UndoRedo_IsPerformed" Event
        Private Sub Raise_UndoRedo_StackSizeChanged()

            RaiseEvent UndoRedo_StackSizeChanged(Me, New StackSizeChangedEventArgs _
                       With {.UndoStack = LV.Undostack, _
                             .RedoStack = LV.Redostack, _
                             .UndoStackIsEmpty = LV.Undostack.Count = 0, _
                             .RedoStackIsEmpty = LV.Redostack.Count = 0})

        End Sub

        ' Reverses an Undo/Redo action
        Private Function GetReverseAction(ByVal e As UndoneRedoneEventArgs) As ListView_Action

            action = New ListView_Action

            action.Name = e.Item.Text
            action.Data = e.Item

            action.Operation = If(e.Method = Method.Add, _
                            New RemoveDelegate(AddressOf LV.RemoveItem), _
                            New AddDelegate(AddressOf LV.AddItem))

            action.Method = If(e.Method = Method.Add, _
                         Method.Remove, _
                         Method.Add)

            Return action

        End Function

        ' This handles when an Undo or Redo operation is performed.
        Private Sub UndoneRedone(ByVal sender As Object, ByVal e As UndoneRedoneEventArgs) _
        Handles Me.UndoRedo_IsPerformed

            Select Case e.Operation

                Case Operation.Undo
                    ' Create a Redo Action for the undone action.
                    LV.Redostack.Push(GetReverseAction(e))

                Case Operation.Redo
                    ' Create a Undo Action for the redone action.
                    LV.Undostack.Push(GetReverseAction(e))

            End Select

        End Sub

        ' Monitors when an Item is added to create an Undo Operation.
        Private Sub OnItemAdded(sender As Object, e As ItemAddedEventArgs) _
        Handles LV.ItemAdded

            If LV.Enable_UndoRedo_Manager _
                AndAlso (Not LV.IsDoingUndo And Not LV.IsDoingRedo) Then

                LV.Redostack.Clear()

                ' // Crate an Undo Action
                action = New ListView_Action
                action.Name = e.Item.Text
                action.Operation = New RemoveDelegate(AddressOf LV.RemoveItem)
                action.Data = e.Item
                action.Method = Method.Remove

                LV.Undostack.Push(action)

                Raise_UndoRedo_StackSizeChanged()

            End If

        End Sub

        ' Monitors when an Item is removed to create an Undo Operation.
        Private Sub OnItemRemoved(sender As Object, e As ItemRemovedEventArgs) _
        Handles LV.ItemRemoved

            If LV.Enable_UndoRedo_Manager _
                AndAlso (Not LV.IsDoingUndo And Not LV.IsDoingRedo) Then

                LV.Redostack.Clear()

                ' // Crate an Undo Action
                action = New ListView_Action
                action.Name = e.Item.Text
                action.Operation = New AddDelegate(AddressOf LV.AddItem)
                action.Data = e.Item
                action.Method = Method.Add

                LV.Undostack.Push(action)

                Raise_UndoRedo_StackSizeChanged()

            End If

        End Sub

    End Class

    End Class
公共类ListView\u Elektro:继承ListView
''' 
''启用或取消撤消/重做监视。
''' 
公共属性Enable\u UndoRedo\u管理器为布尔值=False
作为EventHandler添加的公共共享事件项(属于ItemAddedEventArgs)
公共类ItemAddedEventArgs:继承EventArgs
作为ListViewItem的公共属性项
末级
公共函数附加项(作为ListViewItem的ByVal项)作为ListViewItem
MyBase.Items.Add(项目)
RaiseEvent ItemAdded(Me,带有{.Item=Item}的新ItemAddedEventArgs)
退货项目
端函数
''' 
''创建撤消/重做操作。
''' 
类ListView\u操作
''' 
''命名撤消/重做操作。
''' 
属性名为字符串
''' 
''指向要执行的方法。
''' 
属性操作作为[委托]
''' 
撤消/重做操作的“”方法。
''' 
属性方法作为UndoRedoManager.Method
''' 
要执行的方法的“”数据数组。
''' 
属性数据作为ListViewItem
末级
公共类管理器
Private WithEvents LV作为ListView\u Elektro
'委托添加用于撤消/重做操作的项。
委托子添加委托(项目作为ListViewItem)
'委托以删除用于撤消/重做操作的项。
代理子RemoveDelegate(项目作为ListViewItem)
'撤消/重做操作。
作为ListView的私有操作\u action=Nothing
"手术,。
公共枚举操作为短
撤消=0
重做=1
结束枚举
'撤消/重做操作的方法。
公共枚举方法简称为
相加=0
删除=1
结束枚举
''' 
''执行撤消/重做操作后引发此事件。
''' 
事件UndoRedo_作为(Undoneredoneventargs的)事件处理程序执行
类Undoneredoneventargs:继承EventArgs
公共财产经营作为经营
公共财产法
作为ListViewItem的公共属性项
公共属性作为堆栈撤消堆栈(ListView_操作的)
公共属性RedoStack作为堆栈(ListView_操作的)
末级
''' 
''当撤消/重做堆栈大小更改时引发此事件。
''' 
事件撤消重做\u StackSizeChanged更改为EventHandler(属于StackSizeChangedEventArgs)
类StackSizeChangedEventArgs:继承EventArgs
公共属性作为堆栈撤消堆栈(ListView_操作的)
公共属性RedoStack作为堆栈(ListView_操作的)
作为布尔值的公共属性UndoStackIsEmpty
作为布尔值的公共属性为空
末级
Public Sub New(ByVal ListView作为ListView_Elektro)
LV=列表视图
'MsgBox(LV.ToString)
端接头
''' 
''撤消最后一个操作。
''' 
子操作()
如果LV.Undostack.Count=0,则退出Sub“无需撤消”。
LV.IsDoingUndo=真
action=LV.Undostack.Pop'从堆栈中获取操作并将其删除。
action.Operation.DynamicInvoke(action.Data)'调用撤销操作。
LV.IsDoingUndo=错误
Raise\u UndoRedo\u已执行(Operation.Undo、action.Method、action.Data)
端接头
''' 
''重做最后一个动作。
''' 
子操作()
如果LV.Redostack.Count=0,则退出Sub“无需重做”。
LV.IsDoingRedo=True
action=LV.Redostack.Pop()'从堆栈中获取操作并将其删除。
action.Operation.DynamicInvoke(action.Data)'调用重做操作。
LV.IsDoingRedo=False
Raise\u UndoRedo\u已执行(Operation.Redo、action.Method、action.Data)
端接头
'引发“UndoRedo_IsPerformed”事件
已执行私有子提升(ByVal操作作为操作_
ByVal方法作为方法_
ByVal项作为ListViewItem)
RaiseEvent UndoRedo_已执行(Me、新UNDONEREDONEVENTARG_
使用{.Item=Item_
.方法=方法_
.操作=操作_
.UndoStack=LV.UndoStack_
.RedoStack=LV.RedoStack})
提升\u撤消恢复\u堆栈大小更改()
端接头
'引发“UndoRedo_IsPerformed”事件
私有子集合\u UndoRedo\u StackSizeChanged()
RaiseEvent UndoRedo_StackSizeChanged(Me,新StackSizeChangedEventArgs_
使用{.UndoStack=LV.UndoStack_
.RedoStack=LV.RedoStack_
.UndoStackIsEmpty=LV.Undostack.Count=0_
.redostackismpty=LV.Redostack.Count=0})
端接头
'撤消撤消/重做操作
私有函数GetReverseAction(以值e作为撤消器
Public Class ListView_Elektro : Inherits ListView

    ''' <summary>
    ''' Enable or disble the Undo/Redo monitoring.
    ''' </summary>
    Public Property Enable_UndoRedo_Manager As Boolean = False

    Public Shared Event ItemAdded As EventHandler(Of ItemAddedEventArgs)
    Public Class ItemAddedEventArgs : Inherits EventArgs
        Public Property Item As ListViewItem
    End Class

   Public Function AddItem(ByVal Item As ListViewItem) As ListViewItem
        MyBase.Items.Add(Item)
        RaiseEvent ItemAdded(Me, New ItemAddedEventArgs With {.Item = Item})
        Return Item
    End Function

    ''' <summary>
    ''' Creates a Undo/Redo Action.
    ''' </summary>
    Class ListView_Action

        ''' <summary>
        ''' Names the Undo/Redo Action.
        ''' </summary>
        Property Name As String

        ''' <summary>
        ''' Points to a method to excecute.
        ''' </summary>
        Property Operation As [Delegate]

        ''' <summary>
        ''' Method of the Undo/Redo operation.
        ''' </summary>
        Property Method As UndoRedoManager.Method

        ''' <summary>
        ''' Data Array for the method to excecute.
        ''' </summary>
        Property Data As ListViewItem

    End Class

  Public Class UndoRedoManager

        Private WithEvents LV As ListView_Elektro

        ' Delegate to Add an Item for Undo/Redo operations.
        Delegate Sub AddDelegate(item As ListViewItem)

        ' Delegate to Remove an Item for Undo/Redo operations.
        Delegate Sub RemoveDelegate(item As ListViewItem)

        ' The Undo/Redo action.
        Private action As ListView_Action = Nothing

        ' The operation.
        Public Enum Operation As Short
            Undo = 0
            Redo = 1
        End Enum

        ' The method for the Undo/Redo operation.
        Public Enum Method As Short
            Add = 0
            Remove = 1
        End Enum

        ''' <summary>
        ''' This event is raised after an Undo/Redo action is performed.
        ''' </summary>
        Event UndoRedo_IsPerformed As EventHandler(Of UndoneRedoneEventArgs)
        Class UndoneRedoneEventArgs : Inherits EventArgs
            Public Property Operation As Operation
            Public Property Method As Method
            Public Property Item As ListViewItem
            Public Property UndoStack As Stack(Of ListView_Action)
            Public Property RedoStack As Stack(Of ListView_Action)
        End Class

        ''' <summary>
        ''' This event is raised when Undo/Redo Stack size changed.
        ''' </summary>
        Event UndoRedo_StackSizeChanged As EventHandler(Of StackSizeChangedEventArgs)
        Class StackSizeChangedEventArgs : Inherits EventArgs
            Public Property UndoStack As Stack(Of ListView_Action)
            Public Property RedoStack As Stack(Of ListView_Action)
            Public Property UndoStackIsEmpty As Boolean
            Public Property RedoStackIsEmpty As Boolean
        End Class

        Public Sub New(ByVal ListView As ListView_Elektro)
            LV = ListView
            ' MsgBox(LV.ToString)
        End Sub

        ''' <summary>
        ''' Undo the last action.
        ''' </summary>
        Sub UndoLastAction()

            If LV.Undostack.Count = 0 Then Exit Sub ' Nothing to Undo.

            LV.IsDoingUndo = True
            action = LV.Undostack.Pop ' Get the Action from the Stack and remove it.
            action.Operation.DynamicInvoke(action.Data) ' Invoke the undo Action.
            LV.IsDoingUndo = False

            Raise_UndoRedo_IsPerformed(Operation.Undo, action.Method, action.Data)

        End Sub

        ''' <summary>
        ''' Redo the last action.
        ''' </summary>
        Sub RedoLastAction()

            If LV.Redostack.Count = 0 Then Exit Sub ' Nothing to Redo.

            LV.IsDoingRedo = True
            action = LV.Redostack.Pop() ' Get the Action from the Stack and remove it.
            action.Operation.DynamicInvoke(action.Data) ' Invoke the redo Action.
            LV.IsDoingRedo = False

            Raise_UndoRedo_IsPerformed(Operation.Redo, action.Method, action.Data)

        End Sub

        ' Raises the "UndoRedo_IsPerformed" Event
        Private Sub Raise_UndoRedo_IsPerformed(ByVal Operation As Operation, _
                                               ByVal Method As Method, _
                                               ByVal Item As ListViewItem)

            RaiseEvent UndoRedo_IsPerformed(Me, New UndoneRedoneEventArgs _
                       With {.Item = Item, _
                             .Method = Method, _
                             .Operation = Operation, _
                             .UndoStack = LV.Undostack, _
                             .RedoStack = LV.Redostack})

            Raise_UndoRedo_StackSizeChanged()

        End Sub

        ' Raises the "UndoRedo_IsPerformed" Event
        Private Sub Raise_UndoRedo_StackSizeChanged()

            RaiseEvent UndoRedo_StackSizeChanged(Me, New StackSizeChangedEventArgs _
                       With {.UndoStack = LV.Undostack, _
                             .RedoStack = LV.Redostack, _
                             .UndoStackIsEmpty = LV.Undostack.Count = 0, _
                             .RedoStackIsEmpty = LV.Redostack.Count = 0})

        End Sub

        ' Reverses an Undo/Redo action
        Private Function GetReverseAction(ByVal e As UndoneRedoneEventArgs) As ListView_Action

            action = New ListView_Action

            action.Name = e.Item.Text
            action.Data = e.Item

            action.Operation = If(e.Method = Method.Add, _
                            New RemoveDelegate(AddressOf LV.RemoveItem), _
                            New AddDelegate(AddressOf LV.AddItem))

            action.Method = If(e.Method = Method.Add, _
                         Method.Remove, _
                         Method.Add)

            Return action

        End Function

        ' This handles when an Undo or Redo operation is performed.
        Private Sub UndoneRedone(ByVal sender As Object, ByVal e As UndoneRedoneEventArgs) _
        Handles Me.UndoRedo_IsPerformed

            Select Case e.Operation

                Case Operation.Undo
                    ' Create a Redo Action for the undone action.
                    LV.Redostack.Push(GetReverseAction(e))

                Case Operation.Redo
                    ' Create a Undo Action for the redone action.
                    LV.Undostack.Push(GetReverseAction(e))

            End Select

        End Sub

        ' Monitors when an Item is added to create an Undo Operation.
        Private Sub OnItemAdded(sender As Object, e As ItemAddedEventArgs) _
        Handles LV.ItemAdded

            If LV.Enable_UndoRedo_Manager _
                AndAlso (Not LV.IsDoingUndo And Not LV.IsDoingRedo) Then

                LV.Redostack.Clear()

                ' // Crate an Undo Action
                action = New ListView_Action
                action.Name = e.Item.Text
                action.Operation = New RemoveDelegate(AddressOf LV.RemoveItem)
                action.Data = e.Item
                action.Method = Method.Remove

                LV.Undostack.Push(action)

                Raise_UndoRedo_StackSizeChanged()

            End If

        End Sub

        ' Monitors when an Item is removed to create an Undo Operation.
        Private Sub OnItemRemoved(sender As Object, e As ItemRemovedEventArgs) _
        Handles LV.ItemRemoved

            If LV.Enable_UndoRedo_Manager _
                AndAlso (Not LV.IsDoingUndo And Not LV.IsDoingRedo) Then

                LV.Redostack.Clear()

                ' // Crate an Undo Action
                action = New ListView_Action
                action.Name = e.Item.Text
                action.Operation = New AddDelegate(AddressOf LV.AddItem)
                action.Data = e.Item
                action.Method = Method.Add

                LV.Undostack.Push(action)

                Raise_UndoRedo_StackSizeChanged()

            End If

        End Sub

    End Class

    End Class
Public Property Enable_UndoRedo_Manager As Boolean = False

 ...
Public Function AddItem(ByVal Item As ListViewItem) As BOOLEAN
    MyBase.Items.Add(Item)

    ' TEST to see if this LV instance should signal a change:
    If Enable_UndoRedo_Manager THen
        RaiseEvent ItemAdded(Me, New ItemAddedEventArgs With {.Item = Item})
        Return True
    end if
    Return False
End Function
Private _undoStack As Stack(Of ListViewEUndo)


Public Function AddItem(ByVal Item As ListViewItem) As Boolean
    ' need to not stack Undos
    _IgnoreChange = True
    ' NOT NEEDED for an internal UM
    'RaiseEvent ItemAdded(Me, Item)

    MyBase.Items.Add(Item)
    AddUnDoAction(Item)          ' create an undo action, push it
    _IgnoreChange = False
End Function
MyBase.Items.Add(Item)
myUndoMgr.AddUnDoAction(Item)       ' _undoStack would be inside this class'
                                    ' in this case       
Public Shared Event ItemAdded As EventHandler(Of ItemAddedEventArgs)
' a good UnDo Mgr will need to know WHO changed 
'     and might as well tell WHAT changed to make things easy
Public Event ItemAdded(ByVal sender As Object, ByVal item As ListViewItem)
Public Event ItemRemoved(ByVal sender As Object, ByVal item As ListViewItem)