在 C# 中實現計數器

Haider Ali 2023年10月12日
在 C# 中實現計數器

在本指南中,我們將瞭解如何使用兩個按鈕在 C# 中建立遞增和遞減計數器。例如,如果有兩個按鈕,A 和 B。

按 A 和 B 應該增加計數器,按 A 和 B 應該分別減少計數器。讓我們深入研究本指南並學習自己。

C# 中實現計數器

在這個問題中,我們需要兩個變數。首先,我們需要一個計數器變數,其次,我們需要一個檢查最後按下的按鈕。

讓我們看看我們將如何做到這一點。首先,我們需要建立事件委託,因此每當單擊按鈕時都會發生特定事件。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace counter {
  class MyForm : Form {
    // Window  From Code
    Button buttonA;
    Button buttonB;

    void InitializeComponent() {
      buttonA.Clicked += Button_A_Click;  // btn_Clicked Event.. delgates..
      buttonB.Clicked += Button_B_Click;
    }
  }

}

在上面的程式碼中,我們製作了兩個按鈕並分配了事件委託。每當單擊 buttonA 時,函式 Button_A_Click 將執行。

接下來,我們需要有兩個變數。看一看。

Button LastPressed = null;  // Track which button was last pressed.
int counter = 0;            // initializing Counter Value

LastPressed 將跟蹤最後按下的按鈕,計數器將跟蹤遞增和遞減。接下來我們需要有兩個函式來跟蹤這一切。

void Button_A_Click(object source, EventArgs e) {
  if (LastPressed == buttonB)  // verifying which button is being pressed.
  {
    // button B was pressed first, so decrement the counter
    --counter;  // decrementing...
    // reset state for the next button press
    LastPressed = null;  // again button set to null for next tracking..
  } else {
    LastPressed = buttonA;  // assging which button was pressed.
  }
}

void Button_B_Click(object source, EventArgs e) {
  if (LastPressed == buttonA) {
    // buttonA was pressed 1st, so increment the counter
    ++counter;
    // reset state for the next button press
    LastPressed = null;
  } else {
    LastPressed = buttonB;
  }
}

如果點選 buttonA,程式將檢查 LastPressed 中的值。如果是 buttonB,計數器將遞減;如果沒有,buttonA 將被分配給 LastPressed

buttonB 也是如此;如果 LastPressed 是 A,它將增加計數器;否則,將分配 buttonB。這就是你如何在 C# 中編寫一個遞增和遞減計數器的程式。

以下是完整程式碼。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace counter {
  class MyForm : Form {
    // Window  From Code
    Button buttonA;
    Button buttonB;

    void InitializeComponent() {
      buttonA.Clicked += Button_A_Click;  // btnB_Clicked Event.. delgates..
      buttonB.Clicked += Button_B_Click;
    }

    Button LastPressed = null;  // Track which button was last pressed.
    int counter = 0;            // initializing Counter Value

    void Button_A_Click(object source, EventArgs e) {
      if (LastPressed == buttonB)  // verifying which button is being pressed.
      {
        // button B was pressed first, so decrement the counter
        --counter;  // decrementing...
        // reset state for the next button press
        LastPressed = null;  // again button set to null for next tracking..
      } else {
        LastPressed = buttonA;  // assging which button was pressed.
      }
    }

    void Button_B_Click(object source, EventArgs e) {
      if (LastPressed == buttonA) {
        // buttonA was pressed 1st, so increment the counter
        ++counter;
        // reset state for the next button press
        LastPressed = null;
      } else {
        LastPressed = buttonB;
      }
    }
  }

}
作者: Haider Ali
Haider Ali avatar Haider Ali avatar

Haider specializes in technical writing. He has a solid background in computer science that allows him to create engaging, original, and compelling technical tutorials. In his free time, he enjoys adding new skills to his repertoire and watching Netflix.

LinkedIn