c# - 在一个事件中设置 'int' 并在另一事件中使用它

标签 c# visual-studio if-statement int

我正在 Windows 窗体 (C# Visual Studio) 中制作一些东西 但我需要知道如何设置一个 int,然后在另一个事件中使用它。例如:

private void BtnYes_Click(object sender, EventArgs e)
{
    int yes = 1;
    int no = 0;
    timer1.Start();
}

private void timer1_Tick(object sender, EventArgs e) 
 {
    if (yes == 1) {
        //EVENT
 }
}

当我这样做时,我遇到了一些错误。谁能帮我这个?或者只是告诉我如何使用不同的技术来做这样的事情?

最佳答案

您需要为此使用一个字段:

private int _yes;
private void BtnYes_Click(object sender, EventArgs e)
{
    _yes = 1;
    int no = 0;
    timer1.Start();
}

private void timer1_Tick(object sender, EventArgs e) 
 {
    if (_yes == 1) {
        //EVENT
 }
}

关于c# - 在一个事件中设置 'int' 并在另一事件中使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32026591/

相关文章:

visual-studio - 未从引用的项目复制 NuGet 包

java - 如何检查一个方法是否不为空?

c - 否则,如果选项 3 没有产生答案……我做错了什么?

c# - 在标签中自动包含版本号

javascript - 如何将原始数据主体添加到 axios 请求?

c# - Visual Studio 调试器未附加

c - MS Visual Studio 编译器提供哪些 C99 功能?

matlab - 提取元胞数组中某个字符的索引

c# - 使用 Azure DocumentDB 的 MongoDB Indexes.CreateOneAsync 异常

c# - 告诉 HashSet 使用 IEquatable?