OnFormInitialize

From AutoCount Resource Center
Jump to navigation Jump to search

OnFormInitialize

OnFormInitialize is triggered when a form is generating.
Form Caching
OnFormInitialize is not triggered when the form is loaded from the cache (temporary stored in memory).
Most of the entry forms have built-in Caching capability.
When the form is loaded from cache after first time loaded, it does not require to be reinitialized.
Hence, OnFormInitialize event is not trigger.
Tab Pages
User Defined Field has not been created at OnFormInitialize event.
UDF is created in OnDataBinding.
  • There are times when user wants refresh the form caching without re-login AutoCount Accounting, user can go to
Tools | Clear Cache & Criteria Data
and click [Clear Form Cache] button.

Usage

There are many usage for this event, because the event arguments contain properties from User Interface to Invoice Entity.

  • Access GridView on the Form
  • Add User Button
  • Reallocate component's location
  • Add addition User Access Control to components (button, textedit, lookupedit, etc.)
  • Some function requires combination of OnDataBinding event to work properly
    eg. Create a TextEdit in OnFormInitialize, and add DataBindings of table column to TextEdit in OnDataBinding.

OnFormInitialize event in Sales Invoice

public void OnFormInitialize(BCE.AutoCount.Invoicing.Sales.Invoice.FormInvoiceEntry.FormInitializeEventArgs e)

Example

Access GridView on the form

public void OnFormInitialize(BCE.AutoCount.Invoicing.Sales.Invoice.FormInvoiceEntry.FormInitializeEventArgs e)
{
    //Use 'e' to retrieve GridView in the GridControl
    DevExpress.XtraGrid.Views.Grid.GridView gridView = (DevExpress.XtraGrid.Views.Grid.GridView)e.GridControl.DefaultView;
}

GridControl holds the object BaseView. The hierarchy of BaseView is the base class of:

  • GridView
  • LayoutView
  • CardView
  • WinExplorerView
  • TileView

We know the GridControl.DefaultView is a GridView.
Therefore, we can cast the object of DefaultView to DevExpress.XtraGrid.Views.Grid.GridView.


Add a button to prompt a message to display Debtor's Outstanding and Last Payment Amount

Add using Directive

using BCE.Data;
using BCE.AutoCount.Invoicing.Sales.Invoice;
using DevExpress.XtraEditors;
public void OnFormInitialize(FormInvoiceEntry.FormInitializeEventArgs e)
{
    //Use anonymous method to pass DBSetting and Invoice Object to Click Event
    EventHandler debtorInfoClickHandler = (s, args) => BtnDebtorInfo_Click(e.DBSetting, e.Invoice);
    SimpleButton btnDebtorInfo = CreateButtonDebtorInfo();
    e.HeaderPanel.Controls.Add(btnDebtorInfo);
    btnDebtorInfo.BringToFront();
 
    btnDebtorInfo.Click -= debtorInfoClickHandler;
    btnDebtorInfo.Click += debtorInfoClickHandler;
}

private void BtnDebtorInfo_Click(DBSetting dbSetting, Invoice iv)
{
    BCE.Application.AppMessage.ShowMessage(GetDebtorInfoMessage(dbSetting, iv));
}

private string GetDebtorInfoMessage(DBSetting dbSetting, Invoice iv)
{
    decimal outstanding = iv.SalesCreditControl.AROutstanding;
    return string.Format("{0}\nOutstanding:{1}",
        LastPaymentMessage(iv.DebtorCode, dbSetting),
        outstanding);
}

private string LastPaymentMessage(string debtorCode, DBSetting dbSetting)
{
    string sqlSelect = "SELECT TOP 1 CONCAT(DocNo, '\n'"
        + ", Format(DocDate,'dd/MM/yyyy')"
        + ", '\n', COALESCE(PaymentAmt, 0)) AS [DebtorInfo]"
        + " FROM ARPayment"
        + " WHERE Cancelled = 'F' AND DebtorCode = ?"
        + " ORDER BY DocDate DESC";
    object obj = dbSetting.ExecuteScalar(sqlSelect, debtorCode);

    return (obj == null)
        ? "No Payment Found."
        : string.Format("Last Payment:\n{0}", obj.ToString());
}

private SimpleButton CreateButtonDebtorInfo()
{
    DevExpress.XtraEditors.SimpleButton btn = new DevExpress.XtraEditors.SimpleButton();
    btn.Text = "Show Debtor Info";
    btn.Size = new System.Drawing.Size(200, 35);
    btn.Location = new System.Drawing.Point(480, 210);
    return btn;
}


See Also

Go to menu

IconAC81.png Go to top
ProgrammerGo.jpg Resources For AutoCount Software Developers