нужно реализовать C # Counter - PullRequest
0 голосов
/ 30 августа 2009

Я хочу сделать приращение и уменьшение счетчика. Есть две кнопки, называемые X и Y. Если сначала нажать X, а затем нажать Y, счетчик должен увеличиваться. Если сначала нажать Y, а затем нажать X, счетчик должен уменьшиться.

Я не знаком с c #. Так может кто-нибудь помочь мне, пожалуйста? (

Ответы [ 3 ]

3 голосов
/ 30 августа 2009

Похоже, вам нужны 2 переменные: счетчик и последняя нажатая кнопка. Я предполагаю, что это приложение WinForms, так как вы не указали в то время, когда я пишу это.

class MyForm : Form
{
    // From the designer code:
    Button btnX;
    Button btnY;

    void InitializeComponent()
    {
        ...
        btnX.Clicked += btnX_Clicked;
        btnY.Clicked += btnY_Clicked;
        ...
    }

    Button btnLastPressed = null;
    int counter = 0;

    void btnX_Clicked(object source, EventArgs e)
    {
        if (btnLastPressed == btnY)
        {
            // button Y was pressed first, so decrement the counter
            --counter;
            // reset the state for the next button press
            btnLastPressed = null;
        }
        else
        {
            btnLastPressed = btnX;
        }
    }

    void btnY_Clicked(object source, EventArgs e)
    {
        if (btnLastPressed == btnX)
        {
            // button X was pressed first, so increment the counter
            ++counter;
            // reset the state for the next button press
            btnLastPressed = null;
        }
        else
        {
            btnLastPressed = btnY;
        }
    }
}
1 голос
/ 30 августа 2009

Вы хотели бы иметь переменную, которую вы хотите отслеживать счетчик.

int counter = 0;

Если это веб-приложение, то вы должны хранить его где-то, например, состояние сеанса. затем в вашем счетчике приращений:

counter++;

и в вашем счетчике декремента сделайте это:

counter--;
0 голосов
/ 29 мая 2013

Нашли это на другом сайте:

public partial class Form1 : Form
{
        //create a global private integer 
        private int number;

        public Form1()
        {
            InitializeComponent();
            //Intialize the variable to 0
            number = 0;
            //Probably a good idea to intialize the label to 0 as well
            numberLabel.Text = number.ToString();
        }
        private void Xbutton_Click(object sender, EventArgs e)
        {
            //On a X Button click increment the number
            number++;
            //Update the label. Convert the number to a string
            numberLabel.Text = number.ToString();
        }
        private void Ybutton_Click(object sender, EventArgs e)
        {
            //If number is less than or equal to 0 pop up a message box
            if (number <= 0)
            {
                MessageBox.Show("Cannot decrement anymore. Value will be  
                                                negative");
            }
            else
            {
                //decrement the number
                number--;
                numberLabel.Text = number.ToString();
            }
        }
    }
...