Reputation: 20474
( First of all sorry because I don't know how to properly name this question. )
I need to perform some kind of operations when I call this from a Button in the GUI:
Listview1.Items.Add(LVItem)
The thing is that I need to do it from other Class, I mean not inheriting the ListView to raise an own ItemIsAdded
Event etc...
Then is this possibly to do?
I will show an example to understande me better:
I have the Form1
class:
Public Class Form1
Public WithEvents _undoManager As UndoManager
Private Sub Form1_Load(sender As Object, e As EventArgs) _
Handles MyBase.Load
_undoManager = New UndoManager(Listview1)
End Sub
Private Sub Button_AddItem_Click(sender As Object, e As EventArgs) _
Handles Button_AddItem.Click
Listview1.Items.Add(LVItem)
End Sub
End Class
And the UndoManager
Class:
Public Class UndoManager
Delegate Sub AddDelegate(item As ListViewItem)
Private Shared LV As ListView
Private Shared ListView_Add_Method = New AddDelegate(AddressOf LV.Items.Add)
Public Sub New(ByVal ListView As ListView)
LV = ListView
End Sub
' Here code to capture when the delegate method is called...
private sub blahblahblah() handles ListView_Add_Method.IsCalled
' blah blah blah
end sub
End Class
Then the UndoManager
Class need to capture when I call Listview1.Items.Add(LVItem)
in the Form1
Class to perform some kind of operations and then add the item (really no mather if the operation runs before or runs after the Items.Add()
method is called).
Upvotes: 0
Views: 73
Reputation: 38895
you already have an ItemAdded
event - what you are missing is a way for Undo Manager to watch for those events. One way is to just add a hander to a class which will watch for that event. For instance, for text watcher:
Friend Overrides Function Watcher(ByVal ctl As Control) As Boolean
' can also be ComboBox, DateTimePicker etc
If TypeOf Ctl is TextBox Then
AddHandler ctl.Enter, AddressOf _Enter
AddHandler ctl.Leave, AddressOf _Leave
Return True
Case Else
Return False
End Select
End Function
_enter gets the BeforeText _leave compares it to the text now. if there is a change, then begin to make an undoaction. In your case, you only need to monitor ctl.ItemAdded and ctl.ItemRemoved to make an event (no need to compare). Being only a little clever you should be able to easily create Item, Label and Check Undo actions based on the event being watched.
The watchers (that other UndoManager called them Monitors
) is the one that creates undo actions:
Private Sub _Leave(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim NewText As String = CType(sender, Control).Text
If _Text <> NewText Then
MyBase.OnNewAction(sender, _
New AddActionArgs(UndoManager.UnDoReDo.UnDo, _
New TextUndo(sender, _Text)))
End If
End Sub
Upvotes: 1