Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

RichTextBox line breaks disappear after copying from Settings

$
0
0

Hi, I have two richtextboxes rtbInp and rtbOut.
For rtbInp in the xaml I set Paragraph Margin to 0 to avoid extra lines when user hits the 'Enter' key.
The button 'Copy' copies content of rtbInp to rtbOut.

The rtbInp text is also saved to Settings so it's loaded back to this textbox when the application opens next time, and this where I have issue.

When we clear the text and enter manually some in rtbInp, on 'Copy' the rtbOut looks exactly the same.
But when application loads next time the rtbInp text looks exactly the same as the last time, however on 'Copy' the rtbOut doesn't show the line breaks anymore. Why?

<Window x:Class="WpfAppRTB.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded" Closing="Window_Closing"><Grid><Grid.RowDefinitions><RowDefinition Height="auto"></RowDefinition><RowDefinition Height="*"></RowDefinition></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition Width="*"></ColumnDefinition><ColumnDefinition Width="*"></ColumnDefinition></Grid.ColumnDefinitions><Button Name="btnCopy" Grid.Row="0" Grid.ColumnSpan="2" HorizontalAlignment="Center" Click="btnCopy_Click">Copy</Button><RichTextBox Name="rtbInp" Grid.Row="1" Grid.Column="0" VerticalScrollBarVisibility="Auto"><RichTextBox.Resources><Style TargetType="{x:Type Paragraph}"><Setter Property="Margin" Value="0"/></Style></RichTextBox.Resources></RichTextBox><RichTextBox Name="rtbOut" Grid.Row="1" Grid.Column="1" VerticalScrollBarVisibility="Auto" IsReadOnly="True"></RichTextBox></Grid></Window>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace WpfAppRTB
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        bool isFirstTimeWindowLoaded = true;

        private void btnCopy_Click(object sender, RoutedEventArgs e)
        {
            rtbOut.Document.Blocks.Clear();

            TextRange textRangeInp = new TextRange(rtbInp.Document.ContentStart, rtbInp.Document.ContentEnd);
            MemoryStream ms = new MemoryStream();
            textRangeInp.Save(ms, DataFormats.Rtf);

            FlowDocument fd = new FlowDocument();
            TextRange textRangeOut = new TextRange(fd.ContentStart, fd.ContentEnd);
            textRangeOut.Load(ms, DataFormats.Rtf);
            rtbOut.Document = fd;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (isFirstTimeWindowLoaded)
            {
                string lastInp = WpfAppRTB.Properties.Settings.Default.strInp;

                if (!String.IsNullOrEmpty(lastInp))
                {
                    MessageBox.Show(lastInp,"Read from Settings");
                    rtbInp.Document.Blocks.Clear();
                    rtbInp.Document.Blocks.Add(new Paragraph(new Run(lastInp)));
                }

                isFirstTimeWindowLoaded = false;
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            string lastInp = new TextRange(rtbInp.Document.ContentStart, rtbInp.Document.ContentEnd).Text;
            MessageBox.Show(lastInp, "Writing to Settings");

            if (!String.IsNullOrEmpty(lastInp))
                WpfAppRTB.Properties.Settings.Default.strInp = lastInp;

            WpfAppRTB.Properties.Settings.Default.Save();

        }
    }
}

RTBThanks,

-srinivas y.




Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>