using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using SAPbouiCOM;
namespace Matrix
{
class Matrix
{
//**********************************************************
// This parameter will use us to manipulate the
// SAP Business One Application
//**********************************************************
private SAPbouiCOM.Application SBO_Application;
private SAPbouiCOM.Form oForm;
private SAPbouiCOM.Matrix oMatrix;
private SAPbouiCOM.Columns oColumns;
private SAPbouiCOM.Column oColumn;
// declareing a DB data source for all the Data binded columns
private SAPbouiCOM.DBDataSource oDBDataSource;
// declaring a User data source for the "Remarks" Column
private SAPbouiCOM.UserDataSource oUserDataSource;
private SAPbouiCOM.DataTable oDataTable;
private static bool init = false;
// *****************************************************************
// This Function is called automatically when an instance
// of the class is created.
// Indise this function
// *****************************************************************
public Matrix()
{
//*************************************************************
// set SBO_Application with an initialized application object
//*************************************************************
SetApplication();
// Create the UI
CreateFormWithMatrix();
// Add Data Sources to the Form
AddDataSourceToForm();
// Bind the Form's items with the desired data source
BindDataToForm();
// Load date to matrix
GetDataFromDataSource();
// Make the form visible
oForm.Visible = true;
// events handled by SBO_Application_AppEvent
SBO_Application.AppEvent += new SAPbouiCOM._IApplicationEvents_AppEventEventHandler(SBO_Application_AppEvent);
// events handled by SBO_Application_ItemEvent
SBO_Application.ItemEvent += new SAPbouiCOM._IApplicationEvents_ItemEventEventHandler(SBO_Application_ItemEvent);
//form load
//SBO_Application.
}
private void SetApplication()
{
// *******************************************************************
// Use an SboGuiApi object to establish connection
// with the SAP Business One application and return an
// initialized appliction object
// *******************************************************************
SAPbouiCOM.SboGuiApi SboGuiApi = null;
string sConnectionString = null;
SboGuiApi = new SAPbouiCOM.SboGuiApi();
// by following the steps specified above, the following
// statment should be suficient for either development or run mode
sConnectionString = System.Convert.ToString(Environment.GetCommandLineArgs().GetValue(2));
// connect to a running SBO Application
try
{
// If there's no active application the connection will fail
SboGuiApi.Connect(sConnectionString);
}
catch
{ // Connection failed
System.Windows.Forms.MessageBox.Show("No SAP Business One Application was found");
System.Environment.Exit(0);
}
// get an initialized application object
SBO_Application = SboGuiApi.GetApplication(-1);
// SBO_Application.MessageBox("Hello World")
}
private void SBO_Application_AppEvent(SAPbouiCOM.BoAppEventTypes EventType)
{
switch (EventType)
{
case SAPbouiCOM.BoAppEventTypes.aet_ShutDown:
SBO_Application.MessageBox("A Shut Down Event has been caught" + Environment.NewLine + "Terminating 'Add Menu Item' Add On...", 1, "Ok", "", "");
// terminating the Add On
System.Windows.Forms.Application.Exit();
break;
}
}
private void CreateFormWithMatrix()
{
//*******************************************************
// Don't Forget:
// it is much more efficient to load a form from xml.
// use code only to create your form.
// once you have created it save it as XML.
// see "WorkingWithXML" sample project
//*******************************************************
// we will use the following object to add items to our form
SAPbouiCOM.Item oItem = null;
// *******************************************
// we will use the following objects to set
// the specific values of every item
// we add.
// this is the best way to do so
//*********************************************
SAPbouiCOM.Button oButton = null;
SAPbouiCOM.StaticText oStaticText = null;
SAPbouiCOM.EditText oEditText = null;
// The following object is needed to create our form
SAPbouiCOM.FormCreationParams creationPackage;
//object creationPackage = null;
object tmp = null;
SAPbouiCOM.FormCreationParams oCreationParams = null;
tmp = SBO_Application.CreateObject(SAPbouiCOM.BoCreatableObjectType.cot_FormCreationParams);
creationPackage = ((SAPbouiCOM.FormCreationParams)(SBO_Application.CreateObject(SAPbouiCOM.BoCreatableObjectType.cot_FormCreationParams)));
creationPackage.UniqueID = "UidFrmMatrix";
creationPackage.FormType = "TypeFrmMatrix";
// Add our form to the SBO application
oForm = SBO_Application.Forms.AddEx(((SAPbouiCOM.FormCreationParams)(creationPackage)));
// Set the form properties
oForm.Title = "Matrix, Datasources aind Linked Button";
oForm.Left = 336;
oForm.ClientWidth = 520;
oForm.Top = 44;
oForm.ClientHeight = 200;
//*****************************************
// Adding Items to the form
// and setting their properties
//*****************************************
// /**********************
// Adding an Ok button
//*********************
// We get automatic event handling for
// the Ok and Cancel Buttons by setting
// their UIDs to 1 and 2 respectively
oItem = oForm.Items.Add("1", SAPbouiCOM.BoFormItemTypes.it_BUTTON);
oItem.Left = 5;
oItem.Width = 65;
oItem.Top = 170;
oItem.Height = 19;
oButton = ((SAPbouiCOM.Button)(oItem.Specific));
oButton.Caption = "Ok";
//************************
// Adding a Cancel button
//***********************
oItem = oForm.Items.Add("2", SAPbouiCOM.BoFormItemTypes.it_BUTTON);
oItem.Left = 75;
oItem.Width = 65;
oItem.Top = 170;
oItem.Height = 19;
oButton = ((SAPbouiCOM.Button)(oItem.Specific));
oButton.Caption = "Cancel";
//******************************************
// Adding an Load/Flush column button
//******************************************
oItem = oForm.Items.Add("BtnLoad", SAPbouiCOM.BoFormItemTypes.it_BUTTON);
oItem.Left = 160;
oItem.Width = 100;
oItem.Top = 170;
oItem.Height = 19;
oButton = ((SAPbouiCOM.Button)(oItem.Specific));
oButton.Caption = "Load";
//******************************************
// Adding an Load/Flush column button
//******************************************
oItem = oForm.Items.Add("BtnFlush", SAPbouiCOM.BoFormItemTypes.it_BUTTON);
oItem.Left = 265;
oItem.Width = 100;
oItem.Top = 172;
oItem.Height = 19;
oButton = ((SAPbouiCOM.Button)(oItem.Specific));
oButton.Caption = "Flush";
// Add the matrix to the form
AddMatrixToForm();
}
private void AddMatrixToForm()
{
// we will use the following object to add items to our form
SAPbouiCOM.Item oItem = null;
// we will use the following object to set a linked button
SAPbouiCOM.LinkedButton oLink = null;
//***************************
// Adding a Matrix item
//***************************
oItem = oForm.Items.Add("Matrix1", SAPbouiCOM.BoFormItemTypes.it_MATRIX);
oItem.Left = 5;
oItem.Width = 500;
oItem.Top = 5;
oItem.Height = 150;
oMatrix = ((SAPbouiCOM.Matrix)(oItem.Specific));
oColumns = oMatrix.Columns;
//***********************************
// Adding Culomn items to the matrix
//***********************************
oColumn = oColumns.Add("#", SAPbouiCOM.BoFormItemTypes.it_EDIT);
oColumn.TitleObject.Caption = "#";
oColumn.Width = 30;
oColumn.Editable = false;
// Add a column for BP Card Code
oColumn = oColumns.Add("DSCardCode", SAPbouiCOM.BoFormItemTypes.it_LINKED_BUTTON);
oColumn.TitleObject.Caption = "Card Code";
oColumn.Width = 40;
oColumn.Editable = true;
// Link the column to the BP master data system form
oLink = ((SAPbouiCOM.LinkedButton)(oColumn.ExtendedObject));
oLink.LinkedObject = SAPbouiCOM.BoLinkedObject.lf_BusinessPartner;
SAPbouiCOM.ChooseFromListCollection oCFLs = oForm.ChooseFromLists;
SAPbouiCOM.ChooseFromListCreationParams oCFLCreationParams;
oCFLCreationParams = (SAPbouiCOM.ChooseFromListCreationParams)SBO_Application.CreateObject(BoCreatableObjectType.cot_ChooseFromListCreationParams);
//Adding a choosefromlist for the column
oCFLCreationParams.MultiSelection = false;
oCFLCreationParams.ObjectType = "2";
oCFLCreationParams.UniqueID = "CFL1";
SAPbouiCOM.ChooseFromList oCFL = oCFLs.Add(oCFLCreationParams);
SAPbouiCOM.UserDataSource udsCardCode = oForm.DataSources.UserDataSources.Add("UdsCardCd", BoDataType.dt_SHORT_TEXT, 10);
oColumn.DataBind.SetBound(true, "", "UdsCardCd");
oColumn.ChooseFromListUID = "CFL1";
// Add a column for BP Card Name
oColumn = oColumns.Add("DSCardName", SAPbouiCOM.BoFormItemTypes.it_EDIT);
oColumn.TitleObject.Caption = "Name";
oColumn.Width = 40;
oColumn.Editable = true;
// Add a column for BP Card Phone
oColumn = oColumns.Add("DSPhone", SAPbouiCOM.BoFormItemTypes.it_EDIT);
oColumn.TitleObject.Caption = "Phone";
oColumn.Width = 40;
oColumn.Editable = true;
// Add a column for Date
oColumn = oColumns.Add("Date", SAPbouiCOM.BoFormItemTypes.it_EDIT);
oColumn.TitleObject.Caption = "Date";
oColumn.Width = 40;
oColumn.Editable = true;
oForm.DataSources.UserDataSources.Add("DSDate", BoDataType.dt_DATE, 20);
oColumn.DataBind.SetBound(true, "", "DSDate");
}
public void AddDataSourceToForm()
{
// Add data table for the DB bound columns in the matrix
oForm.DataSources.DataTables.Add("MyDataTable");
oForm.DataSources.DataTables.Item(0).ExecuteQuery("select CardCode, CardName, Phone1 from OCRD");
oDataTable = oForm.DataSources.DataTables.Item(0);
}
public void BindDataToForm()
{
// getting the matrix column by the UID
//oColumn = oColumns.Item("#");
//oColumn.DataBind.Bind("MyDataTable", "RowID");
oColumn = oColumns.Item("DSCardCode");
oColumn.DataBind.Bind("MyDataTable", "CardCode");
oColumn = oColumns.Item("DSCardName");
oColumn.DataBind.Bind("MyDataTable", "CardName");
oColumn = oColumns.Item("DSPhone");
oColumn.DataBind.Bind("MyDataTable", "Phone1");
}
public void GetDataFromDataSource()
{
// Ready Matrix to populate data
oMatrix.Clear();
oMatrix.AutoResizeColumns();
oMatrix.LoadFromDataSource();
}
private void SBO_Application_ItemEvent(string FormUID, ref SAPbouiCOM.ItemEvent pVal, out bool BubbleEvent)
{
BubbleEvent = true;
if ((pVal.FormUID == "UidFrmMatrix"))
{
if (((pVal.ItemUID == "BtnFlush") & (pVal.EventType == SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED) & (pVal.Before_Action == false)))
{
FlushData();
}
if (((pVal.ItemUID == "BtnLoad") & (pVal.EventType == SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED) & (pVal.Before_Action == false)))
{
LoadData();
}
if (((pVal.EventType == SAPbouiCOM.BoEventTypes.et_FORM_UNLOAD) & (pVal.Before_Action == false)))
{
SBO_Application.MessageBox("Form Unloaded, Addon will terminate", 1, "Ok", "", "");
System.Windows.Forms.Application.Exit();
}
}
else if (pVal.FormType == 139)
{
if (((pVal.ItemUID == "38") && (pVal.ColUID == "U_PickList") && (pVal.EventType == SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED) && (pVal.Before_Action == false)))
{
this.OnPickListClick();
}
if (((pVal.ItemUID == "38") && (pVal.ColUID == "U_PickList") && (pVal.EventType == SAPbouiCOM.BoEventTypes.et_DOUBLE_CLICK) && (pVal.Before_Action == false)))
{
SBO_Application.MessageBox("Open your own form here", 1, "Ok", "", "");
}
}
}
public void LoadData()
{
//Set the new phone number in the DataTable, at Phone1 of the 1st row
string newPhone = "61080000";
oDataTable = oForm.DataSources.DataTables.Item(0);
oDataTable.SetValue(2, 0, newPhone);
//Load data from DataTable to GUI
oMatrix.LoadFromDataSource();
}
public void FlushData()
{
//Flush the data changed in GUI to DataTable
oMatrix.FlushToDataSource();
//Check the new phone number in DataTable
string newPhone = (string)(oForm.DataSources.DataTables.Item(0).GetValue(2, 0));
SBO_Application.MessageBox(newPhone, 1, "Ok", "", "");
}
public void OnPickListClick()
{
if(init)
{
return;
}
SAPbouiCOM.Form soForm = SBO_Application.Forms.ActiveForm;
// we will use the following object to add items to our form
SAPbouiCOM.Item oItem = null;
// we will use the following object to set a linked button
SAPbouiCOM.LinkedButton oLink = null;
SAPbouiCOM.Matrix soMatrix = null;
//***************************
// Getting a Matrix item
//***************************
oItem = soForm.Items.Item(38);
soMatrix = ((SAPbouiCOM.Matrix)(oItem.Specific));
oColumns = soMatrix.Columns;
Add a column for BP Card Code
//oColumn = oColumns.Item("U_PickList");
SAPbouiCOM.EditText et = (SAPbouiCOM.EditText)oColumn.Cells.Item(1).Specific;
et.Value = "1";
Link the column to the BP master data system form
//oLink = ((SAPbouiCOM.LinkedButton)(oColumn.ExtendedObject));
//oLink.LinkedObject = SAPbouiCOM.BoLinkedObject.lf_PickList;
// Add a column for BP Card Code
oColumn = oColumns.Add("PickListNum", SAPbouiCOM.BoFormItemTypes.it_LINKED_BUTTON);
oColumn.TitleObject.Caption = "Pick List#";
oColumn.Width = 40;
oColumn.Editable = true;
// Link the column to the BP master data system form
oLink = ((SAPbouiCOM.LinkedButton)(oColumn.ExtendedObject));
oLink.LinkedObject = SAPbouiCOM.BoLinkedObject.lf_PickList;
soForm.DataSources.DataTables.Add("PickListDataTable");
soForm.DataSources.DataTables.Item(0).ExecuteQuery("select U_PickList from RDR1");
oDataTable = oForm.DataSources.DataTables.Item(0);
oColumn.DataBind.Bind("PickListDataTable", "U_PickList");
init = true;
}
}
}