Windows 窗体 C#“输入字符串的格式不正确”

发布于 2024-10-01 01:29:30 字数 10293 浏览 7 评论 0原文

我有一个简单的 Windows 窗体应用程序,用于计算二次方程的解。由于它需要将一些值输入到三个不同的文本框中,然后单击“计算”按钮,使用输入的值进行一些计算。在测试应用程序并在输入任何值之前单击“计算”按钮时,我得到一个输入字符串格式不正确这是由于尝试解析不存在的值。有什么办法可以避免这种情况吗?我尝试根据是否单击了按钮并且文本框中没有值来构造一个条件,以不执行任何操作,但这不太有效。这是我的设计器代码:

namespace QuadraticSolver
{
    partial class QuadraticSolver
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.lblPrompt = new System.Windows.Forms.Label();
            this.lblA = new System.Windows.Forms.Label();
            this.lblB = new System.Windows.Forms.Label();
            this.lblC = new System.Windows.Forms.Label();
            this.txtA = new System.Windows.Forms.TextBox();
            this.txtB = new System.Windows.Forms.TextBox();
            this.txtC = new System.Windows.Forms.TextBox();
            this.btnCalculate = new System.Windows.Forms.Button();
            this.lblSolutions = new System.Windows.Forms.Label();
            this.txtSolution1 = new System.Windows.Forms.TextBox();
            this.txtSolution2 = new System.Windows.Forms.TextBox();
            this.chkImaginary = new System.Windows.Forms.CheckBox();
            this.SuspendLayout();
            // 
            // lblPrompt
            // 
            this.lblPrompt.AutoSize = true;
            this.lblPrompt.Location = new System.Drawing.Point(12, 9);
            this.lblPrompt.Name = "lblPrompt";
            this.lblPrompt.Size = new System.Drawing.Size(92, 13);
            this.lblPrompt.TabIndex = 0;
            this.lblPrompt.Text = "Enter Your Values";
            // 
            // lblA
            // 
            this.lblA.AutoSize = true;
            this.lblA.Location = new System.Drawing.Point(12, 49);
            this.lblA.Name = "lblA";
            this.lblA.Size = new System.Drawing.Size(16, 13);
            this.lblA.TabIndex = 1;
            this.lblA.Text = "a:";
            // 
            // lblB
            // 
            this.lblB.AutoSize = true;
            this.lblB.Location = new System.Drawing.Point(12, 85);
            this.lblB.Name = "lblB";
            this.lblB.Size = new System.Drawing.Size(16, 13);
            this.lblB.TabIndex = 2;
            this.lblB.Text = "b:";
            // 
            // lblC
            // 
            this.lblC.AutoSize = true;
            this.lblC.Location = new System.Drawing.Point(12, 122);
            this.lblC.Name = "lblC";
            this.lblC.Size = new System.Drawing.Size(16, 13);
            this.lblC.TabIndex = 3;
            this.lblC.Text = "c:";
            // 
            // txtA
            // 
            this.txtA.Location = new System.Drawing.Point(34, 46);
            this.txtA.Name = "txtA";
            this.txtA.Size = new System.Drawing.Size(360, 20);
            this.txtA.TabIndex = 4;
            // 
            // txtB
            // 
            this.txtB.Location = new System.Drawing.Point(34, 82);
            this.txtB.Name = "txtB";
            this.txtB.Size = new System.Drawing.Size(360, 20);
            this.txtB.TabIndex = 5;
            // 
            // txtC
            // 
            this.txtC.Location = new System.Drawing.Point(34, 122);
            this.txtC.Name = "txtC";
            this.txtC.Size = new System.Drawing.Size(360, 20);
            this.txtC.TabIndex = 6;
            // 
            // btnCalculate
            // 
            this.btnCalculate.Location = new System.Drawing.Point(175, 154);
            this.btnCalculate.Name = "btnCalculate";
            this.btnCalculate.Size = new System.Drawing.Size(75, 23);
            this.btnCalculate.TabIndex = 7;
            this.btnCalculate.Text = "Calculate!";
            this.btnCalculate.UseVisualStyleBackColor = true;
            this.btnCalculate.Click += new System.EventHandler(this.btnCalculate_Click);
            // 
            // lblSolutions
            // 
            this.lblSolutions.AutoSize = true;
            this.lblSolutions.Location = new System.Drawing.Point(31, 226);
            this.lblSolutions.Name = "lblSolutions";
            this.lblSolutions.Size = new System.Drawing.Size(53, 13);
            this.lblSolutions.TabIndex = 8;
            this.lblSolutions.Text = "Solutions:";
            // 
            // txtSolution1
            // 
            this.txtSolution1.Location = new System.Drawing.Point(34, 242);
            this.txtSolution1.Name = "txtSolution1";
            this.txtSolution1.ReadOnly = true;
            this.txtSolution1.Size = new System.Drawing.Size(165, 20);
            this.txtSolution1.TabIndex = 9;
            // 
            // txtSolution2
            // 
            this.txtSolution2.Location = new System.Drawing.Point(222, 242);
            this.txtSolution2.Name = "txtSolution2";
            this.txtSolution2.ReadOnly = true;
            this.txtSolution2.Size = new System.Drawing.Size(172, 20);
            this.txtSolution2.TabIndex = 10;
            // 
            // chkImaginary
            // 
            this.chkImaginary.AutoSize = true;
            this.chkImaginary.Location = new System.Drawing.Point(33, 189);
            this.chkImaginary.Name = "chkImaginary";
            this.chkImaginary.Size = new System.Drawing.Size(71, 17);
            this.chkImaginary.TabIndex = 11;
            this.chkImaginary.Text = "Imaginary";
            this.chkImaginary.UseVisualStyleBackColor = true;
            // 
            // QuadraticSolver
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(406, 285);
            this.Controls.Add(this.chkImaginary);
            this.Controls.Add(this.txtSolution2);
            this.Controls.Add(this.txtSolution1);
            this.Controls.Add(this.lblSolutions);
            this.Controls.Add(this.btnCalculate);
            this.Controls.Add(this.txtC);
            this.Controls.Add(this.txtB);
            this.Controls.Add(this.txtA);
            this.Controls.Add(this.lblC);
            this.Controls.Add(this.lblB);
            this.Controls.Add(this.lblA);
            this.Controls.Add(this.lblPrompt);
            this.Name = "QuadraticSolver";
            this.Text = "Quadratic Solver";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label lblPrompt;
        private System.Windows.Forms.Label lblA;
        private System.Windows.Forms.Label lblB;
        private System.Windows.Forms.Label lblC;
        private System.Windows.Forms.TextBox txtA;
        private System.Windows.Forms.TextBox txtB;
        private System.Windows.Forms.TextBox txtC;
        private System.Windows.Forms.Button btnCalculate;
        private System.Windows.Forms.Label lblSolutions;
        private System.Windows.Forms.TextBox txtSolution1;
        private System.Windows.Forms.TextBox txtSolution2;
        private System.Windows.Forms.CheckBox chkImaginary;
    }
}

这是我的程序代码,它执行实际的数据操作:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

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

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            if (txtA.Text == "" || txtB.Text == "" || txtC.Text == "")
            {
                string stringSol = "Please enter some values!";
                txtSolution1.Text = stringSol;
                txtSolution2.Text = stringSol;
            }
            double aValue = double.Parse(txtA.Text);
            double bValue = double.Parse(txtB.Text);
            double cValue = double.Parse(txtC.Text);

            double solution1Double, solution2Double;

            //Quadratic Formula: x = (-b +- sqrt(b^2 - 4ac)) / 2a

            //Calculate discriminant
            double insideSquareRoot = (bValue * bValue) - 4 * aValue * cValue;

            if (insideSquareRoot < 0)
            {
                //No real solution
                solution1Double = Double.NaN;
                solution2Double = Double.NaN;

                txtSolution1.Text = solution1Double.ToString();
                txtSolution2.Text = solution2Double.ToString();
            }
            else if (insideSquareRoot == 0)
            {
                //One real solution
                double sqrtOneSolution = Math.Sqrt(insideSquareRoot);
                solution1Double = (-bValue + sqrtOneSolution) / (2 * aValue);
                solution2Double = double.NaN;

                txtSolution1.Text = solution1Double.ToString();
                txtSolution2.Text = solution2Double.ToString();
            }
            else if (insideSquareRoot > 0)
            {
                //Two real solutions
                double sqrtTwoSolutions = Math.Sqrt(insideSquareRoot);
                solution1Double = (-bValue + sqrtTwoSolutions) / (2 * aValue);
                solution2Double = (-bValue - sqrtTwoSolutions) / (2 * aValue);

                txtSolution1.Text = solution1Double.ToString();
                txtSolution2.Text = solution2Double.ToString();
            }
        }
    }
}

I have a simple Windows Forms application which is used to calculate the solutions to a quadratic equation. Since it requires some values to be inputted into three different textboxes and then upon clicking a "Calculate" button, makes some calculations with the inputted values. Upon testing the application, and clicking the "Calculate" button prior to inputting any values, I get a Input string was not in a correct format This is due to trying to parse a non-existent value. Is there any way to avoid this? I tried to construct a conditional based on if the button was clicked and there was no values in the textboxes, to not do anything, but that didn't quite work. Here is my designer code:

namespace QuadraticSolver
{
    partial class QuadraticSolver
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.lblPrompt = new System.Windows.Forms.Label();
            this.lblA = new System.Windows.Forms.Label();
            this.lblB = new System.Windows.Forms.Label();
            this.lblC = new System.Windows.Forms.Label();
            this.txtA = new System.Windows.Forms.TextBox();
            this.txtB = new System.Windows.Forms.TextBox();
            this.txtC = new System.Windows.Forms.TextBox();
            this.btnCalculate = new System.Windows.Forms.Button();
            this.lblSolutions = new System.Windows.Forms.Label();
            this.txtSolution1 = new System.Windows.Forms.TextBox();
            this.txtSolution2 = new System.Windows.Forms.TextBox();
            this.chkImaginary = new System.Windows.Forms.CheckBox();
            this.SuspendLayout();
            // 
            // lblPrompt
            // 
            this.lblPrompt.AutoSize = true;
            this.lblPrompt.Location = new System.Drawing.Point(12, 9);
            this.lblPrompt.Name = "lblPrompt";
            this.lblPrompt.Size = new System.Drawing.Size(92, 13);
            this.lblPrompt.TabIndex = 0;
            this.lblPrompt.Text = "Enter Your Values";
            // 
            // lblA
            // 
            this.lblA.AutoSize = true;
            this.lblA.Location = new System.Drawing.Point(12, 49);
            this.lblA.Name = "lblA";
            this.lblA.Size = new System.Drawing.Size(16, 13);
            this.lblA.TabIndex = 1;
            this.lblA.Text = "a:";
            // 
            // lblB
            // 
            this.lblB.AutoSize = true;
            this.lblB.Location = new System.Drawing.Point(12, 85);
            this.lblB.Name = "lblB";
            this.lblB.Size = new System.Drawing.Size(16, 13);
            this.lblB.TabIndex = 2;
            this.lblB.Text = "b:";
            // 
            // lblC
            // 
            this.lblC.AutoSize = true;
            this.lblC.Location = new System.Drawing.Point(12, 122);
            this.lblC.Name = "lblC";
            this.lblC.Size = new System.Drawing.Size(16, 13);
            this.lblC.TabIndex = 3;
            this.lblC.Text = "c:";
            // 
            // txtA
            // 
            this.txtA.Location = new System.Drawing.Point(34, 46);
            this.txtA.Name = "txtA";
            this.txtA.Size = new System.Drawing.Size(360, 20);
            this.txtA.TabIndex = 4;
            // 
            // txtB
            // 
            this.txtB.Location = new System.Drawing.Point(34, 82);
            this.txtB.Name = "txtB";
            this.txtB.Size = new System.Drawing.Size(360, 20);
            this.txtB.TabIndex = 5;
            // 
            // txtC
            // 
            this.txtC.Location = new System.Drawing.Point(34, 122);
            this.txtC.Name = "txtC";
            this.txtC.Size = new System.Drawing.Size(360, 20);
            this.txtC.TabIndex = 6;
            // 
            // btnCalculate
            // 
            this.btnCalculate.Location = new System.Drawing.Point(175, 154);
            this.btnCalculate.Name = "btnCalculate";
            this.btnCalculate.Size = new System.Drawing.Size(75, 23);
            this.btnCalculate.TabIndex = 7;
            this.btnCalculate.Text = "Calculate!";
            this.btnCalculate.UseVisualStyleBackColor = true;
            this.btnCalculate.Click += new System.EventHandler(this.btnCalculate_Click);
            // 
            // lblSolutions
            // 
            this.lblSolutions.AutoSize = true;
            this.lblSolutions.Location = new System.Drawing.Point(31, 226);
            this.lblSolutions.Name = "lblSolutions";
            this.lblSolutions.Size = new System.Drawing.Size(53, 13);
            this.lblSolutions.TabIndex = 8;
            this.lblSolutions.Text = "Solutions:";
            // 
            // txtSolution1
            // 
            this.txtSolution1.Location = new System.Drawing.Point(34, 242);
            this.txtSolution1.Name = "txtSolution1";
            this.txtSolution1.ReadOnly = true;
            this.txtSolution1.Size = new System.Drawing.Size(165, 20);
            this.txtSolution1.TabIndex = 9;
            // 
            // txtSolution2
            // 
            this.txtSolution2.Location = new System.Drawing.Point(222, 242);
            this.txtSolution2.Name = "txtSolution2";
            this.txtSolution2.ReadOnly = true;
            this.txtSolution2.Size = new System.Drawing.Size(172, 20);
            this.txtSolution2.TabIndex = 10;
            // 
            // chkImaginary
            // 
            this.chkImaginary.AutoSize = true;
            this.chkImaginary.Location = new System.Drawing.Point(33, 189);
            this.chkImaginary.Name = "chkImaginary";
            this.chkImaginary.Size = new System.Drawing.Size(71, 17);
            this.chkImaginary.TabIndex = 11;
            this.chkImaginary.Text = "Imaginary";
            this.chkImaginary.UseVisualStyleBackColor = true;
            // 
            // QuadraticSolver
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(406, 285);
            this.Controls.Add(this.chkImaginary);
            this.Controls.Add(this.txtSolution2);
            this.Controls.Add(this.txtSolution1);
            this.Controls.Add(this.lblSolutions);
            this.Controls.Add(this.btnCalculate);
            this.Controls.Add(this.txtC);
            this.Controls.Add(this.txtB);
            this.Controls.Add(this.txtA);
            this.Controls.Add(this.lblC);
            this.Controls.Add(this.lblB);
            this.Controls.Add(this.lblA);
            this.Controls.Add(this.lblPrompt);
            this.Name = "QuadraticSolver";
            this.Text = "Quadratic Solver";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label lblPrompt;
        private System.Windows.Forms.Label lblA;
        private System.Windows.Forms.Label lblB;
        private System.Windows.Forms.Label lblC;
        private System.Windows.Forms.TextBox txtA;
        private System.Windows.Forms.TextBox txtB;
        private System.Windows.Forms.TextBox txtC;
        private System.Windows.Forms.Button btnCalculate;
        private System.Windows.Forms.Label lblSolutions;
        private System.Windows.Forms.TextBox txtSolution1;
        private System.Windows.Forms.TextBox txtSolution2;
        private System.Windows.Forms.CheckBox chkImaginary;
    }
}

Here is my program's code which does the actual data manipulation:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

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

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            if (txtA.Text == "" || txtB.Text == "" || txtC.Text == "")
            {
                string stringSol = "Please enter some values!";
                txtSolution1.Text = stringSol;
                txtSolution2.Text = stringSol;
            }
            double aValue = double.Parse(txtA.Text);
            double bValue = double.Parse(txtB.Text);
            double cValue = double.Parse(txtC.Text);

            double solution1Double, solution2Double;

            //Quadratic Formula: x = (-b +- sqrt(b^2 - 4ac)) / 2a

            //Calculate discriminant
            double insideSquareRoot = (bValue * bValue) - 4 * aValue * cValue;

            if (insideSquareRoot < 0)
            {
                //No real solution
                solution1Double = Double.NaN;
                solution2Double = Double.NaN;

                txtSolution1.Text = solution1Double.ToString();
                txtSolution2.Text = solution2Double.ToString();
            }
            else if (insideSquareRoot == 0)
            {
                //One real solution
                double sqrtOneSolution = Math.Sqrt(insideSquareRoot);
                solution1Double = (-bValue + sqrtOneSolution) / (2 * aValue);
                solution2Double = double.NaN;

                txtSolution1.Text = solution1Double.ToString();
                txtSolution2.Text = solution2Double.ToString();
            }
            else if (insideSquareRoot > 0)
            {
                //Two real solutions
                double sqrtTwoSolutions = Math.Sqrt(insideSquareRoot);
                solution1Double = (-bValue + sqrtTwoSolutions) / (2 * aValue);
                solution2Double = (-bValue - sqrtTwoSolutions) / (2 * aValue);

                txtSolution1.Text = solution1Double.ToString();
                txtSolution2.Text = solution2Double.ToString();
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

忘你却要生生世世 2024-10-08 01:29:30

您可以使用 double.TryParse< /a> 方法。

这比检查 == "" 更好,因为它会告诉您不能将“Hello”解析为双精度值。

如果 TryParse 之一返回 false,您还需要从事件处理程序返回。

You can use the double.TryParse method.

This is better than checking for == "" since it will tell you that you can not parse "Hello" as a double too.

You also need to return from the event handler if one of the TryParse returns false.

牵强ㄟ 2024-10-08 01:29:30

您可以考虑使用 NumericUpDown 控件。您可以保证该控件的输入将是数字。所需的所有代码如下:

double userNumber = myNumUpDown.Value;

您甚至可以限制数字输入,以确保它落在您定义的范围内

myNumUpDown.Minimum = 300;
myNumUpDown.Maximum = 500;

以及许多其他内容。最后,它甚至还配备了向上/向下 GUI 控件,因此您的用户可以非常懒惰地用鼠标输入数字!

替代文本

You may consider using a NumericUpDown control instead. You are guaranteed the input from this control will be numeric. All the code that is required is then:

double userNumber = myNumUpDown.Value;

You can even restrict the number input to ensure it falls within your defined range

myNumUpDown.Minimum = 300;
myNumUpDown.Maximum = 500;

Along with many other things. Finally, it even comes with up/down GUI controls so your users can be super lazy and enter the number with their mouse!

alt text

橘虞初梦 2024-10-08 01:29:30

你可以试试这个格式,
将 textbox1 替换为您的文本框名称。

int a = textbox1.Text != "" ? Convert.ToInt32(textbox1.Text) : 0;
int b = textbox2.Text != "" ? Convert.ToInt32(textbox2.Text) : 0;
int total = a + b;

您可以这样重新分配结果:

textbox3 = total.toString();

You can try this format,
replace the textbox1 with your textbox name.

int a = textbox1.Text != "" ? Convert.ToInt32(textbox1.Text) : 0;
int b = textbox2.Text != "" ? Convert.ToInt32(textbox2.Text) : 0;
int total = a + b;

you can reassign the result this way:

textbox3 = total.toString();
请别遗忘我 2024-10-08 01:29:30

if (txtA.Text == "" || txtB.Text == "" || txtC.Text == "")
            {
                string stringSol = "Please enter some values!";
                txtSolution1.Text = stringSol;
                txtSolution2.Text = stringSol;
                return;
            }


if (txtA.Text == "" || txtB.Text == "" || txtC.Text == "")
            {
                string stringSol = "Please enter some values!";
                txtSolution1.Text = stringSol;
                txtSolution2.Text = stringSol;
                return;
            }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文