WPF RichTextBox scroll bar auto-scroll instance, text auto-scroll instance

Source: Internet
Author: User

Description
1. Background code add test data

2. Using the Richtextbox.scrolltoverticaloffset () method, scroll the position of the scroll bar vertically

3. Use the timer DispatcherTimer to modify the page display data

4. Calculate your own processing, the height of the position has been scrolled

XAML Code:

<Grid>    <grid.columndefinitions>        <ColumnDefinitionWidth= "205*"/>        <ColumnDefinitionWidth= "87*"/>    </grid.columndefinitions>    <Buttonx:name= "button"Content= "Start Playback"HorizontalAlignment= "Left"Margin= "2,36,0,0"VerticalAlignment= "Top"Width= " the"Grid.column= "1"Height= "$"Click= "Button_Click"/>    <RichTextBoxx:name= "RichTextBox"HorizontalAlignment= "Left"Height= "209"Margin= "10,36,0,0"VerticalAlignment= "Top"Width= "The ">    </RichTextBox></Grid>

Add test data code in the background:

 PublicText4 () {InitializeComponent (); Richtextbox.document=Doc; Richtextbox.fontsize= -; //Add ContentAppendline (NULL,"from your world passing by"); Appendline (" One","The Moon shines on the sea"); Appendline (NULL,"from your world passing by"); Appendline (NULL,"Tianya at this time"); Appendline (" Both","Zhang Sanfeng"); Appendline (NULL,"from your world passing by"); Appendline (NULL,"Mr. Lu Xun"); Appendline (NULL,NULL);} FlowDocument Doc=NewFlowDocument ();Private voidAppendline (stringNamestringLine ) {Paragraph P=NewParagraph (); if(string. IsNullOrEmpty (name) = =false) Doc.    Registername (name, p); Run R=NewRun (line); P.textalignment=Textalignment.center;    P.inlines.add (R); Doc. Blocks.add (P);}

Timer Display control Code:

intPindex =0;DoubleCurtop =0;Private voidButton_Click (Objectsender, RoutedEventArgs e) {    //Timing Control content display and scroll bar positionDispatcherTimer _timer =NewDispatcherTimer (); _timer. Interval= Timespan.fromseconds (1); _timer. Tick+ = (st, ET) =    {        //gets the contents of the specified rowBlockcollection col =richTextBox.Document.Blocks; intindex =0; TextElement prev=NULL; foreach(TextElement IteminchCol) {            //Modify the style of the current row            if(Index = =pindex)            {Alterstyle (item, prev); } Index++; Prev=item; } pindex++;    }; _timer. Start ();}Private voidAlterstyle (textelement item, TextElement prev) {//Current LineParagraph CP = Item asParagraph; Cp.foreground=brushes.red; TextRange Range=NewTextRange (Cp.contentstart, cp.contentend); //Scroll Position Control    if(Pindex >0&& range. Text.length >0)    {        //previous line, style callback        if(Prev! =NULL) {prev. Foreground=Brushes.black; } curtop+ = range. Text.length >7? +: -; Curtop+= -;    Richtextbox.scrolltoverticaloffset (Curtop); }}

Operation Result:

WPF RichTextBox scroll bar auto-scroll instance, text auto-scroll instance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.