none
如何用RichTextBox实现Word输入一定内容自动生成新页的功能 RRS feed

  • 问题

  • RichTextBox编辑框默认为可以无限输入,如何限定RichTextBox的的最大输入内容(居然有涉及到了字体),最后在捕捉这个事件,在旧的RichTextBox下方再生成一个新的RichTextBoX ?
    2014年2月26日 2:38

答案

  • 你好,

    我们可以用handle RichTextBox的TextChanged 事件,计算文本长度,如果超出指定长度,删除即可。

    因为你希望添加新的 RichTextBox,所以 Demo 中我预留了一行:

    <Grid x:Name="gd">
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>
            <RichTextBox TextChanged="RichTextBox_TextChanged" />
        </Grid>

    后台代码:

    private void RichTextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
                RichTextBox rtb = (RichTextBox)sender;
                TextRange textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
                var text = textRange.Text.Trim();
    
                if (text.Length > 10) //RichTextBox Maximum Length
                {
                    int gap = 0;
                    while (rtb.CaretPosition.DeleteTextInRun(-1) == 0)
                    {
                        rtb.CaretPosition = rtb.CaretPosition.GetPositionAtOffset(--gap);
                    }
    
                    newrtb = new RichTextBox();
                    newrtb.Background = Brushes.LightBlue;
                    Grid.SetRow(newrtb, 1);
                    gd.Children.Add(newrtb);
    
                    dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
                    dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
                    dispatcherTimer.Interval = new TimeSpan(0,0, 0, 0, 100);
                    dispatcherTimer.Start();
                }
    }
    
    System.Windows.Threading.DispatcherTimer dispatcherTimer;
    RichTextBox newrtb;
    
    private void dispatcherTimer_Tick(object sender, EventArgs e)
    {
                dispatcherTimer.Stop();
                Keyboard.Focus(newrtb);
    }

    当输入超过10个字符后,第二行会添加一个新的RichTextBox, 输入焦点也会自动转移到此控件上:


    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月27日 12:05
    2014年2月27日 2:46
    版主

全部回复

  • 你好,

    我们可以用handle RichTextBox的TextChanged 事件,计算文本长度,如果超出指定长度,删除即可。

    因为你希望添加新的 RichTextBox,所以 Demo 中我预留了一行:

    <Grid x:Name="gd">
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>
            <RichTextBox TextChanged="RichTextBox_TextChanged" />
        </Grid>

    后台代码:

    private void RichTextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
                RichTextBox rtb = (RichTextBox)sender;
                TextRange textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
                var text = textRange.Text.Trim();
    
                if (text.Length > 10) //RichTextBox Maximum Length
                {
                    int gap = 0;
                    while (rtb.CaretPosition.DeleteTextInRun(-1) == 0)
                    {
                        rtb.CaretPosition = rtb.CaretPosition.GetPositionAtOffset(--gap);
                    }
    
                    newrtb = new RichTextBox();
                    newrtb.Background = Brushes.LightBlue;
                    Grid.SetRow(newrtb, 1);
                    gd.Children.Add(newrtb);
    
                    dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
                    dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
                    dispatcherTimer.Interval = new TimeSpan(0,0, 0, 0, 100);
                    dispatcherTimer.Start();
                }
    }
    
    System.Windows.Threading.DispatcherTimer dispatcherTimer;
    RichTextBox newrtb;
    
    private void dispatcherTimer_Tick(object sender, EventArgs e)
    {
                dispatcherTimer.Stop();
                Keyboard.Focus(newrtb);
    }

    当输入超过10个字符后,第二行会添加一个新的RichTextBox, 输入焦点也会自动转移到此控件上:


    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月27日 12:05
    2014年2月27日 2:46
    版主
  • 非常感谢
    2014年2月27日 12:05