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

■ TextBox 클래스의 GetLineIndexFromCharacterIndex/GetCharacterIndexFromLineIndex 메소드를 사용해 행/열 인덱스를 구하는 방법을 보여준다.

TestProject.zip
다운로드

▶ 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 클래스 : GetLineIndexFromCharacterIndex/GetCharacterIndexFromLineIndex 메소드를 사용해 행/열 인덱스 구하기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Grid>
        <StackPanel
            HorizontalAlignment="Center"
            VerticalAlignment="Center">
            <TextBox Name="textBox"
                Margin="10"
                Width="300"
                Height="300"
                AcceptsReturn="True" />
            <TextBlock Name="textBlock"
                HorizontalAlignment="Center"
                Margin="10" />
        </StackPanel>
    </Grid>
</Window>

 

▶ MainWindow.xaml.cs

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

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

        #region 생성자 - MainWindow()

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

            this.textBox.SelectionChanged += textBox_SelectionChanged;
        }

        #endregion

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

        #region 텍스트 박스 선택 변경시 처리하기 - textBox_SelectionChanged(sender, e)

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

            int characterIndex = textBox.SelectionStart;
            int lineIndex      = textBox.GetLineIndexFromCharacterIndex(characterIndex);

            if(lineIndex == -1)
            {
                this.textBlock.Text = string.Empty;

                return;
            }

            int columnIndex = characterIndex - textBox.GetCharacterIndexFromLineIndex(lineIndex);

            string location  = string.Format("행 {0} 열 {1}", lineIndex + 1, columnIndex + 1);

            if(textBox.SelectionLength > 0)
            {
                characterIndex += textBox.SelectionLength;

                lineIndex = textBox.GetLineIndexFromCharacterIndex(characterIndex);

                columnIndex = characterIndex - textBox.GetCharacterIndexFromLineIndex(lineIndex);

                location += string.Format(" - 행 {0} 열 {1}", lineIndex + 1, columnIndex + 1);
            }

            this.textBlock.Text = location;
        }

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

댓글을 달아 주세요