如何在标签c上显示百分比#

时间:2014-10-09 17:02:40

标签: c# mysql winforms windows-store-apps

如何在Windows商店应用程序中显示文本块(标签)的百分比? 如何根据设置的值更新并在表单上显示百分比?

private void Button_add_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            int cls, atnd, prcent;

            cls = int.Parse(totClsInput.Text);
            atnd = int.Parse(attendedClsInput.Text);
            prcent = atnd * 100 / cls;
            attndPercentageInput.Text = prcent.ToString();

            string Query = @"INSERT INTO `bcasdb`.`tbl_attend`
                (`tbl_subject_sub_id`,
                `reg_id`,
                `total_classes`,
                `attend_classes`,
                `percentage`)
                VALUES ("
               + this.subIDInput.Text + ",'"
               + this.stdRegIdInput.Text + "','"
               + this.totClsInput + "','"
               + this.attendedClsInput + "','"
               + this.attndPercentageInput + "')";
            //This is command class which will handle the query and connection object.
            MySqlConnection conn = new MySqlConnection(BCASApp.DataModel.DB_CON.connection);
            MySqlCommand cmd = new MySqlCommand(Query, conn);
            MySqlDataReader MyReader;
            conn.Open();
            MyReader = cmd.ExecuteReader();// Here our query will be executed and data saved into the database.           
            conn.Close();
            successmsgBox();
        }
        catch (Exception)
        {
            errormsgBox();
        }
    }

1 个答案:

答案 0 :(得分:0)

如果您需要在执行添加操作之前更新标签,那么您的计算逻辑不应该在"添加操作"事件处理程序。

您可以为LostFocustotClsInput设置attendedClsInput(不确定在winforms中调用的确切内容)事件处理程序,然后调用计算百分比函数。像这样:

protected void totClsInput_LostFocus(object sender, EventArgs e)
{
    this.CalculatePercent();
}

protected void attendedClsInput_LostFocus(object sender, EventArgs e)
{
    this.CalculatePercent();
}

private void CalculatePercent()
{
    int cls, atnd, prcent;

    if (totClsInput.Text != string.Empty && attendedClsInput.Text != string.Empty)
    {

        cls = int.Parse(totClsInput.Text);
        atnd = int.Parse(attendedClsInput.Text);
        prcent = atnd * 100 / cls;
        attndPercentageInput.Text = prcent.ToString();

    }
}