C#单击按钮时向变量添加值

时间:2013-12-10 15:28:57

标签: c#

所以,呵呵,我对编程总体上有点新意,我有一个问题,也许你可以帮助我。我不知道我是否做得对,但希望你能在这里给我一个亮点。

public partial class Form1 : Form
{
    private int tick;
    private int conta_jogadas;
    private string[,] grelha = new string[3,3];



    public Form1()
    {
        InitializeComponent();
    }

    private void pos00_button_Click(object sender, EventArgs e)
    {
        if (tick == 0)
        {
            pos00_button.Text = "X";
            tick = 1;

        }
        else if (tick == 1)
        {
            pos00_button.Text = "O";
            tick = 0;
        }

        grelha[0, 0] = pos00_button.Text;
        conta_jogadas++;
    }

要验证我有:

private void Form1_Load(object sender, EventArgs e)
    {
        if (conta_jogadas == 1)
        {
            MessageBox.Show("Teste");
        }

    }

目前我只想让我的按钮为我的变量添加1。如果我点击它3次,我的conta_jogadas将是= 3.我想我在这里很清楚,不知道我还能添加什么来帮助。好吧,先谢谢你们。

2 个答案:

答案 0 :(得分:0)

此功能

private void Form1_Load(object sender, EventArgs e)
{
    if (conta_jogadas == 1)
    {
        MessageBox.Show("Teste");
    }

}

当表单加载时,将执行一次。那时变量conta_jogadas将具有值0int的默认值),因此您将看不到您的消息框。

如果您希望在值变为conta_jogadas

时看到消息框,则可以将此更改为您在更新1时调用的普通函数

答案 1 :(得分:0)

  

目前我只想让我的按钮为我的变量添加1。如果我点击它3次,我的conta_jogadas将是= 3。

试试这个:

public partial class Form1 : Form
{
    private int tick;
    private int conta_jogadas;
    private string[,] grelha = new string[3,3];

    public Form1()
    {
         InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        if (conta_jogadas == 1)
        {
           MessageBox.Show("Teste");
        }
    }

    private void pos00_button_Click(object sender, EventArgs e)
    {
        conta_jogadas++;
        grelha[0, 0] = pos00_button.Text;
    }
} 

每次按钮:pos00被点击,变量conta_jogadas被添加到当前数字。

注意:无法编译,因为我没有安装Visual Studio ...