简体   繁体   English

我要向我的 windows forms C# 程序添加什么错误消息?

[英]What error messaging do i add to my windows forms C# program?

My program is a prime numbers C# program and I need error messaging that shows what happens when I add letters and/or larger numbers first in the textboxes.我的程序是一个素数 C# 程序,我需要错误消息来显示当我首先在文本框中添加字母和/或更大的数字时会发生什么。 Here is my code:这是我的代码:

namespace Task_2_Prime_Numbers
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btcalculate_Click(object sender, EventArgs e)
        {
            // Find all Prime numbers between the first and last prime numbers
            int firstnum = Convert.ToInt32(number1.Text);
            int lastnum = Convert.ToInt32(number2.Text);

            IbPrime.Items.Clear();

            // See which numbers are factors and add them to the list box
            for (int i = firstnum; i <= lastnum; i++)
            {
                if (IsPrime(i))
                {
                    IbPrime.Items.Add(i);
                }

            }
        }
        private bool IsPrime(int num)
        {
            if (num < 2)
                return false;
            // Looks for a number that evenly divides the sum
            for (int i = 2; i <= num / 2; i++)
                if (num % i == 0)
                    return false;

            return true;
        }
    }
}

Use int.TryParse .使用int.TryParse

int firstnum, lastnum;
if (!int.TryParse(number1.Text, out firstnum)){
    // Error
}
if (!int.TryParse(number2.Text, out lastnum)){
    // Error
}
if (firstnum >= lastnum){
    // Error
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为c#Windows Forms应用程序添加访问权限? - How do I add access rights to my c# windows forms application? C#:如何在程序中添加对“/?”的响应 - C#: How do I add a response to “/?” in my program 我应该在C#程序中使用.NET Framework 3.5检测Windows 8.1的正确操作系统版本? - What should I do in my C# program to detect the correct OS version for Windows 8.1, using .NET Framework 3.5? C#windows窗体程序中的“指定的强制转换无效”错误 - “Specified cast is not valid” error in C# windows forms program 如何在Windows窗体中添加多个文本框的值。 C#,Visual Studios - How do I add values of multiple text box in windows forms. C#, Visual Studios 如何将oAuth(DevDefined oAuth)添加到C#Windows Forms项目中? - How do I add oAuth (DevDefined oAuth) to a C# windows forms project? 如何将列表框中的数据添加到C#Windows窗体的SQL Server数据库中? - How do I add data from ListBox into SQL Server database in C# Windows forms? 如何在 C# Windows 窗体中的原始窗体前面启动新窗体? - How do I launch a new form in front of my original form in C# Windows Forms? 如何退出Windows Forms / C#程序 - How to exit a Windows Forms/C# program 安装C#程序后,在Windows 7 64bit上出现错误 - i get error on Windows 7 64bit after i installed my C# program
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM