none
WPF RichTextBox中有类似GetFirstCharIndexOfCurrentLine()和GetLineFromCharIndex(index)的函数 RRS feed

  • 问题

  • 我想得到RichTextBox的光标所在行和列,在WinForm中可以调用这2个函数,但是在WPF的RichTextBox中好像没有这2个函数
    2014年2月27日 11:31

答案

  • 你好,

    你可以通过如下方法计算光标所在的行列值:

    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
    2014年2月28日 12:24
    版主

全部回复

  • 这我也看过,但好像没有找到解决的办法
    2014年2月28日 11:42
  • 你好,

    你可以通过如下方法计算光标所在的行列值:

    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
    2014年2月28日 12:24
    版主
  • 太感谢 了! 我去研究下
    2014年2月28日 15:02