1

I am making a text editor program in WPF and I need to make a Plain button, which it's purpose is to remove the formatting (bold, italic, font such as Arial, font size) of text of the RichTextBox.

This is my code so far:

<Window x:Class="TextEditor.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TextEditor"
    Title="Text Editor" Height="480" Width="640">

    <DockPanel>
        <Menu DockPanel.Dock="Top">

            <MenuItem Header="_File" Name="Menu">
                <MenuItem Header="New" Name="New"
                          Click="New_Click" />

                <MenuItem Header="Save" Name="Save"
                          Click="Save_Click" />
             </MenuItem>
        </Menu>

        <DockPanel DockPanel.Dock="Top">                       
                <ToolBar>

                <ToggleButton x:Name="boldButton"
                              ToolTip="Bold"
                              Command="{x:Static EditingCommands.ToggleBold}" CommandTarget="{Binding ElementName=_richTextBox}">

                    <Image Source="Icons/text_bold.png"
                            Height="25"
                            Width="25"/>

                    </ToggleButton>

                    <ToggleButton x:Name="italicButton"
                              ToolTip="Italic"
                              Command="{x:Static EditingCommands.ToggleItalic}" CommandTarget="{Binding ElementName=_richTextBox}">
                        <Image Source="Icons/text_italic.png"
                            Height="25"
                            Width="25"/>

                    </ToggleButton>

                <ToggleButton x:Name="PlainButton"
                        ToolTip="Make the text plain"
                        Click="PlainButton_Click">
                    PlainText
                </ToggleButton>

                <Button x:Name="BackgroundButton"
                        ToolTip="Change the background of the textbox"
                        Click="BackgroundButton_Click">
                        Change the background                       
                        
                </Button>
                    <Separator/>

                    <ComboBox x:Name="fonts"
                          MinWidth="100"
                          DataContext="{x:Static Fonts.SystemFontFamilies}"
                          ItemsSource="{Binding}"
                          ToolTip="Font"
                           SelectionChanged="Font_SelectionChanged"/>

                <ComboBox x:Name="fontSize"
                          MinWidth="40"
                          ToolTip="Font Size"
                          
                          >  

                </ComboBox>
                </ToolBar>            
            </DockPanel>

            <StatusBar DockPanel.Dock="Bottom">
            <TextBlock x:Name="status"/>            
        </StatusBar>
        
        <RichTextBox x:Name="body"
                     FontSize="{Binding ElementName=fontSize, Path=SelectedItem}"
                     
                     SpellCheck.IsEnabled="True"
                     AcceptsReturn="True"
                     AcceptsTab="True"
                     SelectionChanged="body_SelectionChanged"
                     BorderThickness="0 2 0 0"/>           
        
                     
    </DockPanel>
</Window>  

C sharp code:

public MainWindow()
{
    InitializeComponent();
    for (double i = 8; i <= 48; i += 2)
    {
        fontSize.Items.Add(i);
    }            
}  
 
private void body_SelectionChanged(object sender, RoutedEventArgs e)
{
    object temp = body.Selection.GetPropertyValue(Inline.FontFamilyProperty);
    fonts.SelectedItem = temp;         

}

private void New_Click(object sender, RoutedEventArgs e)
{
    body.Document.Blocks.Clear();
}

private void Save_Click(object sender, RoutedEventArgs e)
{
    SaveFileDialog sfd = new SaveFileDialog();
    sfd.Filter = "Text file|*.txt";
    sfd.FileName = "Untitled document";

    if (sfd.ShowDialog() == true)
    {
        FileStream fileStream = new FileStream(sfd.FileName, FileMode.Create);
        TextRange range = new TextRange(body.Document.ContentStart, body.Document.ContentEnd);
        range.Save(fileStream, DataFormats.Text);
    }
}

private void Font_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (fonts.SelectedItem != null)
        body.Selection.ApplyPropertyValue(Inline.FontFamilyProperty, fonts.SelectedItem);
}

private void PlainButton_Click(object sender, RoutedEventArgs e)
{           
  //the code of the plain button
}

private void BackgroundButton_Click(object sender, RoutedEventArgs e)
{    
    body.Background = Brushes.Yellow;
}
Jackdaw
  • 7,626
  • 5
  • 15
  • 33
Fran9419
  • 11
  • 2

2 Answers2

0

It's a solution from 2009 ... I don't know if the richtext API in .NET has improved, but this post seems to have a solution for what you need.

Community
  • 1
  • 1
olitee
  • 1,683
  • 10
  • 12
0

Use the following code for WPF, if you are still looking for answers.

myRichTextBox.SelectAll();
myRichTextBox.Selection.ClearAllProperties();
Owen
  • 1