在 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;
}
}
}
}
相关文章
在 C# 中将 List<string>转换为字符串
发布时间:2024/03/16 浏览次数:198 分类:编程语言
-
在 C# 中,有两种主要方法可用于将 List
转换为字符串变量,Linq 方法和 String.Join()函数。
在 C# 中将 List<string>转换为字符串
发布时间:2024/03/16 浏览次数:171 分类:编程语言
-
在 C# 中,有两种主要方法可用于将 List
转换为字符串变量,Linq 方法和 String.Join()函数。
在 C# 中将 List<string>转换为字符串
发布时间:2024/03/16 浏览次数:187 分类:编程语言
-
在 C# 中,有两种主要方法可用于将 List
转换为字符串变量,Linq 方法和 String.Join()函数。
在 C# 中发出 HTTP POST Web 请求
发布时间:2024/02/04 浏览次数:131 分类:编程语言
-
在 C# 中,可以使用 3 种主要方法来发出 HTTP POST Web 请求:WebClient 类,HttpWebRequest 类和 HttpClient 类。本教程将讨论在 C# 中发出 HTTP POST Web 请求的方法。使用 C# 中的 WebClient 类发出 HTTP POST Web 请求
在 C# 中运行命令提示符命令
发布时间:2024/02/04 浏览次数:130 分类:编程语言
-
Process 类可用于在 C# 中运行命令提示符命令。在 C# 中使用 Process.Start() 函数运行命令提示符命令
在 C# 中调整图像大小
发布时间:2024/02/04 浏览次数:203 分类:编程语言
-
有两种主要方法可用于在 C# 中调整图像的大小,Bitmap 类构造函数和 graphics.DrawImage()函数。在本教程中,我们将讨论在C#中调整图像大小的方法。我们将带您完成整个过程,从加载原始图像到保
在 C# 中下载图片
发布时间:2024/02/04 浏览次数:138 分类:编程语言
-
有 3 种主要方法可用于下载 C# 中的图片,WebClient.DownloadFile()函数,Bitmap 类和 Image.FromStream()函数。在 C# 中使用 WebClient 类下载图片 WebClient 类提供了用于向 C# 中的 URL 发送数据和从 URL 接收数据
在 C# 中使用秒表
发布时间:2024/02/04 浏览次数:139 分类:编程语言
-
我们可以使用 Stopwatch 类来计算 C# 中的经过时间。使用 C# 中的秒表类计算经过时间 Stopwatch 类在 C# 中准确测量经过的时间。
在 C# 中获取可执行路径
发布时间:2024/02/04 浏览次数:200 分类:编程语言
-
有 3 种主要方法可用于获取 C# 中程序的可执行路径,即 Assembly 类,AppDomain 类和 Path 类。本教程将介绍获取 C# 代码的可执行路径的方法。使用 C# 中的 Assembly 类获取可执行路径