첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
다운로드

▶ TextFilter.cs

using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace TestProject
{
    /// <summary>
    /// 텍스트 박스 필터
    /// </summary>
    public static class TextBoxFilter
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region Field

        /// <summary>
        /// 정수 패턴
        /// </summary>
        public static string IntegerPattern = "(?<Number>[0-9])";

        /// <summary>
        /// 십진수 패턴
        /// </summary>
        public static string DecimalPattern = @"(?<Number>^[0-9]*\.?[0-9]*)";

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region 숫자 구하기 - GetNumber(regexPattern, sourceText, isPositiveOnly)

        /// <summary>
        /// 숫자 구하기
        /// </summary>
        /// <param name="regexPattern">정규식 패턴</param>
        /// <param name="sourceText">소스 텍스트</param>
        /// <param name="isPositiveOnly">양수만 여부</param>
        /// <returns>숫자 문자열</returns>
        public static string GetNumber(string regexPattern, string sourceText, bool isPositiveOnly = false)
        {
            string newNumber = string.Empty;

            if(!isPositiveOnly)
            {
                if(sourceText.StartsWith("-"))
                {
                    newNumber += "-";
                }
            }

            sourceText = sourceText.Replace("-", string.Empty);

            Regex regex = new Regex(regexPattern);

            Match match = regex.Match(sourceText);

            while(match.Success)
            {
                newNumber += match.Groups["Number"].Value;

                match = match.NextMatch();
            }

            return newNumber;
        }

        #endregion
        #region 텍스트 설정하기 - SetText(textBox, filter)

        /// <summary>
        /// 텍스트 설정하기
        /// </summary>
        /// <param name="textBox">텍스트 박스</param>
        /// <param name="filter">필터</param>
        public static void SetText(TextBox textBox, string filter)
        {
            int    currentPosition = textBox.SelectionStart;
            string currentText     = textBox.Text;

            if(filter.Length < currentText.Length)
            {
                currentPosition--;

                currentText = currentText.Remove(currentPosition, 1);

                textBox.Text = currentText;
            }
            else
            {
                textBox.Text = filter;
            }

            if(currentPosition >= textBox.Text.Length)
            {
                textBox.SelectionStart = textBox.Text.Length;
            }
            else
            {
                textBox.SelectionStart = currentPosition;
            }
        }

        #endregion
    }
}

 

728x90

 

▶ MainForm.cs

using System;
using System.Windows.Forms;

namespace TestProject
{
    /// <summary>
    /// 메인 폼
    /// </summary>
    public partial class MainForm : Form
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainForm()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            this.integerTextBox.TextChanged += integerTextBox_TextChanged;
            this.decimalTextBox.TextChanged += decimalTextBox_TextChanged;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 정수 텍스트 박스 텍스트 변경시 처리하기 - integerTextBox_TextChanged(sender, e)

        /// <summary>
        /// 정수 텍스트 박스 텍스트 변경시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void integerTextBox_TextChanged(object sender, EventArgs e)
        {
            TextBox textBox = sender as TextBox;

            if(textBox.Text != string.Empty)
            {
                string number = TextBoxFilter.GetNumber(TextBoxFilter.IntegerPattern, textBox.Text, false);

                TextBoxFilter.SetText(textBox, number);
            }
        }

        #endregion
        #region 십진수 텍스트 박스 텍스트 변경시 처리하기 - decimalTextBox_TextChanged(sender, e)

        /// <summary>
        /// 십진수 텍스트 박스 텍스트 변경시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void decimalTextBox_TextChanged(object sender, EventArgs e)
        {
            TextBox textBox = sender as TextBox;

            if(textBox.Text != string.Empty)
            {
                string number = TextBoxFilter.GetNumber(TextBoxFilter.DecimalPattern, textBox.Text, false);

                TextBoxFilter.SetText(textBox, number);
            }
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요