Page 1 of 1

Run plugin when field changed

PostPosted: Fri Feb 13, 2015 5:06 pm
by DannyC
Hi,

I have the following plugin code which I want to fire when a field has been changed but only when it has lost focus. Currently this fires on every character entered into a field.
Code: Select all
Public Class FormPlugin
    Inherits System.MarshalByRefObject
    Implements JiwaApplication.IJiwaFormPlugin

    Public Overrides Function InitializeLifetimeService() As Object
        ' returning null here will prevent the lease manager
        ' from deleting the Object.
        Return Nothing
    End Function

    Public Sub SetupBeforeHandlers(ByVal JiwaForm As JiwaApplication.IJiwaForm, ByVal Plugin As JiwaApplication.Plugin.Plugin) Implements JiwaApplication.IJiwaFormPlugin.SetupBeforeHandlers
    End Sub

    Public Sub Setup(ByVal JiwaForm As JiwaApplication.IJiwaForm, ByVal Plugin As JiwaApplication.Plugin.Plugin) Implements JiwaApplication.IJiwaFormPlugin.Setup
      Dim InventoryForm As JiwaInventoryUI.InventoryMaintenanceForm = DirectCast(JiwaForm, JiwaApplication.IJiwaForm)
      AddHandler InventoryForm.Inventory.PropertyChanged, AddressOf Changed               
    End Sub
   
   Sub Changed(item As JiwaInventory.Inventory, e As System.ComponentModel.PropertyChangedEventArgs)
      messagebox.show(e.PropertyName)
   End Sub
End Class


I think the line which needs changing is
Code: Select all
      AddHandler InventoryForm.Inventory.PropertyChanged, AddressOf Changed

but I don't know the appropriate event.

I basically need it to behave the same as the old breakout Inventory Changed.

Can you assist?

Thanks

Danny

Re: Run plugin when field changed  Topic is solved

PostPosted: Wed Feb 18, 2015 11:33 am
by Mike.Sheen
Hi Danny,

You probably want to hook into the Validated event of the textbox / control you are interested in.

Attached is a sample plugin which does this for the Part No. textbox of the inventory maintenance form.

Plugin Inventory Field Validation.xml
Sample Plugin
(31.98 KiB) Downloaded 87 times


Mike

Re: Run plugin when field changed

PostPosted: Wed Feb 18, 2015 1:01 pm
by DannyC
Mike,

Thanks for that. Two issues using the Validated.
1. It fires when losing focus whether the value has changed or not. I only want to run my code if the value has changed.
2. What I really want to do is set a custom field with a date when the Sell Price or RRP has changed.

At the moment my code looks like this but I'm getting an error dimming the InventoryForm in the Sellprice_changed sub.

Code: Select all
    Public Sub Setup(ByVal JiwaForm As JiwaApplication.IJiwaForm, ByVal Plugin As JiwaApplication.Plugin.Plugin) Implements JiwaApplication.IJiwaFormPlugin.Setup
      Dim InventoryForm As JiwaInventoryUI.InventoryMaintenanceForm = DirectCast(JiwaForm, JiwaInventoryUI.InventoryMaintenanceForm)
      AddHandler InventoryForm.SellPriceNumericEditor.Validated, AddressOf Sellprice_Changed               
    End Sub
   
   Sub Sellprice_Changed(Sender As Object, e As System.EventArgs)
      'messagebox.show("value: '{0}'")    'This doesn't work
      Dim InventoryForm As JiwaInventory.Inventory = DirectCast(Sender, JiwaInventory.Inventory)
      InventoryForm.CustomFieldValues.ItemFromSettingName("SellChangeDate").Contents = Now.ToString()
   End Sub


Thanks
Danny

Re: Run plugin when field changed

PostPosted: Sun Feb 22, 2015 2:42 pm
by Mike.Sheen
DannyC wrote:Mike,

Thanks for that. Two issues using the Validated.
1. It fires when losing focus whether the value has changed or not. I only want to run my code if the value has changed.
2. What I really want to do is set a custom field with a date when the Sell Price or RRP has changed.

At the moment my code looks like this but I'm getting an error dimming the InventoryForm in the Sellprice_changed sub.

Code: Select all
    Public Sub Setup(ByVal JiwaForm As JiwaApplication.IJiwaForm, ByVal Plugin As JiwaApplication.Plugin.Plugin) Implements JiwaApplication.IJiwaFormPlugin.Setup
      Dim InventoryForm As JiwaInventoryUI.InventoryMaintenanceForm = DirectCast(JiwaForm, JiwaInventoryUI.InventoryMaintenanceForm)
      AddHandler InventoryForm.SellPriceNumericEditor.Validated, AddressOf Sellprice_Changed               
    End Sub
   
   Sub Sellprice_Changed(Sender As Object, e As System.EventArgs)
      'messagebox.show("value: '{0}'")    'This doesn't work
      Dim InventoryForm As JiwaInventory.Inventory = DirectCast(Sender, JiwaInventory.Inventory)
      InventoryForm.CustomFieldValues.ItemFromSettingName("SellChangeDate").Contents = Now.ToString()
   End Sub


Thanks
Danny


Hi Danny,

Ok - you really should get the price at read end of the business logic, and then at save starting compare the two and then set your custom field value. Relying on user interface events isn't reliable - as there are many ways the user can set the sell price (sell price text box, or the pricing grid on the prices tab).

Attached is a plugin which works for me.

Plugin Inventory Maintenance - Log date sell price changed by user.xml
Sample plugin
(32.82 KiB) Downloaded 97 times


Mike