/// <summary>
        /// Initalize and show form
        /// </summary>
        public void Start(BoFormModality modality = BoFormModality.fm_None)
        {
            try
            {
                var assembly = GetType().Assembly;
                Form = FormHelper.CreateFormFromResource(FormResource, FormType, null, assembly, modality);
                SboApp.Logger.Debug($"Form created: Type={Form.Type}, UID={Form.UniqueID}");
                UniqueID = Form.UniqueID;
                try
                {
                    FormCreated();
                }
                catch (Exception e)
                {
                    SboApp.Application.MessageBox($"FormCreated Error: {e.Message}");
                }
                SboApp.Application.ItemEvent -= ItemEventHandler;
                SboApp.Application.ItemEvent += ItemEventHandler;
                try
                {
                    BindFormEvents();
                }
                catch (Exception e)
                {
                    SboApp.Application.MessageBox($"BindFormEvents Error: {e.Message}");
                }

                Form.Visible = true;
            }
            catch (Exception e)
            {
                SboApp.Application.MessageBox($"Failed to open form {FormType}: {e.Message}");
            }
        }
Пример #2
0
        private Form CreateForm(string formType, string formId, string xmlData, BoFormModality modality = BoFormModality.fm_None)
        {
            var formDefinition = Application.Create <FormCreationParams>(BoCreatableObjectType.cot_FormCreationParams);

            formDefinition.XmlData  = xmlData;
            formDefinition.UniqueID = formId;
            formDefinition.FormType = formType;
            formDefinition.Modality = modality;

            return(Application.Forms.AddEx(formDefinition));
        }
        /// <summary>
        /// Create SBO Form from internal resource
        /// </summary>
        /// <param name="resourceName"></param>
        /// <param name="formtype">Form type (identifyer)</param>
        /// <param name="formId">Optional FormId</param>
        /// <param name="assembly"></param>
        /// <returns>IForm reference</returns>
        public static IForm CreateFormFromResource(string resourceName, string formtype, string formId = null, Assembly assembly = null, BoFormModality modality = BoFormModality.fm_None)
        {
            if (assembly == null)
            {
                assembly = Assembly.GetCallingAssembly();
            }

            if (formId != null)
            {
                // Try get existing form
                try
                {
                    var form = SboApp.Application.Forms.Item(formId);
                    return(form);
                }
                catch
                {
                    // ignored
                }
            }

            try
            {
                string formXml;

                resourceName = string.Concat(assembly.GetName().Name, ".", resourceName);
                var stream = assembly.GetManifestResourceStream(resourceName);
                if (stream == null)
                {
                    var embededResources = string.Join(", ", assembly.GetManifestResourceNames());
                    throw new Exception($"Failed to load embeded resource '{resourceName}' from Assembly '{assembly.GetName().Name}'. Available Resources: {embededResources}");
                }

                using (var textStreamReader = new System.IO.StreamReader(stream))
                    formXml = textStreamReader.ReadToEnd();

                var creationPackage = SboApp.Application.CreateObject(BoCreatableObjectType.cot_FormCreationParams) as FormCreationParams;

                creationPackage.FormType    = formtype;
                creationPackage.BorderStyle = BoFormBorderStyle.fbs_Fixed;
                creationPackage.XmlData     = formXml;
                creationPackage.Modality    = modality;

                if (formId != null)
                {
                    creationPackage.UniqueID = formId;
                }

                var form = SboApp.Application.Forms.AddEx(creationPackage);
                return(form);
            }
            catch (Exception e)
            {
                throw new Exception($"Failed to create form from resource {resourceName}: {e.Message}");
            }
        }