用户控制错误:尝试除以零 [英] Error in User Control : Attempted to divide by zero

查看:146
本文介绍了用户控制错误:尝试除以零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将用户控件添加到我的表单中。用户控件中的代码是:

 

我尝试将用户控件添加到我的表单中。

我在User Control中的代码是:

private void DashBoard_Load(object sender,EventArgs e)
{
CheckLabel.Text = Settings.Default。 Checked.ToString();
DelLabel.Text = Settings.Default.Deleted.ToString();
TotalLabel.Text = Settings.Default.Counter.ToString();
double Percentage = Settings.Default.Checked / Settings.Default.Counter * 100;
PercentageLabel.Text = Percentage.ToString(" ##。##")+"%";
if(Settings.Default.Checked> 999)
{
CheckLabel.Font = new Font(" Helvetica LT Std",16,FontStyle.Bold);
CheckLabel.Location = new Point(291,160); ;
}
if(Settings.Default.Counter> 999)
{
TotalLabel.Font = new Font(" Helvetica LT Std",16,FontStyle.Bold);
TotalLabel.Location = new Point(426,160);
}
if(Settings.Default.Deleted> 999)
{
DelLabel.Font = new Font(" Helvetica LT Std",16,FontStyle.Bold);
DelLabel.Location = new Point(428,160);
}
if(Percentage> = 10)
{
PercentageLabel.Font = new Font(" Helvetica LT Std",22,FontStyle.Bold);
PercentageLabel.Location = new Point(56,160);
}
PerChart.Series [" Per"]。Points.AddXY(百分比,百分比);
int减去= 100 - Convert.ToInt32(百分比);
PerChart.Series [" Per"]。Points.AddXY(减,减);
if(Settings.Default.Theme == false)
{
DashBoardLabel.ForeColor = SystemColors.ControlDarkDark;
}
}
出现此错误。

正如你所看到的,我试图获得2个int变量的百分比(例如56/87 = 0.64367816091954022988505747126437 * 100 = 64.367816091954022988505747126437,我需要将其转换为int,然后转换为字符串。



解决方案

您好SebGM2018,


这是因为以下代码:

 double Percentage = Settings.Default.Checked /  Settings.Default.Counter  * 100; 

我猜您已将Settings.Default.Counter的值设置为0 在您的设置文件中,该字段不能为0。



P 租赁检查。


或更改您的代码to:

 private void UserControl1_Load(object sender,EventArgs e)
{
CheckLabel.Text = Settings.Default.Checked.ToString();
DelLabel.Text = Settings.Default.Deleted.ToString();
TotalLabel.Text = Settings.Default.Counter.ToString();

double百分比= 0;
if(Settings.Default.Counter!= 0)
{
Percentage = Settings.Default.Checked / Settings.Default.Counter * 100;
}

PercentageLabel.Text = Percentage.ToString(" ##。##")+"%" ;;
// ... ...
}

问候,


弗兰克


I tried to add a user control to my Form. My code in User Control is:

I tried to add a user control to my Form. My code in User Control is: private void DashBoard_Load(object sender, EventArgs e) { CheckLabel.Text = Settings.Default.Checked.ToString(); DelLabel.Text = Settings.Default.Deleted.ToString(); TotalLabel.Text = Settings.Default.Counter.ToString(); double Percentage = Settings.Default.Checked / Settings.Default.Counter * 100; PercentageLabel.Text = Percentage.ToString("##.##") + "%"; if (Settings.Default.Checked > 999) { CheckLabel.Font = new Font("Helvetica LT Std", 16, FontStyle.Bold); CheckLabel.Location = new Point(291, 160); ; } if (Settings.Default.Counter > 999) { TotalLabel.Font = new Font("Helvetica LT Std", 16, FontStyle.Bold); TotalLabel.Location = new Point(426, 160); } if (Settings.Default.Deleted > 999) { DelLabel.Font = new Font("Helvetica LT Std", 16, FontStyle.Bold); DelLabel.Location = new Point(428, 160); } if (Percentage >= 10) { PercentageLabel.Font = new Font("Helvetica LT Std", 22, FontStyle.Bold); PercentageLabel.Location = new Point(56, 160); } PerChart.Series["Per"].Points.AddXY(Percentage,Percentage); int Minus = 100 - Convert.ToInt32(Percentage); PerChart.Series["Per"].Points.AddXY(Minus, Minus); if (Settings.Default.Theme == false) { DashBoardLabel.ForeColor = SystemColors.ControlDarkDark; } } This Error Appeared.

As you can see, I was trying to get a percentage of 2 int variables (ex. 56/87 = 0.64367816091954022988505747126437 *100 = 64.367816091954022988505747126437, I need to convert it to int, and then to string.


解决方案

Hi SebGM2018,

That is because of the following code:

    double Percentage = Settings.Default.Checked / Settings.Default.Counter * 100;

I guess you have set the Settings.Default.Counter's value to 0 in your settings file, the filed can not be 0.

Please check it.

Or change your code to:

        private void UserControl1_Load(object sender, EventArgs e)
        {
            CheckLabel.Text = Settings.Default.Checked.ToString();
            DelLabel.Text = Settings.Default.Deleted.ToString();
            TotalLabel.Text = Settings.Default.Counter.ToString();

            double Percentage = 0;
            if (Settings.Default.Counter != 0)
            {
                Percentage = Settings.Default.Checked / Settings.Default.Counter * 100;
            }

            PercentageLabel.Text = Percentage.ToString("##.##") + "%";
            //...
        }

Regards,

Frankie


这篇关于用户控制错误:尝试除以零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆