locked
Getting a Textbox's Current Background Color? RRS feed

  • Question

  • Building a Win8 Store App in XAML and VB, I am curious how to get the current background color of a textbox. I can set a textbox's background property by using this syntax:

    TextBox1.Background = New SolidColorBrush(Colors.White)

    But using VB, how do I inquire of a textbox's current background color?

    Thanks in advance.

    Sunday, October 6, 2013 8:18 PM

Answers

  • I'd keep track of the logical state in the model rather than tracking the color. You can bind the color shown to the state.

    Relying on hard coded colors in the interface will be difficult and limiting. It can badly in high contrast modes and make your app unusable.

    --Rob

    Monday, October 7, 2013 12:08 AM
    Moderator
  • Hi David,

    I'm out on my phone so I can't give a link or code, but look up data binding and properties in the docs. Essentially you'll set a class level variable to mark whatever changes the color. Internally the code just changes or checks this variable and doesn't care about the color. By making the variable a property the TextBox can bind its color to automatically draw appropriately for the property state.

    Somebody on a fuller system may be able to explain in more detail

     or point towards specific documentation.

    Monday, October 7, 2013 5:26 AM
    Moderator

All replies

  • What is your goal for this? Do you need the specific color or do you need a brush you can use to draw the same thing? The latter is easy. The former is possible only in limited situations.

    You can get the brush used for the background from the TextBox.Background property, the same as you set it (but in reverse.

    The catch is that the brush may not be a solid color. It could be a gradient or an image. You can try to cast the brush to a SolidColorBrush, and if that succeeds then you can get the SolidColorBrush's Color property to get its A,R,G, and B values. There isn't a direct mapping from these values to

    --Rob

    Sunday, October 6, 2013 8:54 PM
    Moderator
  • Thanks Rob. I preset the textbox to a white color, and in the course of the app I might change the background to crimson, and will need to spot check the current color. But whichever color it is, I had set it previously so there'll only be a few possibilities. Any pointers appreciated.
    Sunday, October 6, 2013 10:10 PM
  • I'd keep track of the logical state in the model rather than tracking the color. You can bind the color shown to the state.

    Relying on hard coded colors in the interface will be difficult and limiting. It can badly in high contrast modes and make your app unusable.

    --Rob

    Monday, October 7, 2013 12:08 AM
    Moderator
  • When I try to inquire as the textbox's background, I get an error on the SolidColorBrush, as the syntax isn't correct.

    If TextBox1.Background <> SolidColorBrush(Colors.Crimson) Then

    "SolidColorBrush is a type and cannot be used as an expression"

    Monday, October 7, 2013 12:53 AM
  • Right. Background is a Brush, not a SolidColorBrush.

    Keying off the color is really the wrong approach. Keep track if the state that you use to determine the color in a property in your code class.

    Monday, October 7, 2013 1:13 AM
    Moderator
  • Hi Rob, where would I need to look for tracking a state and property in a code class? I am way behind in programming.
    Monday, October 7, 2013 2:04 AM
  • Hello David, 

    Try this for set background color :

    textbox1.Background = New SolidColorBrush(Windows.UI.Colors.Yellow)

    or

    txt1.Background = New SolidColorBrush(Windows.UI.Color.FromArgb(100, 133, 133, 124))

    or get background color of textbox : 

    Dim a As Windows.UI.Xaml.Media.SolidColorBrush
            a = txt1.Background
            Debug.WriteLine(a.Color)
    Monday, October 7, 2013 4:23 AM
  • Hi David,

    I'm out on my phone so I can't give a link or code, but look up data binding and properties in the docs. Essentially you'll set a class level variable to mark whatever changes the color. Internally the code just changes or checks this variable and doesn't care about the color. By making the variable a property the TextBox can bind its color to automatically draw appropriately for the property state.

    Somebody on a fuller system may be able to explain in more detail

     or point towards specific documentation.

    Monday, October 7, 2013 5:26 AM
    Moderator
  • <grid background="red">

    <textblock text="cool" />

    </grid> 

    try this

    Monday, October 7, 2013 9:35 AM