첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ LookupEdit 클래스 : 콤보 박스 모드 사용하기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


MainForm.cs

 

 

using System;

using System.Collections.Generic;

using System.Windows.Forms;

 

using DevExpress.Utils;

using DevExpress.XtraEditors;

using DevExpress.XtraEditors.Controls;

 

namespace TestProject

{

    /// <summary>

    /// 메인 폼

    /// </summary>

    public partial class MainForm : XtraForm

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// 레이블 컨트롤 딕셔너리

        /// </summary>

        private Dictionary<LookUpEditBase, LabelControl> labelControlDictionary;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 레이블 컨트롤 딕셔너리 - LabelControlDictionary

 

        /// <summary>

        /// 레이블 컨트롤 딕셔너리

        /// </summary>

        private Dictionary<LookUpEditBase, LabelControl> LabelControlDictionary

        {

            get

            {

                if(this.labelControlDictionary == null)

                {

                    this.labelControlDictionary = new Dictionary<LookUpEditBase, LabelControl>();

 

                    this.labelControlDictionary.Add(lookUpEdit    , lookupEditValueLabel);

                    this.labelControlDictionary.Add(gridLookUpEdit, gridLookupEditValueLabel);

                }

 

                return this.labelControlDictionary;

            }

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainForm()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainForm()

        {

            InitializeComponent();

 

            Load += Form_Load;

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region 폼 로드시 처리하기 - Form_Load(sender, e)

 

        /// <summary>

        /// 폼 로드시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void Form_Load(object sender, EventArgs e)

        {

            #region 룩업 에디터를 설정한다.

 

            string[] colorArray = new string[]

            {

                "Yellow",

                "Red",

                "Green",

                "Black",

                "White"

            };

 

            this.lookUpEdit.Properties.TextEditStyle              = TextEditStyles.Standard;

            this.lookUpEdit.Properties.AcceptEditorTextAsNewValue = DefaultBoolean.Default;

            this.lookUpEdit.Properties.NullText                   = "(select or type value)";

            this.lookUpEdit.Properties.DataSource                 = colorArray;

 

            #endregion

            #region 그리드 룩업 에디터를 설정한다.

 

            List<Product> productList = new List<Product>

            {

                new Product() { ProductName = "Chang"            },

                new Product() { ProductName = "Ipoh Coffee"      },

                new Product() { ProductName = "Ravioli Angelo"   },

                new Product() { ProductName = "Filo Mix"         },

                new Product() { ProductName = "Tunnbrod"         },

                new Product() { ProductName = "Konbu"            },

                new Product() { ProductName = "Boston Crab Meat" }

            };

 

            this.gridLookUpEdit.Properties.TextEditStyle              = TextEditStyles.Standard;

            this.gridLookUpEdit.Properties.AcceptEditorTextAsNewValue = DefaultBoolean.True;

            this.gridLookUpEdit.Properties.NullText                   = "(select or type value)";

            this.gridLookUpEdit.Properties.ValueMember                = "ProductName";

            this.gridLookUpEdit.Properties.DisplayMember              = this.gridLookUpEdit.Properties.ValueMember;

            this.gridLookUpEdit.Properties.DataSource                 = productList;

 

            #endregion

            #region 이벤트를 설정한다.

 

            this.lookUpEdit.EditValueChanged     += lookUpEdit_EditValueChanged;

            this.gridLookUpEdit.ProcessNewValue  += gridLookUpEdit_ProcessNewValue;

            this.gridLookUpEdit.EditValueChanged += lookUpEdit_EditValueChanged;

 

            #endregion

        }

 

        #endregion

        #region 룩업 에디터 편집 값 변경시 처리하기 - lookUpEdit_EditValueChanged(sender, e)

 

        /// <summary>

        /// 룩업 에디터 편집 값 변경시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void lookUpEdit_EditValueChanged(object sender, EventArgs e)

        {

            LookUpEditBase lookupEditBase = sender as LookUpEditBase;

 

            if(lookupEditBase == null)

            {

                return;

            }

 

            LabelControl labelControl = LabelControlDictionary[lookupEditBase];

 

            if(labelControl == null)

            {

                return;

            }

 

            if(lookupEditBase.EditValue == null)

            {

                labelControl.Text = "EditValue : null";

            }

            else

            {

                labelControl.Text = "EditValue : " + lookupEditBase.EditValue.ToString();

            }

        }

 

        #endregion

        #region 그리드 룩업 에디터 신규 값 처리하기 - gridLookUpEdit_ProcessNewValue(sender, e)

 

        /// <summary>

        /// 그리드 룩업 에디터 신규 값 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void gridLookUpEdit_ProcessNewValue(object sender, ProcessNewValueEventArgs e)

        {

            GridLookUpEdit gridLookUpEdit = sender as GridLookUpEdit;

 

            if(e.DisplayValue == null)

            {

                return;

            }

 

            string newValue = e.DisplayValue.ToString();

 

            if(newValue == string.Empty)

            {

                return;

            }

 

            if(MessageBox.Show(this, "'" + newValue + "' 항목을 목록에 추가하시겠습니까?",

                "CONFIRMATION", MessageBoxButtons.YesNo) == DialogResult.Yes)

            {

                List<Product> productList = gridLookUpEdit.Properties.DataSource as List<Product>;

 

                productList.Add(new Product { ProductName = newValue });

 

                e.Handled = true;

            }

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요