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