Click here to Skip to main content
15,881,804 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi, I need to make a basic Text Editor. This is what I have so far. Now I don't know how to handle RichTextBox_SelectionChanged. Thanks for advices.

XML
<Window x:Class="MyWpfTextEditor.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">
    <Grid>
        <RichTextBox x:Name="rtBox" HorizontalAlignment="Left" Height="264" Margin="10,45,0,0" VerticalAlignment="Top" Width="323" Loaded="rtBox_Loaded" SelectionChanged="rtBox_SelectionChanged"/>
        <ComboBox x:Name="fontSizeComBox" SelectedIndex="4" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="50" Loaded="fontSizeComBox_Loaded" SelectionChanged="fontSizeComBox_SelectionChanged"/>
        <ComboBox x:Name="fontNameComBox" SelectedIndex="9" HorizontalAlignment="Left" Margin="65,10,0,0" VerticalAlignment="Top" Width="230" SelectionChanged="fontNameComBox_SelectionChanged" Loaded="fontNameComBox_Loaded"/>
        <ToggleButton x:Name="boldToggButt" Content="B" FontWeight="Bold" Width="22" Height="22" Margin="300,10,192,287" Checked="boldToggButt_Checked" Unchecked="boldToggButt_Unchecked" />
        <ToggleButton x:Name="italicToggButt" Content="I" FontStyle="Italic" Width="22" Height="22" Margin="328,10,167,287" Checked="italicToggButt_Checked" Unchecked="italicToggButt_Unchecked"/>
        <ToggleButton x:Name="underToggButt" Width="22" Height="22" Margin="355,10,140,287" Checked="underToggButt_Checked" Unchecked="underToggButt_Unchecked">
            <ToggleButton.Content>
                <TextBlock>
                    <Underline>U</Underline>
                </TextBlock>
            </ToggleButton.Content>
        </ToggleButton>

    </Grid>
</Window>


C#
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;

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

        //FontSize
        private void fontSizeComBox_Loaded(object sender, RoutedEventArgs e)
        {  
            fontSizeComBox.ItemsSource = new List<double>() { 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72 };
        }

        private void fontSizeComBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (rtBox.Selection.IsEmpty)
            {
                var r = new Run() { FontSize = (double)fontSizeComBox.SelectedValue };
                (rtBox.Document.Blocks.FirstBlock as Paragraph).Inlines.Add(r);
                rtBox.CaretPosition = r.ElementStart;
            }
            else
            {
                rtBox.Selection.ApplyPropertyValue(Inline.FontSizeProperty, fontSizeComBox.SelectedValue);
            }
            rtBox.Focus();
        }

        //FontFamily
        private void fontNameComBox_Loaded(object sender, RoutedEventArgs e)
        {
            fontNameComBox.ItemsSource = Fonts.SystemFontFamilies.OrderBy(f => f.Source);
        }

        private void fontNameComBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (rtBox.Selection.IsEmpty)
            {
                var r = new Run() { FontFamily = (FontFamily)fontNameComBox.SelectedItem };
                (rtBox.Document.Blocks.FirstBlock as Paragraph).Inlines.Add(r);
                rtBox.CaretPosition = r.ElementStart;
            }
            else
            {
                rtBox.Selection.ApplyPropertyValue(Inline.FontFamilyProperty, fontNameComBox.SelectedItem);
            }
            rtBox.Focus();
        }

        //Bold Button
        private void boldToggButt_Checked(object sender, RoutedEventArgs e)
        {
            if (rtBox.Selection.IsEmpty)
            {
                var r = new Bold() { FontWeight = (FontWeight)FontWeights.Bold };
                (rtBox.Document.Blocks.FirstBlock as Paragraph).Inlines.Add(r);
                rtBox.CaretPosition = r.ElementStart;
            }
            else
            {
                rtBox.Selection.ApplyPropertyValue(Inline.FontWeightProperty, FontWeights.Bold);
            }
            rtBox.Focus();
        }

        private void boldToggButt_Unchecked(object sender, RoutedEventArgs e)
        {
            if (rtBox.Selection.IsEmpty)
            {
                var r = new Bold() { FontWeight = (FontWeight)FontWeights.Normal };
                (rtBox.Document.Blocks.FirstBlock as Paragraph).Inlines.Add(r);
                rtBox.CaretPosition = r.ElementStart;
            }
            else
            {
                rtBox.Selection.ApplyPropertyValue(Inline.FontWeightProperty, FontWeights.Normal);
            }
            rtBox.Focus();
        }

        //Italic Button
        private void italicToggButt_Checked(object sender, RoutedEventArgs e)
        {
            if (rtBox.Selection.IsEmpty)
            {
                var r = new Italic() { FontStyle = (FontStyle)FontStyles.Italic };
                (rtBox.Document.Blocks.FirstBlock as Paragraph).Inlines.Add(r);
                rtBox.CaretPosition = r.ElementStart;
            }
            else
            {
                rtBox.Selection.ApplyPropertyValue(Inline.FontStyleProperty, FontStyles.Italic);
            }
            rtBox.Focus();
        }

        private void italicToggButt_Unchecked(object sender, RoutedEventArgs e)
        {
            if (rtBox.Selection.IsEmpty)
            {
                var r = new Italic() { FontStyle = (FontStyle)FontStyles.Normal };
                (rtBox.Document.Blocks.FirstBlock as Paragraph).Inlines.Add(r);
                rtBox.CaretPosition = r.ElementStart;
            }
            else
            {
                rtBox.Selection.ApplyPropertyValue(Inline.FontStyleProperty, FontStyles.Normal);
            }
            rtBox.Focus();
        }

        //Underline Button
        private void underToggButt_Checked(object sender, RoutedEventArgs e)
        {
            if (rtBox.Selection.IsEmpty)
            {
                var r = new Underline() { TextDecorations = (TextDecorationCollection)TextDecorations.Underline };
                (rtBox.Document.Blocks.FirstBlock as Paragraph).Inlines.Add(r);
                rtBox.CaretPosition = r.ElementStart;
            }
            else
            {
                rtBox.Selection.ApplyPropertyValue(Inline.TextDecorationsProperty, TextDecorations.Underline);
            }
            rtBox.Focus();
        }

        private void underToggButt_Unchecked(object sender, RoutedEventArgs e)
        {
            if (rtBox.Selection.IsEmpty)
            {
                var r = new Underline() { TextDecorations = (TextDecorationCollection)null };
                (rtBox.Document.Blocks.FirstBlock as Paragraph).Inlines.Add(r);
                rtBox.CaretPosition = r.ElementStart;
            }
            else
            {
                rtBox.Selection.ApplyPropertyValue(Inline.TextDecorationsProperty, null);
            }
            rtBox.Focus();
        }

        //RichTextBox
        private void rtBox_Loaded(object sender, RoutedEventArgs e)
        {
            rtBox.AcceptsTab = true;
            rtBox.AcceptsReturn = true;
            rtBox.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
        }

        private void rtBox_SelectionChanged(object sender, RoutedEventArgs e)
        {

        }
    }
}
Posted

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900