方法

h22fl7wq  于 2021-06-20  发布在  Mysql
关注(0)|答案(0)|浏览(218)

在我的visual studio项目中,我通过foreach语句为表(mysql)上的每条记录创建一个按钮,每个按钮都是通过以下方法创建的:

void createRewardButton(int y, int rewardsAmount, string rewardName, int value)
    {
        int x = 200;

        //Create Button object
        Button rewardButton = new Button();

        ///Set button properties
        rewardButton.Name = "button"+rewardName;
        rewardButton.Height = 60;
        rewardButton.Width = 400;
        rewardButton.Location = new Point(x,y);
        rewardButton.Text = "You won "+ rewardsAmount + " awards for  " + rewardName + ". Value: " + value + " tokens";
        rewardButton.BackColor = Color.WhiteSmoke;
        rewardButton.FlatStyle = FlatStyle.Flat;

        //Add button to Awards Panel
        panel_Rewards.Controls.Add(rewardButton);

        ///Button event handlers
        rewardButton.MouseHover += new EventHandler(rewardButton_MouseEnter);
        rewardButton.MouseLeave += new EventHandler(rewardButton_MouseLeave);

        ///Event handler for the click method
        rewardButton.Click += new EventHandler((sender, e) => rewardButton_Click(sender, e, value));

        //Button controls 
        void rewardButton_MouseEnter(object sender, EventArgs e)
        {
            rewardButton.BackColor = Color.FromArgb(100, Color.Gray); ;

        }
        void rewardButton_MouseLeave(object sender, EventArgs e)
        {
            rewardButton.BackColor = Color.WhiteSmoke;

        }
        // On click control method
        void rewardButton_Click(object sender, EventArgs e, int rValue)
        {
            increaseTokens(rValue);
        }
    }

我希望每个按钮在每次单击时都执行increasetokens()方法,该方法需要一个int参数。我发现我可以使用lambda语法让事件处理程序使用“value”作为rewardbutton\u click事件的参数。
以下是increasetokens方法的代码:

void increaseTokens(int value)
    {
        ////Connection String//////////
        string connStr = "SERVER=localhost;" + "DATABASE=scores;" + "UID=root;" + "PASSWORD=password;";
        //Connection object
        MySqlConnection dbcon = new MySqlConnection(connStr);
        try
        {
            dbcon.Open();
        }
        catch (Exception err)
        {
            MessageBox.Show("Message: " + err.Message);
        }

        if (dbcon.State == ConnectionState.Open)
        {
            String query = "UPDATE agents SET agentTokens = @value WHERE agentEid =  @EID";
            //Create Command
            MySqlCommand cmd = new MySqlCommand(query, dbcon);
            cmd.Parameters.AddWithValue("EID", EID);
            cmd.Parameters.AddWithValue("value", value);
            cmd.ExecuteNonQuery();
        }

    }

按钮已创建,但单击按钮时数据库不会更新。由于没有收到任何错误或警报,我不知道rewardbutton\u click事件是否接受int参数并将其传递给increasetokens()方法。
任何关于如何修复的信息都将不胜感激。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题