简体   繁体   中英

Count clicks of specific Button

I'm writing function for counting click of red background's button. This function is not working. I have 40 buttons with the same function for click "ButClick". But when I click any button (for example with green background) - counter counts. I want to do it only when I will click button with red background.

private void But_Click(object sender, RoutedEventArgs e)
{
    var column = Grid.GetColumn(sender as Button);
    var row = Grid.GetRow(sender as Button);
    LabelX.Content = column.ToString();
    LabelY.Content = row.ToString();

    if ((sender as Button).Background == new SolidColorBrush(Colors.Red));
    {
        counts += 1;
        LabelCounterOfClick.Content = counts.ToString();
    }

}

update

Now it's not counting anything. When i click any button- counter show 0. I thing something with function if is wrong(it not detect Red)

private void But_Click(object sender, RoutedEventArgs e) 
{ 
    var column = Grid.GetColumn(sender as Button); 
    var row = Grid.GetRow(sender as Button); 
    LabelX.Content = column.ToString(); 
    LabelY.Content = row.ToString();

    if ((sender as Button).Background == new SolidColorBrush(Colors.Red))
    {
        counts += 1;

    }
    else
    {
        counts = 0;
    }

    LabelCounterOfClick.Content = counts.ToString(); 
}

REst of Code.

namespace Silnik_Zmiany_koloru
{
    public partial class MainWindow : Window
    {
        public Random _random;
        public MainWindow()
        {
            InitializeComponent();
            DispatcherTimer dt = new DispatcherTimer();
            dt.Interval = TimeSpan.FromSeconds(1);
            dt.Tick += Dt_Tick;
            dt.Start();
        }

        private void Dt_Tick(object sender, EventArgs e)
        {
            Losowanie(); // zmiana kolorow (losowe)
        }
        int counts = 0;


        private void But0_0_Click(object sender, RoutedEventArgs e)
        {
            Losowanie();
        }

        private Random random = new Random();
        private Random random1 = new Random();

        int ilosc_kolorow = 3;
        private Color[] colors = new Color[]
               {
               Colors.Red,
               Colors.Blue,
               Colors.Green,
               };


        private void Losowanie()
        {
            for (int i = 0; i <= 6; i++)
            {
                for (int k = 3; k <= 10; k++)
                {
                    var ele = MainGrid.FindName("But" + i + "_" + random.Next(0, 8));
                    Button button = ele as Button;
                    if (button != null)
                    {
                        Change_color(button);
                    }
                }

            }
        }
    }
}

Try removing ; at the end of the line after if condition

and you can check the background with " Brushes.Red " instead of create new object

Why have you attached the ButClick event to all 40 buttons? Why not just add it to those red? Select properties on the button and assign click event to those that should have it.

Keep correction of AbduGo in mind.

Not the best solution but Try doing this,

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Button btn = (Button)sender;

        Brush btnBrush = btn.Background;
        string color = "";
        if (btnBrush is SolidColorBrush)
        {
            color = ((SolidColorBrush)btnBrush).Color.ToString();
        }

        if (color ==  Brushes.Red.ToString())
        {
           //your logic of incrementing counter
        }
        else
        {
           //your logic of resting counter
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM