积极答复者
WPF RichTextBox中有类似GetFirstCharIndexOfCurrentLine()和GetLineFromCharIndex(index)的函数

问题
答案
-
你好,
你可以通过如下方法计算光标所在的行列值:
private void Button_Click(object sender, RoutedEventArgs e) { TextPointer tp1 = rtb.Selection.Start.GetLineStartPosition(0); TextPointer tp2 = rtb.Selection.Start; int column = tp1.GetOffsetToPosition(tp2); int someBigNumber = int.MaxValue; int lineMoved, currentLineNumber; rtb.Selection.Start.GetLineStartPosition(-someBigNumber, out lineMoved); currentLineNumber = -lineMoved; LineColumnLabel.Content = "Line: " + currentLineNumber.ToString() + " Column: " + column.ToString(); }
XAML:
<Grid> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition Height="30" /> <RowDefinition Height="30" /> </Grid.RowDefinitions> <RichTextBox x:Name="rtb" /> <Label Grid.Row="1" x:Name="LineColumnLabel" /> <Button Grid.Row="2" Content="Get Current Line and Column" Click="Button_Click" /> </Grid>
截图:
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- 已标记为答案 ClowBellows 2014年2月28日 15:07
全部回复
-
-
你好,
你可以通过如下方法计算光标所在的行列值:
private void Button_Click(object sender, RoutedEventArgs e) { TextPointer tp1 = rtb.Selection.Start.GetLineStartPosition(0); TextPointer tp2 = rtb.Selection.Start; int column = tp1.GetOffsetToPosition(tp2); int someBigNumber = int.MaxValue; int lineMoved, currentLineNumber; rtb.Selection.Start.GetLineStartPosition(-someBigNumber, out lineMoved); currentLineNumber = -lineMoved; LineColumnLabel.Content = "Line: " + currentLineNumber.ToString() + " Column: " + column.ToString(); }
XAML:
<Grid> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition Height="30" /> <RowDefinition Height="30" /> </Grid.RowDefinitions> <RichTextBox x:Name="rtb" /> <Label Grid.Row="1" x:Name="LineColumnLabel" /> <Button Grid.Row="2" Content="Get Current Line and Column" Click="Button_Click" /> </Grid>
截图:
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- 已标记为答案 ClowBellows 2014年2月28日 15:07
-