0

My code creates multiple buttons with a certain color. When I click a button I want it to change to a HEX color and it works well. The problem is that when I click in another button, I want the previously selected button to go back to the default color scheme.

 private void createButtons()
        {

            foreach (int axis_number in axis_sequence.number)
            {
                AxisSequence axis = new AxisSequence (axis_number); 
                axis.MouseLeftButtonDown += new MouseButtonEventHandler (axis_MouseLeftButtonDown);
                axis_sequence.Children.Add (axis);                           
            }
        }


void axis_MouseLeftButtonDown (object sender, MouseButtonEventArgs e)
    {

        var converter = new System.Windows.Media.BrushConverter();
        var brush_amob_gray = (System.Windows.Media.Brush)converter.ConvertFromString("#515151");
        var brush_amob_orange = (System.Windows.Media.Brush)converter.ConvertFromString("#FF8C00");

        ((AxisSequence)sender).change_canvas = brush_amob_orange;
        ((AxisSequence)sender).change_text_color = System.Windows.Media.Brushes.Black;

//When I click another button I wanted to all the others to the default Colors.

AXIS SEQUENCE:

public partial class AxisSequence : UserControl
{
    /// <summary>
    /// 
    /// </summary>
    /// <param name="numero_eixo"></param>
    public AxisSequence (int numero_eixo)
    {
        try
        {
            InitializeComponent();
            Value.Content = numero_eixo.ToString();

        }
        catch (System.Exception ex)
        {
            System.Windows.MessageBox.Show(ex.ToString());
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public int get_numero_eixo ()
    {
        return Int32.Parse (Value.Content.ToString());
    }

    /// <summary>
    /// 
    /// </summary>
    public System.Windows.Media.Brush change_canvas { get { return canvas.Background; } set { canvas.Background = value; } }

    /// <summary>
    /// 
    /// </summary>
    public System.Windows.Media.Brush change_text_color { get { return Value.Foreground; } set { Value.Foreground = value; } }
}
adiajdiadj
  • 41
  • 8
  • use `last focus` http://stackoverflow.com/questions/4428100/find-out-the-control-with-last-focus –  Apr 18 '17 at 08:51
  • if currClickedButton != null , set the color to default scheme. Then preserve the instance of the current button i.e. currClickedButton = (AxisSequence)sender. This change has to be done in axis_MouseLeftButtonDown() – Mukul Varshney Apr 18 '17 at 10:49

0 Answers0