03-08-2013 05:53 AM
Adding event handlers in VB is quite different than in C# (Even though I'm a VB guy, I think C# has more flexibility with this). Anyways, what you need to do in VB.NET is actually create a subroutine that would handle the event (just like a button click event, just without the "Handles Button1.Click" at the end). This is a sample of what I've done in a project that I needed to handle the RowChanged event on a datatable that I created.
Private Sub frmMain_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load Try 'Table Setup - Removed for clarity AddHandler dtPartReplace.RowChanged, AddressOf dtPartReplace_RowChanged 'Other Code removed for clarity Catch ex As Exception MessageBox.Show(ex.Message & Environment.NewLine & Environment.NewLine & ex.StackTrace, "Error Loading Data", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub Private Sub dtPartReplace_RowChanged(ByVal sender As Object, e As DataRowChangeEventArgs) ValidateReplacementDataUpdateUI() End Sub
In your case, you'll need to make a subroutine that has the same parameters as the FieldChanged event, then you use the AddHandler line like you did, but using your subroutine name after the "AddressOf" portion. If you need to remove the handler, it's basically the same syntax, but with "RemoveHandler" instead of "AddHandler". I hope this helps.
03-08-2013 06:12 AM
You have lost me knif, I'm a moderate VB programmer but this seems quite complicated for me this is code dump. Could you please tell me how to change code accordingly based on your suggestion.
Friend Sub OnLoad(ByVal App As ActApplication) Implements IPlugin.OnLoad
Me.HostApplication = App
Me.HostFramework = App.ActFramework
' Add the HostApplication Logon and LogOff Events
AddHandler HostApplication.AfterLogon, AddressOf HostApplicationAfterLogon
AddHandler HostApplication.BeforeLogoff, AddressOf HostApplicationBeforeLogoff
End Sub 'OnLoad
Public Sub OnUnLoad() Implements IPlugin.OnUnLoad
' Not used in the examples
End Sub 'OnUnLoad
Private Sub HostApplicationAfterLogon(ByVal Sender As Object, ByVal e As System.EventArgs)
AddHandler HostApplication.ApplicationState.CurrentCompany.Fields.FieldChangedHandler, AddressOf mutentfield(Field_Changed)
Private Sub HostApplicationBeforeLogoff(ByVal Sender As Object, ByVal e As System.EventArgs)
Public Sub Field_Changed()
03-08-2013 06:51 AM
I can't verify your Logon Handlers, as what I have doesn't show it (I never needed the application for what I did with ACT), but they look like they would be correct (they are setup properly at least).
For field change event, what you are doing is actually adding a handler to a specific event of a specific object. This is like adding a button click event through the form design, it's just that you are handling all of that in code.
I missed something the first time I looked at your code, and the first portion of your AddHandler isn't correct. You are pointing to the handler, but you should be referencing the event itself that you want to handle.
You would need do to something like this:
Public Sub Field_Changed(byval fieldName as string) 'Do what you need to do here if a particular field changes for this collection End Sub Friend Sub OnLoad(byval App as ActApplication) Implements IPlugin.OnLoad 'Get your field collection of type MutableEntity.FieldCollection Dim fc as MutableEntity.FieldCollection=HostApplication.ApplicationState.CurrentCompany.Fields AddHandler fc.FieldChanged, AddressOf Field_Changed End Sub
I'm assuming that you are getting the proper field collection object in your code. I'm largely just trying to show the structure as it would more-so apply to you. I don't guarantee that this is 100% correct. You'll need to tweak as needed.