How to detect the newly added currencies in Currencies and Exchange Rate form on save button click?

SOLVED

Hi all,

On the form Currencies and Exchange Rate, I need to detect if user have added new currency on the currenciesGrid and prompt user and at the moment the Save button is clicked. But I am not getting much information about it.

OnClick Save button, I can get the currenciesGrid items but there is no telling whether the item are newly added or not. Please help. Thank you.

Parents
  • +1
    verified answer

    Get hold of the MaintainCurrenciesCoordinator object via reflection like this:

    private MaintainCurrenciesCoordinator GetCoordinator()
    {
        FieldInfo fi = _form.UnderlyingControl.GetType().GetField("_oCurrenciesCoordinator", BindingFlags.Instance | BindingFlags.NonPublic);
        if(fi != null)
        {
            return (MaintainCurrenciesCoordinator) fi.GetValue(_form.UnderlyingControl);
        }
        return null;
    }

    Then in the form BeginSave you will be able to determine which items have been added or amended:

    private void _form_BeginSave(object sender, System.ComponentModel.CancelEventArgs e)
    {
        if(coordinator.IsDirty)
        {
            foreach(FinancialCurrency _fc in coordinator.Currencies)
            {
                if(_fc.IsDirty)
                {
                    if(_fc.IsNew)
                    {
                        MessageBox.Show($"Currency {_fc.Name} has been added");
                    }
                    else
                    {
                        MessageBox.Show($"Currency {_fc.Name} has been edited");
                    }
                }
            }
        }
    }        

Reply
  • +1
    verified answer

    Get hold of the MaintainCurrenciesCoordinator object via reflection like this:

    private MaintainCurrenciesCoordinator GetCoordinator()
    {
        FieldInfo fi = _form.UnderlyingControl.GetType().GetField("_oCurrenciesCoordinator", BindingFlags.Instance | BindingFlags.NonPublic);
        if(fi != null)
        {
            return (MaintainCurrenciesCoordinator) fi.GetValue(_form.UnderlyingControl);
        }
        return null;
    }

    Then in the form BeginSave you will be able to determine which items have been added or amended:

    private void _form_BeginSave(object sender, System.ComponentModel.CancelEventArgs e)
    {
        if(coordinator.IsDirty)
        {
            foreach(FinancialCurrency _fc in coordinator.Currencies)
            {
                if(_fc.IsDirty)
                {
                    if(_fc.IsNew)
                    {
                        MessageBox.Show($"Currency {_fc.Name} has been added");
                    }
                    else
                    {
                        MessageBox.Show($"Currency {_fc.Name} has been edited");
                    }
                }
            }
        }
    }        

Children