0

I'm writing a Console for users, so that they can write code in it similar to Visual Studio. I was wondering how to change the color of a set of text in a WPF TextBox as soon as the program detects the input of a certain character, such as the /* */ comment tags.

When a user enters a /* tag, the text after the tag should be colored in green and when the users closes the tags, the texts should turn back to white. I tried doing this with the TextChanged method but I don't know how to proceed.

('console' is the TextBox in my Window)

private void console_TextChanged(object sender, TextChangedEventArgs e)
{
      if (e.Changes.Equals("/*"))
      {
             console.Foreground = Brushes.Green;
      }
}
Jay
  • 4,873
  • 7
  • 72
  • 137
  • console_TextChanged means that when you type text and focus out of the textbox then it will be called. You should use KeyPress event instead. – Lucassith May 06 '15 at 13:25
  • I see. But even if I use KeyPress, I'd have to scan the previously typed text to see if the 'currently typing' text is within comment tags, mate. How can I do that? @Lucassith – Jay May 06 '15 at 13:29
  • http://stackoverflow.com/questions/14085607/textbox-text-color-change identical question although there is no good answer. – Lucassith May 06 '15 at 13:42
  • I saw that @Lucassith but it doesn't provide a proper answer for the question – Jay May 06 '15 at 14:11

1 Answers1

0

You cannot use TextBox to change some specific portion of color. In this case you need RichTextBox. You use the PreviewTextInput event to get the typed text/char. I have written some logic to make the foreground of the RichTextBox change when typing specific char. I think you can build your logic one top of this code.

 <RichTextBox x:Name="rtb" Width="200" Height="200" PreviewTextInput="rtb_PreviewTextInput"/>

 public partial class MainWindow : Window
{
    string prevText=string.Empty;
    TextPointer start;
    public MainWindow()
    {
        InitializeComponent();            
    }      
    private void rtb_PreviewTextInput(object sender, TextCompositionEventArgs e)
    {
        if (e.Text.Equals("*") && (prevText.Equals("/")))
        {
            start = rtb.CaretPosition;
            TextPointer end = rtb.Document.ContentEnd;
            TextRange range = new TextRange(start, end);
            range.ApplyPropertyValue(RichTextBox.ForegroundProperty, Brushes.Green);
        }
        if (e.Text.Equals("/") && (prevText.Equals("*")))
        {                
            TextPointer end = rtb.CaretPosition; 
            TextRange range = new TextRange(start, end);
            range.ApplyPropertyValue(RichTextBox.ForegroundProperty, Brushes.Red);
        }
        prevText = e.Text;
    }        
}
Ayyappan Subramanian
  • 5,348
  • 1
  • 22
  • 44
  • Hey this is a great start! However the start of comment tag /* malfunctions a bit, the green doesn't show up. Any idea why? – Jay May 06 '15 at 16:15
  • I thought of giving idea in which you can improve.. May be complete logic might some time... – Ayyappan Subramanian May 06 '15 at 16:23
  • Currently when you add some text then if you place /* inbetween the text it will change font green to the rest of the text – Ayyappan Subramanian May 06 '15 at 16:24
  • Hey thanks again for taking the time. I'll play around it a bit and see what I can do. I'll select your answer now but if you have the time and you come up with a better code please email it to me, mate :) dinukawylde@gmail.com. Thank you sir. – Jay May 06 '15 at 16:28
  • Only problem is having to add the tag later to see the color. How can I modify the code such that it changes the color as soon as the /* is entered, Ayyappan? – Jay May 06 '15 at 16:56