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

■ TextBox 클래스의 PreviewTextInput 이벤트를 사용해 3자리마다 콤마를 갖는 숫자를 입력하는 방법을 보여준다.

TestProject.zip
다운로드

▶ TextBoxExtension.cs

using System.Linq;
using System.Windows.Controls;

namespace TestProject
{
    /// <summary>
    /// 텍스트 박스 확장
    /// </summary>
    public static class TextBoxExtension
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// public

        #region 숫자 카운트 구하기 - GetDigitCount(textBox)

        /// <summary>
        /// 숫자 카운트 구하기
        /// </summary>
        /// <param name="textBox">텍스트 박스</param>
        /// <returns>숫자 카운트</returns>
        public static int GetDigitCount(this TextBox textBox) => textBox.Text.Count(char.IsDigit);

        #endregion
        #region 콤마 여부 구하기 - HasAnyCommas(textBox)

        /// <summary>
        /// 콤마 여부 구하기
        /// </summary>
        /// <param name="textBox">텍스트 박스</param>
        /// <returns>콤마 여부</returns>
        public static bool HasAnyCommas(this TextBox textBox) => textBox.Text.Any(x => x == ',');

        #endregion
    }
}

 

▶ MainWindow.xaml

<Window x:Class="TestProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="800"
    Height="600"
    Title="TextBox 클래스 : PreviewTextInput 이벤트를 사용해 3자리마다 콤마를 갖는 숫자 입력하기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Grid>
        <TextBox Name="textBox"
            Width="200"
            Height="25"
            HorizontalContentAlignment="Right"
            VerticalContentAlignment="Center"
            MaxLength="20"
            Text="{Binding Value, UpdateSourceTrigger=PropertyChanged, StringFormat='{}{0:#,0}'}" />
    </Grid>
</Window>

 

▶ MainWindow.xaml.cs

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace TestProject
{
    /// <summary>
    /// 메인 윈도우
    /// </summary>
    public partial class MainWindow : Window
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 값 - Value

        /// <summary>
        /// 값
        /// </summary>
        public double Value { get; set; }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainWindow()

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

            DataContext = this;

            this.textBox.PreviewTextInput += textBox_PreviewTextInput;
        }

        #endregion

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

        #region 텍스트 박스 프리뷰 텍스트 입력시 처리하기 - textBox_PreviewTextInput(sender, e)

        /// <summary>
        /// 텍스트 박스 프리뷰 텍스트 입력시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void textBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            TextBox textBox = sender as TextBox;

            string text = textBox.Text;

            if(text.Length + 1 < 3)
            {
                return;
            }

            if((textBox.GetDigitCount()) % 3 == 0 && text.Length != 2)
            {
                if(!textBox.HasAnyCommas())
                {
                    textBox.Text = text.Insert(1, ",");
                }
                else
                {
                    textBox.Text = textBox.Text.Insert(textBox.Text.Length - 2, ",");
                }
            }

            textBox.SelectionStart = textBox.Text.Length;
        }

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

댓글을 달아 주세요