locked
Changing textbox color with referencing problem RRS feed

  • Question

  • Hello everybody,

    I'm trying to change the color of a textbox based on a gotFocus event. My gotFocus event works fine but I'm having trouble getting the textbox to change color.

    I've looked at many forums looking for a solution to this but every solution I try, ends up with me getting an error that was not explained in the said forum. Most solutions I came across included using something called 'SolidColorBrush' and every time I try to use it, I get a referencing error meaning that the 'SolidColorBrush' class in not available in my project. Could someone please explain to me how I can reference this class, using the 'using' keyword to get my problem solved? Any other method to solve this would also be fine. 

    I would really appreciate this. Cheers!

    Wednesday, January 8, 2014 1:56 PM

All replies

  • You should be able to use that class in your code. Like this:

    var brush = new SolidColorBrush(Color.Black);


    Toni Petrina
    My blog: Toni codes .NET
    If a post answers your question, please click "Mark As Answer" on that post and "Vote as Helpful"

    Wednesday, January 8, 2014 2:51 PM
  • Thanks for the reply Toni!

    When trying to use the code I get an error with both 'SolidColorBrush' (The type or namespace SolidColorBrush could not be found) and 'Color' (The name Color does not exist in the current context).

    Could you please explain as to how I can fix this?

    var brush = new SolidColorBrush(Color.Black);

    Friday, January 10, 2014 6:55 AM
  • You should be able to use that class in your code. Like this:

    var brush = new SolidColorBrush(Color.Black);


    Toni Petrina
    My blog: Toni codes .NET
    If a post answers your question, please click "Mark As Answer" on that post and "Vote as Helpful"

    Both SolidColorBrush and Colors.Black can be found in the System.Windows.Media name space.  Note that Colors.Black has the plural form of color.

    You can either add a "using System.Windows.Media;" line in your file or qualify both the SolidColorBrush and Colors calls.  (In Visual Studio, there should be able to "Resolve" the error by right clicking on the word showing the error and selecting the appropriate "Resolve" action in the list.  I am using the Professional version, so I am not sure if that is available in the free version.)

    Friday, January 10, 2014 7:13 AM
  • Hi,

    You can try this.

       <TextBox Width="456" Visibility="Collapsed" Name="textbox" BorderBrush="Red" FontStyle="Italic" MaxLength="100" TextChanged="textbox_TextChanged" GotFocus="textbox_GotFocus"/>

    and .cs file on got focus event

        private void textbox_GotFocus(object sender, RoutedEventArgs e)
            {
                SolidColorBrush brush = (sender as TextBox).Foreground as SolidColorBrush;
                if (null != brush)
                {
                    brush.Color = Colors.Yellow;
                }
            }

    Let me know if you still get any problem.

    Thanks

    Friday, January 10, 2014 7:13 AM