This is a migrated thread and some comments may be shown as answers.

RichTextBox: changing colors and scrolling

2 Answers 132 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Kate
Top achievements
Rank 1
Kate asked on 18 Nov 2010, 12:57 PM
Hello!
I have a problem with changing RadDocument in code: I set Background color for tables and ForeColor for spans inside table cells when I prepare RadDocument in code, then I change ForeColor and Background on button click, but the Document updates only after scrolling. Where is my mistake? 

Here is my code:
public partial class TestWindow : UserControl
    {
        public TestWindow()
        {
            InitializeComponent();
        }
  
        public RadDocument BaseRadDocument;
  
        public void createRadDocument()
        {
            BaseRadDocument = new RadDocument();
            BaseRadDocument.LayoutMode = DocumentLayoutMode.Flow;
            BaseRadDocument.ParagraphDefaultSpacingAfter = 0;
            BaseRadDocument.PageViewMargin = new SizeF(0, 0);
            BaseRadDocument.SectionDefaultPageMargin = new Padding(0);
  
            Section section = new Section();
  
            CreateSection(0, section);
  
            BaseRadDocument.Sections.Add(section);
            rich.Document = BaseRadDocument;
        }
  
        public void CreateSection(int level, Section section)
        {
            for (int i = 0; i < 15; i++)
            {
                createDocElementsForNewItem(i, section);
            }
        }
  
        private void createDocElementsForNewItem(int level, Section section)
        {
            int indent = 20 * level;
  
            var cell_11 = new TableCell();
            cell_11.PreferredWidth = new TableWidthUnit(80 + indent);
  
            var cell_12 = new TableCell();
  
            Paragraph paragraph_111 = new Paragraph();
  
            paragraph_111.LeftIndent = indent;
            paragraph_111.SpacingAfter = 0;
            paragraph_111.SpacingBefore = 0;
            paragraph_111.TextAlignment = RadTextAlignment.Right;
  
            Span date = new Span(String.Format("{0} ", DateTime.UtcNow.ToShortDateString()));
            date.FontSize = 12;
            date.ForeColor = new Color() { A = 0xff, B = 0x68, G = 0x68, R = 0x68 };
            date.FontFamily = new FontFamily("Calibri");
            paragraph_111.Inlines.Add(date);
  
            Paragraph paragraph_112 = new Paragraph();
            paragraph_112.LeftIndent = indent;
            paragraph_112.SpacingAfter = 0;
            paragraph_112.SpacingBefore = 0;
            paragraph_112.TextAlignment = RadTextAlignment.Right;
  
            Span time = new Span(String.Format("{0} ", DateTime.UtcNow.ToLongTimeString()));
            time.FontSize = 12;
            time.ForeColor = new Color() { A = 0xff, B = 0x68, G = 0x68, R = 0x68 };
            time.FontFamily = new FontFamily("Calibri");
            paragraph_112.Inlines.Add(time);
            paragraph_112.LeftIndent = indent;
  
            for (int j = 0; j < 5; j++)
            {
                var span = new Span(String.Format("Текст {0}", j));
                var par = new Paragraph();
                span.ForeColor = new Color() { A = 0xff, B = 0x00, G = 0x52, R = 0xC6 };
                par.Inlines.Add(span);
                cell_12.Blocks.Add(par);
            }
  
            cell_11.Blocks.Add(paragraph_111);
            cell_11.Blocks.AddAfter(paragraph_111, paragraph_112);
  
            var row_1 = new TableRow();
  
            row_1.Cells.Add(cell_11);
            row_1.Cells.AddAfter(cell_11, cell_12);
  
            Table table = new Table();
            table.Background = new Color(){A = 0xFF, R=0xFF, G=0xB9, B=0xB9};
  
            table.Rows.Add(row_1);
  
            section.Blocks.Add(table);
            section.Blocks.Add(new Paragraph());
        }
  
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            createRadDocument();
        }
  
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            foreach (var section in BaseRadDocument.Sections)
                foreach (var block in section.Blocks)
                {
                    if (block is Table)
                    {
                        var table = block as Table;
                        table.Background = new Color() { A = 0xFF, R = 0xFC, G = 0xFF, B = 0xC8 };
  
                        foreach (var row in table.Rows)
                            foreach (var cell in row.Cells)
                                foreach (var item in cell.Blocks)
                                    if (item is Paragraph)
                                    {
                                        foreach (var inline in (item as Paragraph).Inlines)
                                            if (inline is Span)
                                            {
                                                (inline as Span).ForeColor = new Color() { A = 0xFF, R = 0x1F, G = 0x3C, B = 0x6C };
                                            }
                                    }
                    }
                     
                }
  
            BaseRadDocument.UpdateLayout();
  
        }
    }

2 Answers, 1 is accepted

Sort by
0
Iva Toteva
Telerik team
answered on 18 Nov 2010, 05:09 PM
Hello Kate,

In order to apply the changes to the document, you actually need to call UpdateEditorLayout() on the instance of RadRichTextBox and not RadDocument, as UpdateEditorLayout makes sure some bookkeeping is done.
However, there was an issue with the Background property of Table, which is now fixed. It will be visible in the internal build at the end of the week. As a workaround you can set the Background to every cell.

Overall, the changes you need to do to your project are to add the following line:

foreach (var cell in row.Cells)
{
    cell.Background = table.Background;
   ...

and substitute
BaseRadDocument.UpdateLayout()
for
this.rich.UpdateEditorLayout();

I hope that helps.


Best wishes,
Iva
the Telerik team
Browse the videos here>> to help you get started with RadControls for Silverlight
0
Kate
Top achievements
Rank 1
answered on 26 Nov 2010, 01:22 PM
Thank you very much! It helped.
Tags
RichTextBox
Asked by
Kate
Top achievements
Rank 1
Answers by
Iva Toteva
Telerik team
Kate
Top achievements
Rank 1
Share this question
or