[C#/WPF] TextBox 클래스 : GetLineIndexFromCharacterIndex/GetCharacterIndexFromLineIndex 메소드를 사용해 행/열 인덱스 구하기
C#/WPF 2020. 8. 19. 15:03728x90
반응형
728x170
■ TextBox 클래스의 GetLineIndexFromCharacterIndex/GetCharacterIndexFromLineIndex 메소드를 사용해 행/열 인덱스를 구하는 방법을 보여준다.
▶ 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
반응형
그리드형(광고전용)
'C# > WPF' 카테고리의 다른 글
[C#/WPF] Frame 엘리먼트 : NavigationUIVisibility 속성 사용하기 (0) | 2020.08.20 |
---|---|
[C#/WPF] x:Code 엘리먼트 : XAML 파일에서 코드 내장하기 (0) | 2020.08.20 |
[C#/WPF] XAML 문자열에서 객체 생성하기 (0) | 2020.08.20 |
[C#/WPF] KeyGesture 클래스 : 단축 키 사용하기 (0) | 2020.08.19 |
[C#/WPF] DocumentPaginator 클래스 : 일반 텍스트 문자열 출력하기 (0) | 2020.08.19 |
[C#/WPF] FontStretchConverter 클래스 : ConvertToString/ConvertFromString 메소드 사용하기 (0) | 2020.08.19 |
[C#/WPF] FontWeightConverter 클래스 : ConvertToString/ConvertFromString 메소드 사용하기 (0) | 2020.08.19 |
[C#/WPF] 한글 폰트명 리스트 구하기 (0) | 2020.08.19 |
[C#/WPF] DocumentPaginator 클래스 : 배너 문자 출력하기 (0) | 2020.08.18 |
[C#/WPF] PrintDialog 클래스 : PrintVisual 메소드를 사용해 그리드 인쇄하기 (0) | 2020.08.18 |
댓글을 달아 주세요