Create Simple Custom Form and Add Controls on the Form

Create Simple Custom Form and Add Controls on the Form:-

Create Simple Custom Form and Add Controls on the Form. In this post, I will describe you how to Create Simple Custom Form and controls on the form. As I have already posted how to make the connection with your application into sap b1.  My previous post similarly you need to create the connection for that you can make a function for establishing the connection with sap b1 so that you will not have to write code for making the connection again and again.But even then I will do complete code for your easiness.

  • I am going to create  a class for establishing the connection for your application, making the form, setting the controls on the form and saving the form into  XML file[AdSense-A]
using System;
using System.Windows.Forms;
class SimpleFirstForm
{
    private SAPbouiCOM.Application SBO_Application;
    private SAPbouiCOM.Form oForm;

    //This function creates the connection wirth the sap b1.

    private void SetConnection()
    {

        SAPbouiCOM.SboGuiApi SboGuiApi = null;
        string sConnectionString = null;
        SboGuiApi = new SAPbouiCOM.SboGuiApi();
        sConnectionString = System.Convert.ToString(Environment.GetCommandLineArgs().GetValue(1));
        SboGuiApi.Connect(sConnectionString);

        SBO_Application = SboGuiApi.GetApplication(-1);
    }

    //This function creates the form and setting the controls on the from.

    private void CreateControlsOnForm()
    {

        //Declared the control’s type of variable

        SAPbouiCOM.Item oItem = null;
        S APbouiCOM.Button oButton = null;
        SAPbouiCOM.StaticText oStaticText = null;
        SAPbouiCOM.EditText oEditText = null;
        SAPbouiCOM.ComboBox oComboBox = null;

        // Creating  a new form object.
        SAPbouiCOM.FormCreationParams oCreationParams = null;
        oCreationParams = ((SAPbouiCOM.FormCreationParams)(SBO_Application.CreateObject(SAPbouiCOM.BoCreatableObjectType.cot_FormCreationParams)));
        oCreationParams.BorderStyle = SAPbouiCOM.BoFormBorderStyle.fbs_Fixed;
        oCreationParams.UniqueID = “MySimpleForm”;
        oForm = SBO_Application.Forms.AddEx(oCreationParams);

        // adding  User Data Source to the form.
        oForm.DataSources.UserDataSources.Add( “EditSource”, SAPbouiCOM.BoDataType.dt_SHORT_TEXT, 20);
        oForm.DataSources.UserDataSources.Add( “CombSource”, SAPbouiCOM.BoDataType.dt_SHORT_TEXT, 20);

        // setting  properties of the form
        oForm.Title = “Simple Form”;
        oForm.Left = 400;
        oForm.Top = 100;
        oForm.ClientHeight = 80;
        oForm.ClientWidth = 350;

        //************* Adding Items to the form  and setting their properties.***********************//

        //Adding a ok button to the form.One thing you must know when we put the button uid as 1 or 2 then b1 automatically handles it’s event.

        oItem = oForm.Items.Add( “1”, SAPbouiCOM.BoFormItemTypes.it_BUTTON);
        oItem.Left = 6;
        oItem.Width = 65;
        oItem.Top = 51;
        oItem.Height = 19;
        oButton = ((SAPbouiCOM.Button)(oItem.Specific));
        oButton.Caption = “Ok”;

        //Similarly adding the cancel button

        oItem = oForm.Items.Add( “2”, SAPbouiCOM.BoFormItemTypes.it_BUTTON);
        oItem.Left = 75;
        oItem.Width = 65;
        oItem.Top = 51;
        oItem.Height = 19;
        oButton = ((SAPbouiCOM.Button)(oItem.Specific));
        oButton.Caption = “Cancel”;
        //Adding a rectsngle.
        oItem = oForm.Items.Add( “Rect1”, SAPbouiCOM.BoFormItemTypes.it_RECTANGLE);
        oItem.Left = 0;
        oItem.Width = 344;
        oItem.Top = 1;
        oItem.Height = 49;

        //Adding a label for that we use the SataticText .

        oItem = oForm.Items.Add( “StaticTxt1”, SAPbouiCOM.BoFormItemTypes.it_STATIC);
        oItem.Left = 7;
        oItem.Width = 148;
        oItem.Top = 8;
        oItem.Height = 14;

        oItem.LinkTo = “EditText1”;

        oStaticText = ((SAPbouiCOM.StaticText)(oItem.Specific));

        oStaticText.Caption = “Static Text 1”;
        //Adding another label
        oItem = oForm.Items.Add( “StaticTxt2”, SAPbouiCOM.BoFormItemTypes.it_STATIC);
        oItem.Left = 7;
        oItem.Width = 148;
        oItem.Top = 24;
        oItem.Height = 14;
        oItem.LinkTo = “ComboBox1”;
        oStaticText = ((SAPbouiCOM.StaticText)(oItem.Specific));
        oStaticText.Caption = “Static Text 2”;

        // Adding a textbox
        oItem = oForm.Items.Add( “EditText1”, SAPbouiCOM.BoFormItemTypes.it_EDIT);
        oItem.Left = 157;
        oItem.Width = 163;
        oItem.Top = 8;
        oItem.Height = 14;
        oEditText = ((SAPbouiCOM.EditText)(oItem.Specific));

        // binding the textbox item to the defined user data source
        oEditText.DataBind.SetBound(true, “”, “EditSource” );
        oEditText.String = “Edit Text 1”;

        // Adding a ComboBox item
        oItem = oForm.Items.Add( “ComboBox1”, SAPbouiCOM.BoFormItemTypes.it_COMBO_BOX);
        oItem.Left = 157;
        oItem.Width = 163;
        oItem.Top = 24;
        oItem.Height = 14;
        oItem.DisplayDesc = false;
        oComboBox = ((SAPbouiCOM.ComboBox)(oItem.Specific));

        // bind the Combo Box item to the defined user data source
        oComboBox.DataBind.SetBound(true, “”, “CombSource” );

        oComboBox.ValidValues.Add( “1”, “Combo Value 1” );
        oComboBox.ValidValues.Add( “2”, “Combo Value 2” );
        oComboBox.ValidValues.Add( “3”, “Combo Value 3” );
    }

    //This function Saves the xml of the form.
    private void SaveAsXML()
    {
        //It is recommendation that always use XML to work with user forms.
        // after creating our form we should save it as an XML file

        System.Xml.XmlDocument oXmlDoc = null;
        oXmlDoc = new System.Xml.XmlDocument();

        string sXmlString = null;

        // get the form as an XML string
        sXmlString = oForm.GetAsXML();

        // load the form’s XML string to the
        // XML document object
        oXmlDoc.LoadXml(sXmlString);

        // save the XML Document
        string sPath = null;

        sPath = System.IO.Directory.GetParent(Application.StartupPath).ToString();

        oXmlDoc.Save((sPath + @”\SimpleFirstForm.xml” ) );

    }

    //This is the constructor in which we calling all the functions.
    public SimpleFirstForm()
    {
        // This function create the connection  with the SAP b1.
        SetConnection();
        //This function creates the form and sets the controls on to the form.
        CreateControlsOnForm();

        oForm.Visible = true;
        // This function creates the xml of the form.
        SaveAsXML();
    }
}

 

  • Now we will create the Startup of the project.
using System;
using System.Windows.Forms;

class StartMain
{
 static void Main()
 {
 SimpleFirstForm oSForm = null;
 oSForm = new SimpleForm();
 }
}

 


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

error: Content is protected !!