سؤال

I am trying to get the bottom button to change within a column of buttons when the top button is clicked. once the top button is clicked the bottom button in the column should change colour and then when the top one is clicked again the next button in the column should change colour as well.

I'm trying to make something similar to how a column in connect four would work where the column fills as each counter is dropped in by a different player until the column is full.although in this case it is a column of blank buttons that are filling until the top button itself has changed colour.

the code below changes the colours of the bottom two buttons when the top button is clicked once. However what I want to happen is that when I click the top button the bottom button changes to red, then when it is clicked again the button that is one up from the bottom is changed to blue and so on and so forth until the top button itself has changed colour.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        bool player1 = true;
        bool player2 = false;

        private bool button1clicked = true;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (button1clicked == true && player1 == true)
            {
                button6.BackColor = Color.Red;

                player1 = false;
                player2 = true;

                button1clicked = false;
            }
            if (button1clicked == false && player2 == true)
            {
                button5.BackColor = Color.Blue;

                player2 = false;
                player1 = true;

                button1clicked = true;
            }

        }
    }
}`

image of what my code produces after one click of the top button: http://gyazo.com/af1636cd0b6ce5686e46f86e2ed186d1

thanks everyone :)

هل كانت مفيدة؟

المحلول

I would take a different approach, populate a dictionary with your buttons and desired colour, and take the fist occurrence which has the default back colour.

            // Populate dictionary
        Dictionary<Button, Color> myButtons = new Dictionary<Button, Color>();
        myButtons.Add(button1, Color.Red);
        myButtons.Add(button2, Color.Blue);
        myButtons.Add(button3, Color.Green);
        myButtons.Add(button4, Color.Purple);
        myButtons.Add(button5, Color.Pink);
        myButtons.Add(button6, Color.PaleVioletRed);

        // Get first untouched occurrence
        var buttonToBeChanged = myButtons.Reverse().FirstOrDefault(x => x.Key.BackColor.IsSystemColor);

        // No buttons left
        if (buttonToBeChanged.Key == null)
            MessageBox.Show("No buttons left!");
        else
            buttonToBeChanged.Key.BackColor = buttonToBeChanged.Value;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top