c# - Windows 窗体 C# "Input string was not in a correct format"

标签 c# winforms windows-forms-designer

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

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();
            }
        }
    }
}

最佳答案

您可以使用 double.TryParse方法。

这比检查 == "" 更好,因为它会告诉您您也不能将 "Hello"解析为 double。

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

关于c# - Windows 窗体 C# "Input string was not in a correct format",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4119883/

相关文章:

c# - 自动展开 treeViewItem

c# - 新项目的 Linq to SQL

c# - 在 C# 中调用父窗体中的方法和访问父窗体中的 gui 元素的最佳实践

c# - 如何创建一个可以在其中放置其他控件的 UserControl?

c# - 什么事情会触发 Visual Studio 重新生成 *.Designer.cs 文件?

c# - 创建自定义设计时环境

c# - 使用 ReaderWriterLockSlim 调试死锁

c# - 使用c#测试smtp邮件服务器的可用性

c# - 是否可以重载 ShowDialog() 以便子窗体将信息作为输出参数返回?

c# - Windows 窗体图表更改轴间隔的问题