Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF Color buttons selection , +
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"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        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" />
    </Grid>
</Window>
 
        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;
 
            colourText(colour);
            rtb.Focus();
        }
Posted 7-Jul-12 8:05am

1 solution

Rate this: bad
good
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!)
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 766
1 OriginalGriff 345
2 CPallini 200
3 PIEBALDconsult 150
4 Magic Wonder 131
0 OriginalGriff 5,795
1 Sergey Alexandrovich Kryukov 5,028
2 CPallini 4,700
3 George Jonsson 3,142
4 Gihan Liyanage 2,450


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 9 Jul 2012
Copyright © CodeProject, 1999-2014
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