True Modal form with UI API!
User input screen class
Ever needed to get information from your user with a true modal form? If you try to create an input screen using the UI api, you call “Application.form.Add(……..)” but the code continues to run behind the form. It is impossible to request information from the user before moving onto the next line of code.
I have been hitting this wall since I started programing with the SDK.
But I have finaly found a way to do it! By using the only true modal function in the SDK. “MESSAGEBOX”
Message box alone does not give us what we need because it only returns the numerical value of the button pressed.
But, the message box is just a form. (FormTypeEx = “0”). I figured out that the events for this form continue to be lauched while the function that called it is patiently waiting for the user to click “OK”.
So! By using this, it is possible to convert the message box into an INPUT BOX!
Class to encapsulate the functionality:
Public Class cUserInput
Private sInput As String = “”
Private sTitle As String = “”
Public Function ShowInput(Title As String, Message As String) As String
bLoadInputEvents = True
sTitle = Title
oApplication.MessageBox(Message)
ShowInput = sInput
End Function
‘ handles Form event trafic for the form associated to this class instance
Public Overridable Sub ItemEvents(ByRef pVal As SAPbouiCOM.ItemEvent, ByRef BubbleEvent As Boolean)
Try
Select Case pVal.EventType
Case et_ITEM_PRESSED
e_ItemPressed(pVal, BubbleEvent)
Case et_FORM_LOAD
e_FormLoad(pVal, BubbleEvent)
End Select
Catch ex As Exception
oApplication.MessageBox(ex.Message)
End Try
End Sub
Protected Overridable Sub e_FormLoad(ByRef pVal As SAPbouiCOM.ItemEvent, ByRef BubbleEvent As Boolean)
If pVal.BeforeAction = False Then
Dim oform As SAPbouiCOM.Form = oApplication.Forms.Item(pVal.FormUID)
oform.Title = sTitle
oform.Items.Add(“eInput”, SAPbouiCOM.BoFormItemTypes.it_EDIT)
oform.Items.Item(“eInput”).Top = oform.Items.Item(“7”).Top + oform.Items.Item(“7”).Height + 10
oform.Items.Item(“eInput”).Left = oform.Items.Item(“7”).Left
oform.Items.Item(“eInput”).Width = 150
oform = Nothing
End If
End Sub
Protected Overridable Sub e_ItemPressed(ByRef pVal As SAPbouiCOM.ItemEvent, ByRef BubbleEvent As Boolean)
If pVal.ItemUID = “1” And pVal.BeforeAction Then
sInput = oApplication.Forms.Item(pVal.FormUID).Items.Item(“eInput”).Specific.String
If sInput = “” Then
BubbleEvent = False
Else
bLoadInputEvents = False
End If
End If
End Sub
End Class
Using the class:
Global variables
Public Shared bLoadInputEvents As Boolean = False
Public Shared oUserInput As cUserInput
Function Call:
Dim sPassword As String
‘Instanciate class
oUserInput = New cUserInput
‘Call function that will ask our query and wait for the response.
sPassword = oUserInput.ShowInput(“Password”, “What is your password?”)
‘Check results!
If sPassword = “1234” Then
oApplication.MessageBox(“Correct!”)
Else
oApplication.MessageBox(“Incorrect!”)
End If
oUserInput = Nothing
Event Handling
Private Shared Sub oApplication_ItemEvent(ByVal FormUID As String, ByRef pVal As SAPbouiCOM.ItemEvent, ByRef BubbleEvent As Boolean) Handles oApplication.ItemEvent
Try
‘Catch message input box events.
If pVal.FormTypeEx = “0” And bLoadInputEvents Then
oUserInput.ItemEvents(pVal, BubbleEvent)
End If
Catch ex As Exception
oApplication.StatusBar.SetText(ex.Message)
End Try
End Sub
Results
I hope this helps someone else as much as it helped me!
Cheers !
Denis Doiron
Acabuco Inc.
New NetWeaver Information at SAP.com
Very Helpfull