views:

147

answers:

0

Does anyone came across a problem when you have a Class that inherits from Forms and then have all the forms inherit from Class, and then tried to load the form in design mode in visual studio? Does it loads correctly, because on mine it shows "Illegal characters in path.". I ran the visual studio in debug mode and found that it is try to check a path for the file I suppose and gets the ´>´ of the generic definition. So is it possible to implement generics over windows forms or do I have to get a workaround for this, I have searched the internet for some examples or clues where to follow but could find any important one. Any pointers would be much appreciated, because the application works runs fine but not the design mode of visual studio. Thanks.

Base class

public class BaseForm<T, TU> : Form, IFormLanguageStrings
        where T : class
        where TU : class
    {
        #region Variables

        public IApplicationValues applicationValues;
        public T businessObject;
        public IEventBroker eventBroker;
        private bool hasChangesToCommit = false;
        public ILanguageManager languageManager;
        public IMessageBox messageBox;
        public TU objService;
        public DatabaseOperationType operationType;
        public event EventHandler OnNeedToCommitChanges;

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="BaseForm"/> class.
        /// </summary>
        public BaseForm()
        {
            Closing += BaseForm_Closing;
            Load += BaseForm_Load;
            if (IsInDesignMode) return;
            SetService();
        }

        #endregion

        #region Form Events

        /// <summary>
        /// Handles the Load event of the BaseForm control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BaseForm_Load(object sender, EventArgs e)
        {
            if (IsInDesignMode) return;
            SetLabels();
            SetControls();
            LoadCombos();
            LoadDataForForm();
            ValidateDataInput();

            if (operationType == DatabaseOperationType.View)
            {
                eventBroker.Unregister(this);
                DisableAllControls();
                InvisibleSelectControls("BtnAction");
            }
        }

        /// <summary>
        /// Handles the Closing event of the BaseForm control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.ComponentModel.CancelEventArgs"/> instance containing the event data.</param>
        private void BaseForm_Closing(object sender, CancelEventArgs e)
        {
            if (hasChangesToCommit)
            {
                if (
                    messageBox.ShowQuestion(languageManager.GetString("MSG_PerformPendingOperations"),
                                            MessageBoxButton.YesNo, MessageBoxResult.No) == MessageBoxResult.Yes)
                {
                    RaiseCommitChanges();
                }
            }
            if (eventBroker != null)
                eventBroker.Unregister(this);
        }

        #endregion

        #region Virtual Methods

        /// <summary>
        /// Sets the labels.
        /// </summary>
        public virtual void SetLabels()
        {
            Controls.All().OfType<Control>().ToList().ForEach(
                ctl =>
                    {
                        if (!string.IsNullOrEmpty(ctl.Name))
                        {
                            string label = languageManager.GetString(ctl.Name);
                            if (!string.IsNullOrEmpty(label))
                            {
                                ctl.Text = label;
                            }
                            if (ctl is ElementHost)
                            {
                                if (((ElementHost) ctl).Child is IFormLanguageStrings)
                                {
                                    (((ElementHost) ctl).Child as IFormLanguageStrings).SetLabels();
                                }
                            }
                        }
                    });
        }

        /// <summary>
        /// Sets the service.
        /// </summary>
        public virtual void SetService()
        {
            applicationValues = ServiceGateway.GetService<IApplicationValues>();
            languageManager = ServiceGateway.GetService<ILanguageManager>();
            messageBox = ServiceGateway.GetService<IMessageBox>();
            eventBroker = ServiceGateway.GetService<IEventBroker>();
            eventBroker.Register(this);
            objService = ServiceGateway.GetService<TU>();
        }

        /// <summary>
        /// Validates the data input.
        /// </summary>
        public virtual void ValidateDataInput()
        {
        }

        /// <summary>
        /// Determines whether [has to commit changes] [the specified commit].
        /// </summary>
        /// <param name="commit">if set to <c>true</c> [commit].</param>
        public virtual void HasToCommitChanges(bool commit)
        {
            switch (operationType)
            {
                case DatabaseOperationType.Update:
                    hasChangesToCommit = commit;
                    break;
            }
        }

        /// <summary>
        /// Loads the combos.
        /// </summary>
        public virtual void LoadCombos()
        {
        }

        /// <summary>
        /// Loads the data for form.
        /// </summary>
        public virtual void LoadDataForForm()
        {
        }

        /// <summary>
        /// Sets the controls.
        /// </summary>
        public virtual void SetControls()
        {
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Raises the commit changes.
        /// </summary>
        private void RaiseCommitChanges()
        {
            if (OnNeedToCommitChanges != null)
            {
                OnNeedToCommitChanges(this, EventArgs.Empty);
            }
        }

        /// <summary>
        /// Closes the form.
        /// </summary>
        protected void CloseForm()
        {
            FireEventBroker(EventTopics.CloseMdiChild, new CloseMdiChildEventArgs(this));
        }

        /// <summary>
        /// Enables or disable controls.
        /// </summary>
        /// <typeparam name="T">type of control</typeparam>
        /// <param name="isEnable">if set to <c>true</c> [is enable].</param>
        private void EnableDisableControls<TX>(bool isEnable) where TX : Control
        {
            Controls.All()
                .OfType<TX>()
                .ToList()
                .ForEach(ctl => ctl.Enabled = isEnable);
        }

        /// <summary>
        /// Visibles or invisible all controls.
        /// </summary>
        /// <param name="isVisible">if set to <c>true</c> [is visible].</param>
        private void VisibleInvisibleAllControls(bool isVisible)
        {
            Controls.All()
                .OfType<Control>()
                .ToList()
                .ForEach(ctl => ctl.Visible = isVisible);
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Gets a value indicating whether this instance is in design mode.
        /// </summary>
        /// <value>
        ///     <c>true</c> if this instance is in design mode; otherwise, <c>false</c>.
        /// </value>
        public bool IsInDesignMode
        {
            get { return (Process.GetCurrentProcess().ProcessName == "devenv"); }
        }

        /// <summary>
        /// Fires the event broker.
        /// </summary>
        /// <param name="eventTopic">The event topic.</param>
        /// <param name="eventArgs">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        public void FireEventBroker(string eventTopic, EventArgs eventArgs)
        {
            eventBroker.Fire(eventTopic, this, eventArgs);
        }

        /// <summary>
        /// Clears all bindings.
        /// </summary>
        public void ClearAllBindings()
        {
            Controls.All().OfType<Control>().ToList().ForEach(
                ctl => { if (ctl.DataBindings.Count != 0) ctl.DataBindings.Clear(); });
        }

        /// <summary>
        /// Enables all controls.
        /// </summary>
        public void EnableAllControls()
        {
            EnableDisableAllControls(true);
        }

        /// <summary>
        /// Disables all controls.
        /// </summary>
        public void DisableAllControls()
        {
            EnableDisableAllControls(false);
        }

        /// <summary>
        /// Enables or disable all controls.
        /// </summary>
        /// <param name="isEnable">if set to <c>true</c> [is enable].</param>
        private void EnableDisableAllControls(bool isEnable)
        {
            Controls.All()
                .OfType<Control>()
                .ToList()
                .ForEach(ctl => ctl.Enabled = isEnable);
        }

        /// <summary>
        /// Enables all buttons.
        /// </summary>
        public void EnableAllButtons()
        {
            EnableDisableControls<Button>(true);
        }

        /// <summary>
        /// Disables all buttons.
        /// </summary>
        public void DisableAllButtons()
        {
            EnableDisableControls<Button>(false);
        }

        /// <summary>
        /// Enables all text boxes.
        /// </summary>
        public void EnableAllTextBoxes()
        {
            EnableDisableControls<TextBox>(true);
        }

        /// <summary>
        /// Disables all text boxes.
        /// </summary>
        public void DisableAllTextBoxes()
        {
            EnableDisableControls<TextBox>(false);
        }

        /// <summary>
        /// Enables the select controls.
        /// </summary>
        /// <param name="controlNames">The control names.</param>
        public void EnableSelectControls(params string[] controlNames)
        {
            EnableDisableSelectedControls(true, controlNames);
        }

        /// <summary>
        /// Disables the select controls.
        /// </summary>
        /// <param name="controlNames">The control names.</param>
        public void DisableSelectControls(params string[] controlNames)
        {
            EnableDisableSelectedControls(false, controlNames);
        }

        /// <summary>
        /// Enables or disable selected controls.
        /// </summary>
        /// <param name="isEnable">if set to <c>true</c> [is enable].</param>
        /// <param name="controlNames">The control names.</param>
        public void EnableDisableSelectedControls(bool isEnable, params string[] controlNames)
        {
            Controls.All()
                .OfType<Control>()
                .ToList()
                .ForEach(ctl => { if (controlNames.Contains(ctl.Name)) ctl.Enabled = isEnable; });
        }

        /// <summary>
        /// Visibles the select controls.
        /// </summary>
        /// <param name="controlNames">The control names.</param>
        public void VisibleSelectControls(params string[] controlNames)
        {
            VisibleInvisibleSelectedControls(true, controlNames);
        }

        /// <summary>
        /// Invisibles the select controls.
        /// </summary>
        /// <param name="controlNames">The control names.</param>
        public void InvisibleSelectControls(params string[] controlNames)
        {
            VisibleInvisibleSelectedControls(false, controlNames);
        }

        /// <summary>
        /// Visibles or invisible selected controls.
        /// </summary>
        /// <param name="isVisible">if set to <c>true</c> [is visible].</param>
        /// <param name="controlNames">The control names.</param>
        private void VisibleInvisibleSelectedControls(bool isVisible, params string[] controlNames)
        {
            Controls.All()
                .OfType<Control>()
                .ToList()
                .ForEach(ctl => { if (controlNames.Contains(ctl.Name)) ctl.Visible = isVisible; });
        }

        /// <summary>
        /// Visibles all controls.
        /// </summary>
        public void VisibleAllControls()
        {
            VisibleInvisibleAllControls(true);
        }

        /// <summary>
        /// Invisibles all controls.
        /// </summary>
        public void InvisibleAllControls()
        {
            VisibleInvisibleAllControls(false);
        }

        #endregion
    }
}