Tuesday, April 12, 2011

Problem Assigning Button, Background Property In Windows Phone 7

I'm creating a calculator program. One of the buttons on the calculator, toggles two sets of keys across the 1st row of buttons. E.g. by default, the 1st row of buttons display the following function keys (in dark gray):

x^2, x^n, sqrt, pi, (, )

When I press the toggle button, the 1st row of buttons display the following function keys (in green):

FRAC, %, P->R, R->P, nPr, nCr

Pressing the toggle key further, toggles both sets of function keys across the 1st row of buttons.

To achieve the above, I essentially assign each button's Content and Background properties, their respective display text and color. The above works fine in my code if I click on the toggle button several times. If I click any button on the calculator keyboard, except one in the top row, then press the toggle button, my code works fine as well. However, if I click any button in the top row, then click the toggle button, the background color of the top buttons unpredictably display one of the two colors I assign (e.g. green, when it should display gray).

Below is a simplified version of my code which also shows the problem. (The example code uses background colors green and black, instead of green and dark gray.)

   public partial class MainPage : PhoneApplicationPage
    {
        private bool SciCalcFirstKeySetDisplayed_bln = true;
       
        private void ToggleScientificCalcButtons()
        {
            if (SciCalcFirstKeySetDisplayed_bln)
            {
  // Buttons at top of screen
                Button_0_0_btn.Content = "FRAC";
                Button_0_0_btn.Background = new SolidColorBrush(Colors.Green);
                Button_0_1_btn.Content = "%";
                Button_0_2_btn.Content = "P" + "\u2192" + "R";
                Button_0_2_btn.Background = new SolidColorBrush(Colors.Green);
                Button_0_3_btn.Content = "R" + "\u2192" + "P";
                Button_0_3_btn.Background = new SolidColorBrush(Colors.Green);
                Button_0_4_btn.Content = "nPr";
                Button_0_4_btn.Background = new SolidColorBrush(Colors.Green);
                Button_0_5_btn.Content = "nCr";
                Button_0_5_btn.Background = new SolidColorBrush(Colors.Green);
  // Toggle button
                Button_3_5_btn.Content = "1st";
            }
            else
            {
   // Buttons at top of screen
               Button_0_0_btn.Content = "x" + "\u00B2";
                Button_0_0_btn.Background = new SolidColorBrush(Colors.Black);
                Button_0_1_btn.Content = "x" + "\u207F";
                Button_0_2_btn.Content = "\u221A";
                Button_0_2_btn.Background = new SolidColorBrush(Colors.Black);
                Button_0_3_btn.Content = "\u03c0";
                Button_0_3_btn.Background = new SolidColorBrush(Colors.Black);
                Button_0_4_btn.Content = "(";
                Button_0_4_btn.Background = new SolidColorBrush(Colors.Black);
                Button_0_5_btn.Content = ")";
                Button_0_5_btn.Background = new SolidColorBrush(Colors.Black);
   // Toggle button
               Button_3_5_btn.Content = "2nd";
            }
            SciCalcFirstKeySetDisplayed_bln = !SciCalcFirstKeySetDisplayed_bln;
        }

 // Click Event Handler for toggle button
        private void Button_3_5_btn_Click(object sender, RoutedEventArgs e)
        {
            ToggleScientificCalcButtons();
        }
    }

I appreciate any help someone can provide me.

No comments:

Post a Comment