none
How to change TextBlock Foreground color in C#

    Question

  • Greetings,

    I am brand new to C# and I am learning a little of how it relates to the page.xaml code behind page.xaml.cs by following Laurent Bugnion's Silverlight 2 Unleashed.

     I have a problem in which I changed the the background color to a rich dark blue and the study in the book wants to print to the screen with black foreground color.  I just cannot read the text.  So I searched MSDN, the forums, and Google about how to change foreground color in C# but just couldn't get an answer or example that I can understand.  I would very much appreciate it if someone would please explain to me how to change the TextBlock Foreground to yellow.

    The following is the snippet of code in question:

    using System; 
    using System.Windows; 
    using System.Windows.Controls; 
    using System.Windows.Documents; 
    using System.Windows.Ink; 
    using System.Windows.Input; 
    using System.Windows.Media; 
    using System.Windows.Media.Animation; 
    using System.Windows.Shapes; 
    using System.Windows.Media.Imaging; 
    using Thumbnails.Data; 
     
    namespace Thumbnails 
        public partial class Page : UserControl 
        { 
        private FrameworkElement _expandedMedia = null
     
        public Page() 
        { 
          // Required to initialize variables 
          InitializeComponent(); 
     
          FrameworkElement media = null
          int index = 0; 
          do 
          { 
            media = this.FindName("Media" + index++) as FrameworkElement; 
            if (media != null
            { 
              media.Cursor = Cursors.Hand; 
              media.MouseLeftButtonDown 
                += new MouseButtonEventHandler(media_MouseLeftButtonDown); 
            } 
          } 
          while (media != null); 
     
          DataFile dataFile = new DataFile(); 
          User user = dataFile.LoadUser(); 
          if (user.LastVisit == null
          { 
            LastVisitTextBlock.Text = "(This is your first visit)"
          } 
          else 
          { 
              LastVisitTextBlock.Text = "(Your last visit: " 
              + user.LastVisit.Value.ToShortDateString() 
              + " " + user.LastVisit.Value.ToLongTimeString() + ")"
          } 
          user.SetLastVisit(); 
          dataFile.SaveUser(user); 
        } 
     
    It is the output of "This is your first visit" and "Your last visit was on ..dat and time" that I would like o change the font or foreground color to yellow.

    I will very much appreciate any help in solving this small problem.

    Peace,

    Phroneo
    Friday, January 2, 2009 6:37 AM

Answers

  • you can do
    txtBlock.Foreground= new SolidColorBrush(Colors.Yellow);

    • Marked as answer by Phroneo Friday, January 2, 2009 7:57 PM
    Friday, January 2, 2009 7:45 PM
    Moderator

All replies

  • just set the foreground.
    LastVisitTextBlock.Foreground = Brushes.Yellow;
    • Proposed as answer by Vineeth Paliath Friday, January 2, 2009 11:40 AM
    • Unproposed as answer by Phroneo Friday, January 2, 2009 7:35 PM
    Friday, January 2, 2009 7:42 AM

  • Hello MCMoser,

    Thank you for your suggestion. 

    The line of C# you gave me was one of the statements that I had tried.  VS2008 will not let me run the application with that particular line as it is.  I keep on getting this default: "The name 'Brushes' does not exist in the current context." It makes no difference where I put the line - either before of after 'LastVistTextblock.Text = ( )'.  There must be some sort of defining statement (get or set?) for 'Brushes'. 

    As I wrote in my first post, I am brand new to C#.  I may have put the statement in the wrong location. I would be grateful for some more help.

    Peace,
    Phroneo
    Friday, January 2, 2009 7:35 PM
  • you can do
    txtBlock.Foreground= new SolidColorBrush(Colors.Yellow);

    • Marked as answer by Phroneo Friday, January 2, 2009 7:57 PM
    Friday, January 2, 2009 7:45 PM
    Moderator
  • Hello Lee,

    Thank you.  Your suggestion worked just as I hoped it would - now I've yellow (or whatever other color I might want) text.

    I've marked your response to my question as the answer. 

    Peace,
    Phroneo
    Friday, January 2, 2009 8:00 PM
  • That worked like a charm...Thanks a lot
    Thursday, October 13, 2011 3:30 PM
  • you can do
    txtBlock.Foreground= new SolidColorBrush(Colors.Yellow);

    thank you! how do you get it in an IF?

    August

    Tuesday, August 7, 2012 1:50 PM
  • doesn't work for me :( 

    it says "the name "colors" doesn't exist in the current context".  Why ????

    Monday, July 11, 2016 1:21 PM
  • i figured it out, but already forgot, anyway... just play around with the -s and capital "c". look for the advised API usage. 

    (txtBlock.Foreground= new SolidColorBrush(Colors.Yellow); the lightbulb will most likely appear and suggest using "using windows.ui.colors") or something similar...

    try:

    color,Color,colors,Colors

    hope it helps :)

    Saturday, October 7, 2017 10:09 PM