Hi, everybody
We are trying to rewrite our very long VB.NET add-on (which uses a single class) using multiple classes (one class for one form) in order to make our code more readable and easier to maintain. We are not using B1DE because we have not yet learned how to use this tool.
So we made a simple application as a start. See pictures below.
But we are stuck. Please see our codes below.
Could anybody tell us what is wrong? The code is standard, taken from SDK samples.Please see the codes in bold.
When we run the application, we get this error:
Thanks
Leon Lai
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
HERE ARE OUR CODES
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Module SubMain
Public Sub Main()
Dim oclass1 As Class1
oclass1 = New Class1
System.Windows.Forms.Application.Run()
End Sub
End Module
--------------------------------------------------------------------------------------------------------------------------------------
Public Class Class1
Public WithEvents SBO_Application As SAPbouiCOM.Application
Public oForm As SAPbouiCOM.Form
Public oItem As SAPbouiCOM.Item
Public oCompany As SAPbobsCOM.Company
Public Sub New()
MyBase.New()
'//*************************************************************
'// set SBO_Application with an initialized application object
'//*************************************************************
SetApplication()
SetConnectionContext()
ConnectToCompany()
'//*************************************************************
'// Create the Form
'//*************************************************************
CreateForm()
oForm.Visible = True
End Sub
Public Sub SetApplication()
'==============================================
'//Use an SboGuiApi object to establish connection
'//with the SAP Business One application and return an
'//initialized appliction object
'==============================================
Dim SboGuiApi As SAPbouiCOM.SboGuiApi
Dim sConnectionString As String
SboGuiApi = New SAPbouiCOM.SboGuiApi
'==================================================================
'//by following the steps specified above, the following
'//statement should be suficient for either development or run mode
'==================================================================
Try
If Environment.GetCommandLineArgs.Length > 1 Then
sConnectionString = Environment.GetCommandLineArgs.GetValue(1)
Else
sConnectionString = Environment.GetCommandLineArgs.GetValue(0)
End If
Catch ex As Exception
End Try
'==============================================
'// Connect to a running SBO Application
'==============================================
SboGuiApi.Connect(sConnectionString)
'==============================================
'// Get an initialized application object.
'==============================================
SBO_Application = SboGuiApi.GetApplication()
oCompany = New SAPbobsCOM.Company
oCompany = SBO_Application.Company.GetDICompany()
End Sub
Public Function SetConnectionContext() As Integer
'==============================================
'//Single-Sign On used to connect DI API
'==============================================
Dim sCookie As String
Dim sConnectionContext As String
oCompany = New SAPbobsCOM.Company
sCookie = oCompany.GetContextCookie
sConnectionContext = SBO_Application.Company.GetConnectionContext(sCookie)
If oCompany.Connected = True Then
oCompany.Disconnect()
End If
SetConnectionContext = oCompany.SetSboLoginContext(sConnectionContext)
End Function
Public Function ConnectToCompany() As Integer
'==============================================
'//CONNECTING DI API
'==============================================
ConnectToCompany = oCompany.Connect
End Function
Private Sub CreateForm()
'==============================================
'// CREATE FORM
'==============================================
Dim oFrmParam As SAPbouiCOM.FormCreationParams
Dim oBtn As SAPbouiCOM.Button
oFrmParam = SBO_Application.CreateObject(SAPbouiCOM.BoCreatableObjectType.cot_FormCreationParams)
oFrmParam.BorderStyle = SAPbouiCOM.BoFormBorderStyle.fbs_Sizable
oFrmParam.UniqueID = "Class1"
'==============================================
'//ADD FORM
'==============================================
oForm = SBO_Application.Forms.AddEx(oFrmParam)
'==============================================
'//Set form width and height
'==============================================
oForm.Height = 500
oForm.Width = 650
oForm.Title = "xxx"
oItem = oForm.Items.Add("btnOk", SAPbouiCOM.BoFormItemTypes.it_BUTTON)
oItem.Top = 80
oItem.Left = 480
oItem.Width = 100
oBtn = oItem.Specific
oBtn.Caption = "OK"
End Sub
Private Sub SBO_Application_ItemEvent(ByVal FormUID As String, ByRef pVal As SAPbouiCOM.ItemEvent, ByRef BubbleEvent As Boolean) Handles SBO_Application.ItemEvent
If pVal.ItemUID = "btnOk" And pVal.BeforeAction = False And pVal.EventType = SAPbouiCOM.BoEventTypes.et_CLICK Then
Dim oClass2 As Class2
oClass2 = New Class2
oClass2.Create()
End If
End Sub
End Class
---------------------------------------------------------------------------------------------------------------------------------------------------------
Public Class Class2
Public WithEvents SBO_Application As SAPbouiCOM.Application
Public oForm As SAPbouiCOM.Form
Public oItem As SAPbouiCOM.Item
Public Sub Create()
'==============================================
'// CREATE FORM
'==============================================
Dim oFrmParam1 As SAPbouiCOM.FormCreationParams
oFrmParam1 = SBO_Application.CreateObject(SAPbouiCOM.BoCreatableObjectType.cot_FormCreationParams)
oFrmParam1.BorderStyle = SAPbouiCOM.BoFormBorderStyle.fbs_Sizable
oFrmParam1.UniqueID = "Class2"
'==============================================
'//ADD FORM
'==============================================
oForm = SBO_Application.Forms.AddEx(oFrmParam1)
'//Set form width and height
'==============================================
oForm.Height = 500
oForm.Width = 650
oForm.Title = "yyy"
oForm.Visible = True
End Sub
End Class