Visual Studio 2015 - First Event Handler Stops Event? - vb.net

I'm working on a project which has currently migrated to Visual Studio 2015. I've just encountered a problem I so far have no idea what to do with. In a chain of inherited classes, looking like this:
Public Class ControlA
Public Event CustomEvent(sender As Object, e As CustomEventArgs)
End Class
Public Class BaseForm
Public WithEvents C As ControlA
End Class
Public Class MiddleForm
Inherits BaseForm
Private Sub C_CustomEvent(sender As Object, e As CustomEventArgs) Handles C.CustomEvent
'do something
End Sub
End Class
Public Class FinalForm
Inherits MiddleForm
Private Sub C_CustomEvent(sender As Object, e As CustomEventArgs) Handles C.CustomEvent
'do something elese
End Sub
End Class
the event handler in the most "outer" class (in this case - a form that is shown to a user) do what it's supposed to do and stops the event. Handlers in the base class are not being called.
Everything worked fine in VS2010.
Anyone had similar problem and could give a hint?
EDIT
A little update: Everything works fine when I use AddHandler in both classes. If subs are defined with Handles - it breaks as described above.

Related

Listbox breaks when setting it to a virtual instance from a class

I have a weird problem that I can't wrap my head around.
I have the following code:
Public Class Form1
Public WithEvents MyClass1 As New MyClass
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Private Sub MyClass_UpdateListbox() Handles MyClass1.UpdateListbox
For Each sItem as String In MyClass1.Listbox
MsgBox(sItem) 'an MsgBox shows correct items each time.
Next sItem
Me.Listbox = Me.MyClass1.Listbox 'doesn't work and breaks listbox.
Me.Listbox.Items.Clear() 'listbox is empty anyway, but has no effect.
Me.Listbox.Items.Add("event triggered") 'does nothing.
End Sub
End Class
Public Class MyClass
Public Listbox as new Listbox
Public Event UpdateListbox()
Public Sub New()
'Constructor. sub.
Me.AddItem("Populating listbox")
End Sub
Public Sub AddItem(sItem as String)
Me.Listbox.Items.Add(sItem)
RaiseEvent UpdateListbox()
End Sub
End Class
If I comment the following lines in above code, the listbox keeps adding event triggered, as expected. Of course, I don't have to remove the clear one. It will work, but then it just adds the same item. If I use a command button and call MyClass.AddItem("Something") that is correctly added too as long as the below is commented out. But if not, then once the listbox is in broken state, nothing can be added anymore.
Me.Listbox = Me.MyClass1.Listbox 'doesn't work and breaks listbox.
Me.Listbox.Items.Clear() 'listbox is empty anyway, but has no effect.
How can I use a virtual listbox and assign it to my real listbox?
Also, instead of assigning one listbox to the other, I can of course use that for each loop and add each item one by one which works, but that for each look was for debugging purpose in the first place.
EDIT:
My goal with this application is to build a Todo list with features that are not in a todolist. This is a project I build for work because there I need a tool like this. I already have a todolist that I use but I built it wrong in the past. Everything was condensed in form1, no modules no extra classes. As a result I got weird bugs that I patched with workarounds. I am now rebuilding the application from the ground up, separating tasks in its own classes so I can apply business logic and have a true OOP application. The todo list will become its own class, and managing the list etc will be handeled by this class. It interacts with controls on the form, such as buttons and listboxes. If I just use form1.listbox from the class, things break at program start. I started another question and the below code was a now deleted answer. At first I did not get it working because I did not realize the listbox crashes if I assign it the virtual instance.
So my goal is to have the todolist be handled entirely by the todolist class. It does need a way to interact with controls on form1, and that is the puzzle I'm currently trying to solve.
In the original code, the main problem is that the Field that hold the instance of a Control shown if a Form is reassigned to the instance of another ListBox Control defined in a custom class:
Me.Listbox = Me.MyClass1.Listbox
From now on, Me.Listbox points another ListBox that is not show on screen, so any attempt to update the Form's child ListBox fails, except when Me.Listbox.Items.Clear() is called - in the same procedure - after it's being reassigned, because the handle of the Owner of the ObjectCollection (the object that holds the Items shown in the ListBox) has not been updated yet. It's going to fail after the current method exits nonetheless.
As noted in comments, this is a simplified method to handle a Form and its child Controls using a handler class. The contract between the class handler and a Form is sealed by an Interface (named IFormHandler here).
A Form that implements this Interface exposes the methods defined by the Interface that allow to trigger Actions and specific behaviors, depending on the Type of Control and the implementation.
I suggest to take a look at the MVP or ReactiveUI (MVVM-derived) for WinForms Patterns.
How too proceed:
Open up the ApplicationEvents class object.
If you don't have it already, select Project -> Properties -> Application and click the View Application Events button. It will generate ApplicationEvents.vb. Find it in Solution Explorer and open it up.
It should look like this (plus a bunch of comments that explain what it's for):
Imports Microsoft.VisualBasic.ApplicationServices
Namespace My
Partial Friend Class MyApplication
End Class
End Namespace
Paste into MyApplication these lines of code:
Imports Microsoft.VisualBasic.ApplicationServices
Namespace My
Partial Friend Class MyApplication
Public SomeFormHandler As MyFormHandler(Of SomeForm)
Protected Overrides Function OnStartup(e As StartupEventArgs) As Boolean
SomeFormHandler = New MyFormHandler(Of SomeForm)
Return MyBase.OnStartup(e)
End Function
End Class
End Namespace
Add an Interface that defines the Actions (or Behaviors) that a Form must implement.
Here, the GetUsersList() method specifies that a Form that implements this Interface must return the instance of a child ListBox Control.
(To add an Interface, select Project -> Add -> New Item... and select the Interface template. Name the file IFormHandler)
Extend this Interface as needed, to add more Methods or Properties that define actions and behaviors.
Public Interface IFormHandler
Function GetUsersList() As ListBox
End Interface
A Form that implements the IFormHandler Interface implements and exposes the GetUsersList() method, which returns the instance of a ListBox Control (named usersList here)
There's nothing else to do with this Form, the control is handed over to the MyFormHandler object that is initialized with this Type.
Public Class SomeForm
Implements IFormHandler
Public Sub New()
InitializeComponent()
End Sub
Public Function GetUsersList() As ListBox Implements IFormHandler.GetUsersList
Return Me.usersList
End Function
End Class
Now, to show SomeForm, you can use the MyFormHandler class object show below.
' Set the Owner if called from another Form
My.Application.SomeFormHandler.Show(Me)
' Or without an Owner
My.Application.SomeFormHandler.Show()
To close SomeForm, you can either use its handler:
My.Application.SomeFormHandler.Close()
or close it as usual:
[SomeForm Instance].Close()
If MyFormHandler determines that the instance of SomeForm has been disposed, it creates a new one when you call its Show() method again later.
To update the ListBox Control of SomeForm, use the public methods exposed by the MyFormHandler class:
' Add a new element
My.Application.SomeFormHandler.UpdateUsersList(UpdateType.AddElement, "Some Item")
' Remove an element
My.Application.SomeFormHandler.UpdateUsersList(UpdateType.RemoveElement, "Some Item")
' Replace an element
My.Application.SomeFormHandler.UpdateUsersList(UpdateType.ReplaceElement, "New Item", "Some Item")
' Clears the ListBox
My.Application.SomeFormHandler.ClearUsersList()
All these actions generate an event that you can subscribe to when needed.
See also the example that shows how to raise a custom event when the ListBox raises one of its stardard events; SelectedIndexChanged is handled here.
See the implementation of MyFormHandler.
Generic Form handler:
A Form needs to implement the IFormHandler Interface for the MyFormHandler class to accept it as valid.
You can of course extend the Interface, to add more Actions, or build a MyFormHandler class object that uses a different Interface, or more than one.
Public Class MyFormHandler(Of TForm As {Form, IFormHandler, New})
Implements IDisposable
Private formObject As TForm
Private IsInstanceSelfClosing As Boolean = False
Public Event UsersListUpdate(item As Object, changeType As UpdateType)
Public Event UsersListIndexChanged(index As Integer)
Public Sub New()
InitializeInstance()
Dim lstBox = formObject.GetUsersList()
AddHandler lstBox.SelectedIndexChanged, AddressOf OnUsersListIndexChanged
End Sub
Private Sub InitializeInstance()
formObject = New TForm()
AddHandler formObject.FormClosing, AddressOf OnFormClosing
End Sub
Private Sub OnFormClosing(sender As Object, e As FormClosingEventArgs)
IsInstanceSelfClosing = True
Dispose()
End Sub
Public Sub UpdateUsersList(updateMode As UpdateType, newItem As Object, Optional oldItem As Object = Nothing)
If newItem Is Nothing Then Throw New ArgumentException("New Item is null")
Dim lstBox = formObject.GetUsersList()
Select Case updateMode
Case UpdateType.AddElement
lstBox.Items.Add(newItem)
Case UpdateType.RemoveElement
lstBox.Items.Remove(newItem)
Case UpdateType.ReplaceElement
If oldItem Is Nothing Then Throw New ArgumentException("Replacement Item is null")
Dim index = lstBox.Items.IndexOf(oldItem)
lstBox.Items.Remove(oldItem)
lstBox.Items.Insert(index, newItem)
Case Else : Return
End Select
RaiseEvent UsersListUpdate(newItem, updateMode)
End Sub
Public Sub ClearUsersList()
formObject.GetUsersList().Items.Clear()
End Sub
Private Sub OnUsersListIndexChanged(sender As Object, e As EventArgs)
RaiseEvent UsersListIndexChanged(DirectCast(sender, ListBox).SelectedIndex)
End Sub
Public Sub Show(Optional owner As IWin32Window = Nothing)
If formObject Is Nothing OrElse formObject.IsDisposed Then InitializeInstance()
If formObject.Visible Then
formObject.WindowState = FormWindowState.Normal
formObject.BringToFront()
Else
formObject.Show(owner)
End If
End Sub
Public Sub Close()
If formObject IsNot Nothing AndAlso (Not formObject.IsDisposed) Then
RemoveHandler formObject.FormClosing, AddressOf OnFormClosing
IsInstanceSelfClosing = False
Dispose()
End If
End Sub
Public Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
Protected Overridable Sub Dispose(disposing As Boolean)
If disposing Then
If formObject Is Nothing OrElse formObject.IsDisposed Then Return
Dim lstBox = formObject.GetUsersList()
RemoveHandler lstBox.SelectedIndexChanged, AddressOf OnUsersListIndexChanged
RemoveHandler formObject.FormClosing, AddressOf OnFormClosing
If Not IsInstanceSelfClosing Then formObject.Close()
IsInstanceSelfClosing = False
End If
End Sub
End Class
Enumerator used in MyFormHandler:
Public Enum UpdateType
AddElement
RemoveElement
ReplaceElement
End Enum

Run a function or create a class application at startup

I have a few modules and classes that I need to init themselves once added to the project.
I'm looking for a method mechanism to call a routine(or something) during startup, without explicitly calling it.
I tried the following, with the hope that being MyInit public, will be initialized upon start.
Optimization is preventing creation of MyInit until the first reference.
Partial Public Module InitModule
public MyInit As New MyStart()
Public Class MyStart
Public Sub New()
Debug.Writeline("Yes, init is executed")
End Sub
End Class
End Module
I have a few modules that can be added/linked to multiple projects; when included they provide trace, code verification, version control etc. The idea is that to add or remove, the main code need no changes.
It doesn't matter is if it's a class, sub or function: I want this to run without explicit call.
Thanks #Jimi for his hint
On each module I can add this partial snippet, extending MyApplication class and using the Startup event to do the initialization.
This can be repeated multiple times and me.Startup will be cascaded as needed.
The drawback: MyApplication is available on windows forms only.
#if TARGET="winexe" then
Namespace My
Partial Friend Class MyApplication
Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As ApplicationServices.StartupEventArgs) Handles Me.Startup
My.Application.Log.WriteEntry($"Application start {My.Computer.Clock.ToString}")
' init module stuff
'...
End Sub
End Class
End Namespace
#end if
Update
I found a very easy and straightforward code that works with all targets and not just winexe (forms)
The idea is to force the creation of the class at initialization:
friend _startup_init as new startup_init with {.name="test"}
friend class startup_init
public name as string
public sub new()
'... do here your init,
' in my case a few addhandler to hook the master process
end sub
end class

Inherited class event is not fired

First of all, this is my very first question in this community. Please give me some advice if I did it in the wrong way.
I need a little bit help. I am actually working on a BMEcat class library, BMEcat is a data exchange format for electronic catalogs. Anything works fine, but I realized that there is a memory problem while processing very large files. Because of this, I want to send an event for any processed article/product instead of creating a huge structure in memory.
This is the point where my problem begins.
I have a class CTRANSACTION, from which the classes CT_NEW_CATALOG, CT_UPDATE_PRODUCTS and CT_UPDATE_PRICES are derived.
In the base class CTRANSACTION there is an event defined:
Public Event Transaction_OnNewArticle(ByVal sender As Object, ByVal e As ArticleEventArgs)
The class CBMECAT has the variable
Public WithEvents TRANSACTION As CTRANSACTION
and the event handler
Private Sub TRANSACTION_Transaction_OnNewArticle(sender As Object, e As ArticleEventArgs) Handles TRANSACTION.Transaction_OnNewArticle
'...
End Sub
Because I cannot send the event Transaction_OnNewArticle from the derived CT_NEW_CATALOG class I let it call the TransactionEventOnNewArticle method instead, which is defined in CTRANSACTION. TransactionEventOnNewArticle then calls RaiseEvent Transaction_OnNewArticle.
Everything works wonderful, but the event Transaction_OnNewArticle is not fired. Is there a way to fix it?
Public MustInherit Class CTRANSACTION
Inherits CBMECAT_NODE
Public Event Transaction_OnNewArticle(ByVal sender As Object, ByVal e As ArticleEventArgs)
Public Sub TransactionEventOnNewArticle(ByVal sender As Object, ByVal e As ArticleEventArgs)
RaiseEvent Transaction_OnNewArticle(sender, e)
End Sub
Public Class CT_NEW_CATALOG
Inherits CTRANSACTION
Public Overrides Sub EventOnNewArticle(ByVal sender As Object, ByVal e As ArticleEventArgs)
TransactionEventOnNewArticle(sender, e)
End Sub
Public Class CBMECAT
Inherits CBMECAT_NODE
Public WithEvents TRANSACTION As CTRANSACTION
Private Sub TRANSACTION_Transaction_OnNewArticle(sender As Object, e As ArticleEventArgs) Handles TRANSACTION.Transaction_OnNewArticle
'THIS method is never called - why?
End Sub
End Class
UPDATE
Public Class CBMECAT_ELEMENT
Public Overridable Sub EventOnNewArticle(ByVal sender As Object, ByVal e As ArticleEventArgs)
End Sub
'Please notice that CBMECAT_ELEMENT is the base class of EVERY other class in the library.
'There is a class CBMECAT_NODE, which represents every node of the BMEcat XML structure and is derived from CBMECAT_ELEMENT.
'In CBMECAT_NODE is EventOnNewArticle called whenever an article is processed;
Public Class CBMECAT_NODE
Inherits CBMECAT_ELEMENT
Public Overridable Function CreateChildNode(ByRef Nodename As String, Optional ByRef Parent As CBMECAT_NODE = Nothing) As CBMECAT_ELEMENT
Select Case Nodename
[..]
Case ELEMENT_ARTICLE
CreateChildNode = New CARTICLE(Parent)
Dim e As New ArticleEventArgs With
{
.ARTICLE = CreateChildNode
}
EventOnNewArticle(Me, e)
[..]
UPDATE
Public Class CARTICLE
Inherits CBMECAT_NODE
Public Sub New(ByRef Father As CBMECAT_NODE)
[..]
Public Overrides Sub Read()
[..]
Public Overrides Sub Write()
[..]
Public Overrides Sub Validate()
[..]
UPDATE
Calling sequence:
CBMECAT_NODE.CreateChildNode calls CT_NEW_CATALOG.EventOnNewArticle <- OK
CT_NEW_CATALOG.EventOnNewArticle calls CTRANSACTION.TransactionEventOnNewArticle <- OK
CTRANSACTION.TransactionEventOnNewArticle fires Event Transaction_OnNewArticle
but this event is not received by the event handlier in CBMECAT
If I fire the event manually from a method in CTRANSACTION the event IS received by the event handler.
I also experimentet with AddHandler/RemoveHandler, but this also did not work.
Thank you, Visual Vincent, for helping me to focus the problem and to solve it. In deed it was "a little bit" complicated.
Class CBMECAT had the following read method:
Public Overrides Sub Read()
MyBase.Read()
GetContent(HEADER, ELEMENT_HEADER)
Select Case TransactionType
Case TransactionTypes.T_NEW_CATALOG
GetContent(TRANSACTION, ELEMENT_T_NEW_CATALOG)
Case TransactionTypes.T_UPDATE_PRICES
GetContent(TRANSACTION, ELEMENT_T_UPDATE_PRICES)
Case TransactionTypes.T_UPDATE_PRODUCTS
GetContent(TRANSACTION, ELEMENT_T_UPDATE_PRODUCTS)
Case Else
ReportError(ERROR_BMECAT_UNKNOWN_TRANSACTION_TYPE)
End Select
Validate()
End Sub
MyBase.Read reads the complete XML file and while reading it, the events should be fired. But at this moment the variable TRANSACTION is not assigned by it´s value. This is done by calling GetContent after the reading process has finished.
I have changed to:
TRANSACTION = New CT_NEW_CATALOG
TRANSACTION.Read()
Now all events are fired as expected.
I will remove TransactionEventOnNewArticle() from CBMECAT_ELEMENT. Thanks again, Vincent, for your suggestion. :-)

Calling a procedure within another class

I've created an add-in for outlook 2010. I have a ribbon that has a button on it. When you click that button, I want it to call a procedure in the ThisAddIn.vb.
There are two files: ThisAddin.vb and Ribbon.vb.
I've tried several things to no avail. I've also set all the procedures to public.
Call Testing123()
Call ThisAddIn.Testing123()
Etc
How do I properly call this procedure?
****Ribbon1.vb****
Imports Microsoft.Office.Tools.Ribbon
Public Class MyOutlookTab
Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Button1.Click
Call Testing123()
End Sub
End Class
***ThisAddIn.vb***
Public Class ThisAddIn
Public Sub Testing123()
System.Windows.Forms.MessageBox.Show("It Works!")
End Sub
End Class
The problem is that you are trying to reference class methods without creating a class.
You have three options to make this work:
1) Convert ThisAddIn to a Module. Then there won't be any issues accessing the Testing123 method as you currently have it.
2) Convert ThisAddin.Testing123 to a Shared method, i.e.:
Public Shared Sub Testing123()
Then you would access as follows:
Call ThisAddin.Testing123()
3) Create an instance of the ThisAddIn class prior to using its methods:
Dim oAddIn As New ThisAddIn
Call oAddIn.Testing123()
Update
It appears that addins are treated differently that standard classes.
This MSDN article contains specific implementation guidance for accessing AddIn functionality from other types of solutions.
Based on this article, you need to take a couple of additional steps:
1) Create an interface to expose the functionality from your AddIn:
<ComVisible(True)> _
Public Interface IAddInUtilities
Sub Testing123()
End Interface
2) Add a utilities class to your addin project:
<ComVisible(True)> _
<ClassInterface(ClassInterfaceType.None)> _
Public Class AddInUtilities
Implements IAddInUtilities
Public Sub Testing123() Implements IAddInUtilities.Testing123
System.Windows.Forms.MessageBox.Show("It Works!")
End Sub
End Class
3) Add the following to ThisAddIn to expose the utilities to external callers:
Private utilities As AddInUtilities
Protected Overrides Function RequestComAddInAutomationService() As Object
If utilities Is Nothing Then
utilities = New AddInUtilities()
End If
Return utilities
End Function
4) I am a little unclear on the exact syntax needed for the last step since I don't have automation installed in office, but you will need to do something along these lines:
' OutlookTest should be changed to the name of the project ThisAddIn is in
Dim addIn As Office.COMAddIn = Globals.ThisAddIn.Application.COMAddIns.Item("OutlookTest")
Dim utilities As OutlookTest.IAddInUtilities = TryCast( _
addIn.Object, OutlookTest.IAddInUtilities)
utilities.Testing123()
Thanks for everyones comments but I found the solution in an example here: http://msdn.microsoft.com/en-us/library/ee620548.aspx where they talk about adding a ribbon to the meeting request (2/3's of the way down).
It's actually quite simple. You call the procedure using the "Global"
Globals.ThisAddIn.Testing123()
Nothing else is needed.
You have to create a new instance of the class before you can call it in vb.net!
So something like should allow you to call it..
Public Class MyOutlookTab
Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Button1.Click
Dim testing As New ThisAddIn()
Call testing.Testing123()
End Sub
End Class

Shadowing an event handler

I have a two windows forms classes, a base class and a derived class. The base class has an event handler which handles ValueChanged on some component. I have also written a different event handler for the same event on the derived class.
When I create an instance of the derived class and fire the event, I find that both event handlers run (the base class one and then the derived class one). But I want only the handler in the derived class to run.
Is this possible and if so how do I do it?
(This is .NET 3.5)
Thanks!
Edit: Here is what the code looks like (can't post the actual code):
Public Class BaseForm
Inherits System.Windows.Forms.UserControl
(Windows Form Designer Generated Code)
Private WithEvents myControl As New SomeOtherControl
Protected value As String
Private Sub myControl_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles myControl.ValueChanged
value = SomeLogic()
End Sub
End Class
Public Class DerivedForm
Inherits BaseForm
Private WithEvents myControl As New SomeOtherControl
Private Sub myControl_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles myControl.ValueChanged
value = myControl.Value
End Sub
End Class
You can attach multiple handlers to one event so what you are seeing is by design (you have two handlers attached to the same event so they both fire).
That being said there are ways to accomplish what you are trying to do. One way is to move the code out of your base class handler into an overridable method, and then in the derived class override the method. So your base class handler only has 1 line of code,calling the new method you made. If you inherit from the class it will call your overridden method instead.
Public Class BaseForm
Private Sub myControl_ValueChanged() Handles myControl.ValueChanged
DoSomeLogic()
End Sub
Protected Overridable Sub DoSomeLogic()
'original logic here
End Sub
End Class
Public Class ChildForm
Inherits BaseForm
Protected Overrides Sub DoSomeLogic()
'other logic here
End Sub
End Class
I don't believe you can control that.
If it were me, I think I'd intercept the event in the base class, as you are doing, then define ANOTHER event from the base class and raise that event FROM the event handler in the base class.
on return, if the event was handled (presumably by your derived class), then just exit the base event handler, otherwise, proceed with the base event handling.