Click here to Skip to main content
15,885,767 members
Articles / Web Development / HTML

Windows and Web Generic Components

Rate me:
Please Sign up or sign in to vote.
4.08/5 (7 votes)
23 Jul 2008CPOL4 min read 27.3K   742   15  
A method to create Windows and Web components with the same interface
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections.Generic;
using System.Collections;
using System.Data;
using Framework.baInterfaces;
using Framework.UI.baController;
using Framework.Lib;

namespace Framework.UI.baWebControls
{
    /// <summary>
    /// Summary description for baComboBox.
    /// </summary>
    [DefaultProperty("ListItems"),
       ToolboxData("<{0}:baComboBox runat=server></{0}:baComboBox>")]
    public class baComboBox : System.Web.UI.WebControls.DropDownList, IComboBox, IState
    {
        private baWebListCollection listCollection;
        private IBindingManager oBindingManager;
        private TransactionStateCtrl stateMachineCtrl = null;
        private ComponentUsage oBehaviour = ComponentUsage.None;

        #region Constructor
        public baComboBox()
            : base()
        {
            listCollection = new baWebListCollection(this.Items);
        }
        #endregion 

        #region ListItems
        public baListCollection ListItems
        {
            get
            {
                return listCollection; 
            }
        }
        #endregion

        #region Focus
        public new bool Focus()
        {
            try
            {
                base.Focus();
                return true;
            }
            catch
            {
                return false;
            }
        }
        #endregion

        #region TextField
        public string TextField
        {
            get
            {
                return this.DataTextField;
            }
            set
            {
                this.DataTextField = value;
            }
        }
        #endregion

        #region ValueField
        public string ValueField
        {
            get
            {
                return this.DataValueField;
            }
            set
            {
                this.DataValueField = value;
            }
        }
        #endregion

        #region BindingManager
        public IBindingManager BindingManager
        {
            get
            {
                return oBindingManager;
            }
            set
            {
                try
                {
                    oBindingManager = value;
                    if (value == null)
                        base.DataSource = null;
                    else
                    {
                        base.DataSource = value.DataSource;
                        oBindingManager.TransactionStateChanged += new TransactionStateChangeEventHandler(oBindingManager_TransactionStateChanged);
                    }
                }
                catch (Exception baExp)
                {
                    throw new baException(baExp, System.Reflection.MethodBase.GetCurrentMethod());
                }
            }
        }
        #endregion

        #region oBindingManager_TransactionStateChanged(object sender, TransactionStateChangeArgs Was, TransactionStateChangeArgs Will)
        void oBindingManager_TransactionStateChanged(object sender, TransactionStateChangeArgs Was, TransactionStateChangeArgs Will)
        {
            try
            {
                if (stateMachineCtrl != null)
                    stateMachineCtrl.SetTransactionStateState(Will.TransactionState);
            }
            catch (Exception baExp)
            {
                throw new baException(baExp, System.Reflection.MethodBase.GetCurrentMethod());
            }
        }
        #endregion
        
        #region ComponentUsage Behaviour
        /// <summary>
        /// If set to null, the component wouldn't be changed when
        /// the transaction state of its Binding Manager changed
        /// else it will be changed accortding to the state machine
        /// being set in this property
        /// </summary>
        public ComponentUsage Behaviour
        {
            get
            {
                return oBehaviour;
            }
            set
            {
                try
                {
                    if (value == ComponentUsage.None)
                        stateMachineCtrl = null;
                    else
                        stateMachineCtrl = TransactionStateCtrl.GetTransactionStateCtrl(this, value);
                    oBehaviour = value;
                }
                catch (Exception baExp)
                {
                    throw new baException(baExp, System.Reflection.MethodBase.GetCurrentMethod());
                }
            }
        }
        #endregion

        #region string SelectedValueVS
        private string SelectedValueVS
        {
            get
            {
                if (this.ViewState["SelectedValueVS"] == null)
                    return null;
                else
                    return this.ViewState["SelectedValueVS"].ToString();
            }
            set
            {
                ViewState["SelectedValueVS"] = value;
            }
        }
        #endregion

        #region OnSelectedIndexChanged(EventArgs e)
        protected override void OnSelectedIndexChanged(EventArgs e)
        {
            try
            {
                if (BindingManager != null)
                {
                    BindingManager.Find(this.SelectedValue);
                    //SelectedValueVS = this.SelectedValue;
                }
            }
            catch (Exception baExp)
            {
                throw new baException(baExp, System.Reflection.MethodBase.GetCurrentMethod());
            }
            base.OnSelectedIndexChanged(e);
        }
        #endregion 

        #region DataBind()
        /// <summary>
        /// Control DataBind method being called when 
        /// Container Form DataBind being called
        /// </summary>
        public override void DataBind()
        {
            base.DataBind();
            try
            {
                /// Change the transaction state of this 
                /// contol to its Binding Manager State            
                if (BindingManager != null)
                {
                    this.TransactionState = BindingManager.TransactionState;
                }
            }
            catch (Exception baExp)
            {
                throw new baException(baExp, System.Reflection.MethodBase.GetCurrentMethod());
            }
        }
        #endregion

        #region Location
        [Bindable(true),
        Category("Appearance"),
        DefaultValue(""),
        Description("In order to set the location:\r\n" +
        " - Right click on control and select Style...r\n" +
        " - In Style Builder dialog box, go to Position and set Position mode to Absolutely reposition")]
        public System.Drawing.Point Location
        {
            get
            {
                try
                {
                    return new System.Drawing.Point(int.Parse(Style["LEFT"].Replace("px", "")),
                                                        int.Parse(Style["TOP"].Replace("px", "")));
                }
                catch 
                {
                    throw new Exception("Control should be in absolute positioning style");
                }
            }
            set
            {
                try
                {
                    Style["LEFT"] = value.X.ToString();
                    Style["TOP"] = value.Y.ToString();
                }
                catch (Exception baExp)
                {
                    throw new baException(baExp, System.Reflection.MethodBase.GetCurrentMethod());
                }
            }
        }
        #endregion

        #region ITransactionState Members
        public TransactionStateKind TransactionState
        {
            get
            {
                return (TransactionStateKind)ViewState["oTransactionState"];
            }
            set
            {
                try
                {
                    if (BindingManager != null)
                    {
                        switch (BindingManager.TransactionState)
                        {
                            case TransactionStateKind.Add:
                                {
                                    /// If This control state should be in Add state then put 
                                    /// a null in the list and make it the selected item
                                    this.Items.Add(new ListItem("", "-1"));
                                    this.SelectedValue = "-1";
                                    break;
                                }
                            case TransactionStateKind.Edit:
                            case TransactionStateKind.Delete:
                            case TransactionStateKind.Null:
                            case TransactionStateKind.EditOnly:
                            case TransactionStateKind.AddEditOnly:
                            case TransactionStateKind.SearchOnly:
                            case TransactionStateKind.Initial:
                            case TransactionStateKind.EmptyDataSource:
                            case TransactionStateKind.Confirm:
                                {
                                    if ((BindingManager[ValueField] != null) &&
                                        (this.Items.FindByValue(BindingManager[ValueField].ToString()) != null))
                                    {
                                        /// If the control state goes back to initial state then 
                                        /// move to previously selected item in the list
                                        this.SelectedValue = BindingManager[ValueField].ToString();
                                    }
                                    break;
                                }
                        }
                    }
                    ViewState["oTransactionState"] = value;
                }
                catch (Exception baExp)
                {
                    throw new baException(baExp, System.Reflection.MethodBase.GetCurrentMethod());
                }
            }
        }
        #endregion

        #region System.Windows.Forms.Cursor Cursor
        [Bindable(true),
        Category("Appearance"),
        DefaultValue("")]
        public System.Windows.Forms.Cursor Cursor
        {
            get
            {
                switch (Style["cursor"])
                {
                    case "hand":
                        {
                            return System.Windows.Forms.Cursors.Hand;
                        }
                    default:
                        {
                            return System.Windows.Forms.Cursors.Default;
                        }
                }
            }
            set
            {
                try
                {
                    if (value == System.Windows.Forms.Cursors.Hand)
                    {
                        Style["cursor"] = "hand";
                    }
                    if (value == System.Windows.Forms.Cursors.Default)
                    {
                        Style.Remove("cursor");
                    }
                }
                catch (Exception baExp)
                {
                    throw new baException(baExp, System.Reflection.MethodBase.GetCurrentMethod());
                }
            }
        }
        #endregion

        #region bool Enabled
        public override bool Enabled
        {
            get
            {
                try
                {
                    if (base.Enabled)
                        this.Style.Remove("background-color");
                    else
                        this.Style.Add("background-color", "buttonface");
                    return base.Enabled;
                }
                catch (Exception baExp)
                {
                    throw new baException(baExp, System.Reflection.MethodBase.GetCurrentMethod());
                }
            }
            set
            {
                base.Enabled = value;
            }
        }
        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Canada Canada
Software development experience since 1993

Skills
- Programming Languages: C# .NET, Delphi, C++, Java and VB
- Development Methodologies (SDLC): RUP, Scrum and XP
- Database: SQL server, Oracle, Apollo database and MS Access.

Educations & Certificates
- Microsoft® Certified Professional (MCP)
- Sun® Certified Java2 Programmer
- B.S. in computer science

Comments and Discussions