Click here to Skip to main content
13,139,254 members (73,259 online)
Rate this:
Please Sign up or sign in to vote.
Hi guys,

I'm trying to get this little app to colour text that is selected when a button is pressed, or colour anything that comes after the caret when typing if nothing is selected.

This works, but I have a bug where, if I insert the caret between two characters and press a button, EVERYTHING is coloured. I do not want this, just everything that comes after the caret.

My assumption is that 'rtb.Selection.ApplyPropertyValue(TextElement.ForegroundProperty, colour);' is assuming everything on either side of it is 'selected'.

Can anyone assist, perhaps?

Thank you in advance.

<Window x:Class="WpfApplication1.MainWindow"
        Title="MainWindow" Height="300" Width="400">
    <Grid removed="Gray">
        <RichTextBox removed="Black" Foreground="White" Height="150" HorizontalAlignment="Right" Name="rtb" VerticalAlignment="Top" Width="354" Margin="0,12,12,0" />
        <Button Background="Blue" Content="Blue" Height="75" HorizontalAlignment="Left" Margin="139,174,0,0" Name="button2" VerticalAlignment="Top" Width="100" Click="btnColour_Click" Foreground="Yellow" />
        <Button Background="Red" Content="Red" Height="75" HorizontalAlignment="Left" Margin="12,174,0,0" Name="button1" VerticalAlignment="Top" Width="100" Click="btnColour_Click" Foreground="Blue" />
        <Button Background="Yellow" Content="Green" Height="75" HorizontalAlignment="Left" Margin="266,174,0,0" Name="button3" VerticalAlignment="Top" Width="100" Click="btnColour_Click" Foreground="Red" />

        private void colourText(Brush colour)
            TextPointer startPointer = rtb.Document.ContentStart.GetNextInsertionPosition(LogicalDirection.Forward);
            TextPointer endPointer = rtb.Document.ContentEnd.GetNextInsertionPosition(LogicalDirection.Backward);
            TextRange range = new TextRange(startPointer, endPointer);
            int length = range.Text.Length;
            rtb.Selection.ApplyPropertyValue(TextElement.ForegroundProperty, colour);
            if (length == 0)
                rtb.Foreground = colour;
        private void btnColour_Click(object sender, RoutedEventArgs e)
            Button btn = sender as Button;
            Brush colour = btn.Background;
Posted 7-Jul-12 8:05am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 2

It looks like the only way to resolve this I could come up with was to use the OnTexTInput event and have it manually change every letter when typed. I fear for the issues of speed, but so far it seems to work.

(I'll get used to this solution thing one day, maybe!)

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.170915.1 | Last Updated 9 Jul 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100