Common C#/C# GDI+ Study2008. 6. 9. 16:16

        // Event Method
        #region OnKeyUp
        /// <summary>
        ///
        /// </summary>
        /// <param name="e"></param>
        protected override void OnKeyUp(System.Windows.Forms.KeyEventArgs e)
        {
            // Label 형태의 스킨일 경우 입력 형식을 검사하지 않는다.
            if (!this.labelEnabled)
            {
                int startIndex = this.SelectionStart;
                if (startIndex > 0)
                {
                    string pattern = null;

                    // 정규식에 대한 패턴을 설정한다. 오류 문자열에 대한 패턴을 작성해야 된다.
                    switch (this.inputFormat)
                    {
                        case InputFormat.EnglishOnly:
                            if (this.spaceKeyEnabled)
                                pattern = @"[^ a-zA-Z0-9_-]+";
                            else
                                pattern = @"[^a-zA-Z0-9_-]+";
                            break;

                        case InputFormat.KoreanOnly:
                            if (this.spaceKeyEnabled)
                                pattern = @"[^ ㄱ-힣0-9_-]+";
                            else
                                pattern = @"[^ㄱ-힣0-9_-]+";
                            break;

                        case InputFormat.All:
                            if (!this.spaceKeyEnabled)
                                pattern = @"[ ]";
                            break;
                    }

                    if (pattern != null && Regex.IsMatch(this.Text, pattern))
                    {
                        this.Text = this.Text.Substring(0, startIndex - 1) + this.Text.Substring(startIndex);
                        this.Select(startIndex - 1, 0);
                    }
                }
            }

            base.OnKeyUp(e);
        }
        #endregion
        #region OnLeave
        /// <summary>
        ///
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLeave(EventArgs e)
        {
            if (this.trimEnabled) this.Text = this.Text.Trim();

            // 문자열 수 검사
            if (this.MaxStringLength > -1 && this.MaxStringLength < System.Text.Encoding.Default.GetBytes(this.Text).Length)
            {
                Core.Forms.MessageViewer.ShowDialog("항목이 " + this.MaxStringLength + " Byte를 초과하였습니다(영문 1 Byte, 한글 2 Byte).", System.Windows.Forms.MessageBoxIcon.Warning);

                this.Focus();
            }

            base.OnLeave(e);
        }
        #endregion

Posted by penguindori
Common C#/C# GDI+ Study2008. 6. 9. 16:16

using System;
using System.ComponentModel;
using System.Text.RegularExpressions;

namespace ICS.Core.Controls
{
    /// <summary>
    /// Edit Box
    /// </summary>
    public partial class EditBox : System.Windows.Forms.TextBox, IControlBase
    {
        #region Constructor
        /// <summary>
        /// 기본 생성자입니다.
        /// </summary>
        public EditBox()
        {
            InitializeComponent();
        }
        #endregion

        // User Property
        #region Mandatory
        bool mandatory = false;
        /// <summary>
        /// 필수 입력 항목인지를 가져오거나 설정한다.
        /// </summary>
        [Category("ETC"), DefaultValue(false), Description("필수 입력 여부를 가져오거나 설정한다.")]
        public bool Mandatory
        {
            get { return mandatory; }
            set
            {
                this.mandatory = value;

                if (value)
                {
                    this.BackColor = System.Drawing.Color.Cornsilk;
                }
                else
                {
                    if (this.labelEnabled) this.BackColor = System.Drawing.Color.WhiteSmoke;
                    else this.BackColor = System.Drawing.Color.White;
                }
            }
        }
        #endregion
        #region LabelEnabled
        bool labelEnabled = false;
        /// <summary>
        /// Label과 같이 수정할 수 없는 컨트롤로의 설정 여부를 나타낸다.
        /// </summary>
        [Category("ETC"), DefaultValue(false), Description("Label과 같이 수정할 수 없는 컨트롤로의 설정 여부를 나타낸다.")]
        public bool LabelEnabled
        {
            get { return this.labelEnabled; }
            set
            {
                this.labelEnabled = value;

                if (value)
                {
                    this.ReadOnly = true;
                    if (this.mandatory) this.BackColor = System.Drawing.Color.Cornsilk;
                    else this.BackColor = System.Drawing.Color.WhiteSmoke;
                    this.ForeColor = System.Drawing.Color.FromArgb(100, 100, 100);
                    this.TabStop = false;
                }
                else
                {
                    this.ReadOnly = false;
                    if (this.mandatory) this.BackColor = System.Drawing.Color.Cornsilk;
                    else this.BackColor = System.Drawing.Color.White;
                    this.ForeColor = System.Drawing.Color.Black;
                    this.TabStop = true;
                }
            }
        }
        #endregion
        #region TrimEnabled
        bool trimEnabled = true;
        /// <summary>
        /// Trim 기능을 사용할지 여부를 가져오거나 설정한다.
        /// </summary>
        [Category("ETC"), DefaultValue(true), Description("Trim 기능을 사용할지 여부를 가져오거나 설정한다.")]
        public bool TrimEnabled
        {
            get { return trimEnabled; }
            set { trimEnabled = value; }
        }
        #endregion
        #region MaxStringLength
        int maxStringLength = 40;
        /// <summary>
        /// 입력할 수 있는 문자열의 최대 길이를 설정한다. 영문은 1Byte, 한글은 2Byte로 계산된다.
        /// -1로 설정하면 문자열 길이를 검사하지 않는다는 의미이다.
        /// </summary>
        [Category("ETC"), DefaultValue(40), Description("입력할 수 있는 문자열의 최대 길이를 설정한다. 영문은 1Byte, 한글은 2Byte로 계산된다. -1로 설정하면 문자열 길이를 검사하지 않는다는 의미이다.")]
        public int MaxStringLength
        {
            get { return maxStringLength; }
            set { maxStringLength = value; }
        }
        #endregion
        #region InputFormat
        InputFormat inputFormat = InputFormat.All;
        /// <summary>
        /// 입력 형식을 가져오거나 설정한다. 이 설정값에 따라 IME 모드가 변경된다.
        /// </summary>
        [Category("ETC"), DefaultValue(InputFormat.All), Description("입력 형식을 가져오거나 설정한다. 이 설정값에 따라 IME 모드가 변경된다.")]
        public InputFormat InputFormat
        {
            get { return inputFormat; }
            set
            {
                inputFormat = value;

                switch (value)
                {
                    case InputFormat.EnglishOnly:
                        this.ImeMode = System.Windows.Forms.ImeMode.Alpha;
                        break;

                    case InputFormat.KoreanOnly:
                        this.ImeMode = System.Windows.Forms.ImeMode.Hangul;
                        break;
                }
            }
        }
        #endregion
        #region SpaceKeyEnabled
        bool spaceKeyEnabled = false;
        /// <summary>
        /// 공백(' ')키를 사용할 수 있는지 여부를 가져오거나 설정한다.
        /// </summary>
        [Category("ETC"), DefaultValue(false), Description("공백(' ')키를 사용할 수 있는지 여부를 가져오거나 설정한다.")]
        public bool SpaceKeyEnabled
        {
            get { return spaceKeyEnabled; }
            set { spaceKeyEnabled = value; }
        }
        #endregion
<-----추가 부분----->
   }
}

Posted by penguindori