Warm tip: This article is reproduced from serverfault.com, please click

Richtextbox with color and save,load the richtextbox with color into a rtf file in wpf c#

发布于 2020-12-02 15:00:31

How can i make a richtextbox support color and save the richtextbox content in a rtf file with wpf and c#? I want that the user can select a word in a richtextbox and give it a specail font color. And that the user can save the richtextbox with the special color into a .rtf file and load it back into the richtextbox with the special color.

Questioner
Eagle
Viewed
0
RolandJS 2020-12-03 02:40:19

Here you have something to get you started:

  • Write a filename with path like "C:\test.rtf"
  • Click on "Open" even if the file is new.
  • Add some text below "Document"
  • Select a word and click on the ".. add color.. " button.
  • Save the file.
  • Restart the program and open the file. The text should appear in the Document field.

XAML:

<Window x:Class="WpfApp11.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp11"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <StackPanel>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="File path: "/>
                <TextBox x:Name="filePathBox" Width="300" Margin="5,0,0,0"/>
            </StackPanel>
            <Button x:Name="open" Click="open_Click" Content="Open" Width="300" Margin="0,5,0,0" HorizontalAlignment="Left"/>
            <TextBlock Text="Document" Margin="0,5,0,0"/>
            <RichTextBox x:Name="richTextBox" Margin="0,5,0,0"/>
            <Button x:Name="addColor" Click="addColor_Click" Content="Select and press this button to add color to text" Width="300" Margin="0,5,0,0" HorizontalAlignment="Left"/>
            <Button x:Name="save" Click="save_Click" Content="Save" Width="300" Margin="0,5,0,0" HorizontalAlignment="Left"/>
            <TextBlock Text="Status"/>
            <TextBlock x:Name="statusBox" Width="300" Margin="0,5,0,0" HorizontalAlignment="Left"/>
        </StackPanel>
    </Grid>
</Window>

Code behind (xaml.cs)

using System;
using System.IO;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;

namespace WpfApp11
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            DataContext = this;
            InitializeComponent();
        }

        private void addColor_Click(object sender, RoutedEventArgs e)
        {
            richTextBox.Selection.ApplyPropertyValue(ForegroundProperty, new SolidColorBrush(Colors.Red));
        }

        private void open_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                TextRange range;
                FileStream fStream;
                string filename = filePathBox.Text;
                if (File.Exists(filename))
                {
                    range = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
                    using (fStream = new FileStream(filename, FileMode.OpenOrCreate))
                    {
                        range.Load(fStream, DataFormats.Rtf);
                        fStream.Close();
                    }
                    statusBox.Text = "File opened.";
                }
                else
                {
                    statusBox.Text = "File new.";
                }
            }
            catch (Exception excep)
            {
                statusBox.Text = excep.Message;
            }
        }

        private void save_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                TextRange range;
                FileStream fStream;
                range = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
                string filename = filePathBox.Text;
                using (fStream = new FileStream(filename, FileMode.Create))
                {
                    range.Save(fStream, DataFormats.Rtf);
                    fStream.Close();
                }

                statusBox.Text = "File saved.";
            }
            catch (Exception excep)
            {
                statusBox.Text = excep.Message;
            }
        }
    }
}