C# Windows窗体C“;输入字符串的格式不正确";
我有一个简单的Windows窗体应用程序,用于计算二次方程的解。因为它需要将一些值输入到三个不同的文本框中,然后单击“计算”按钮,使用输入的值进行一些计算。在测试应用程序并在输入任何值之前单击“计算”按钮后,我得到一个C# Windows窗体C“;输入字符串的格式不正确";,c#,winforms,windows-forms-designer,C#,Winforms,Windows Forms Designer,我有一个简单的Windows窗体应用程序,用于计算二次方程的解。因为它需要将一些值输入到三个不同的文本框中,然后单击“计算”按钮,使用输入的值进行一些计算。在测试应用程序并在输入任何值之前单击“计算”按钮后,我得到一个输入字符串的格式不正确这是由于试图解析一个不存在的值。有没有办法避免这种情况?我试图构建一个基于按钮是否被点击,文本框中是否没有值的条件,以不做任何事情,但这并不奏效。以下是我的设计器代码: namespace QuadraticSolver { partial class
输入字符串的格式不正确
这是由于试图解析一个不存在的值。有没有办法避免这种情况?我试图构建一个基于按钮是否被点击,文本框中是否没有值的条件,以不做任何事情,但这并不奏效。以下是我的设计器代码:
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;
}
}
namespace QuadraticSolver
{
部分类二次解算器
{
///
///必需的设计器变量。
///
private System.ComponentModel.IContainer components=null;
///
///清理所有正在使用的资源。
///
///如果应释放托管资源,则为true;否则为false。
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
#区域Windows窗体设计器生成的代码
///
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
///
私有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();
这个.SuspendLayout();
//
//LBL提示
//
this.lblPrompt.AutoSize=true;
this.lblPrompt.Location=新系统图纸点(12,9);
this.lblPrompt.Name=“lblPrompt”;
this.lblPrompt.Size=新系统图纸尺寸(92,13);
this.lblPrompt.TabIndex=0;
this.lblPrompt.Text=“输入您的值”;
//
//lblA
//
this.lblA.AutoSize=true;
this.lblA.Location=新系统图纸点(12,49);
this.lblA.Name=“lblA”;
this.lblA.Size=新系统图纸尺寸(16,13);
this.lblA.TabIndex=1;
this.lblA.Text=“a:”;
//
//lblB
//
this.lblB.AutoSize=true;
this.lblB.Location=新系统图纸点(12,85);
this.lblB.Name=“lblB”;
this.lblB.Size=新系统图纸尺寸(16,13);
this.lblB.TabIndex=2;
this.lblB.Text=“b:”;
//
//lblC
//
this.lblC.AutoSize=true;
this.lblC.Location=新系统图纸点(12,122);
this.lblC.Name=“lblC”;
this.lblC.Size=新系统图纸尺寸(16,13);
this.lblC.TabIndex=3;
this.lblC.Text=“c:”;
//
//txtA
//
this.txtA.Location=新系统图纸点(34,46);
this.txtA.Name=“txtA”;
this.txtA.Size=新系统图纸尺寸(360,20);
this.txtA.TabIndex=4;
//
//txtB
//
this.txtB.Location=新系统图纸点(34,82);
this.txtB.Name=“txtB”;
this.txtB.Size=新系统图纸尺寸(360,20);
this.txtB.TabIndex=5;
//
//txtC
//
this.txtC.Location=新系统图纸点(34122);
this.txtC.Name=“txtC”;
this.txtC.Size=新系统图纸尺寸(360,20);
this.txtC.TabIndex=6;
//
//计算
//
this.btnCalculate.Location=新系统图点(175154);
this.btnCalculate.Name=“btnCalculate”;
this.btnCalculate.Size=新系统.图纸.尺寸(75,23);
this.btnCalculate.TabIndex=7;
this.btnCalculate.Text=“计算!”;
this.btnCalculate.UseVisualStyleBackColor=true;
this.btnCalculate.Click+=新建System.EventHandler(this.btnCalculate\u Click);
//
//LBL解决方案
//
this.lblSolutions.AutoSize=true;
this.lblSolutions.Location=新系统图点(31226);
this.lblSolutions.Name=“lblSolutions”;
this.lblSolutions.Size=新系统图纸尺寸(53,13);
this.lblSolutions.TabIndex=8;
this.lblSolutions.Text=“解决方案:”;
//
//TXT解决方案1
//
this.txtSolution1.Location=新系统图纸点(34242);
this.txtSolution1.Name=“txtSolution1”;
this.txtSolution1.ReadOnly=true;
this.txtSolution1.Size=新系统图纸尺寸(165,20);
this.txtSolution1.TabIndex=9;
//
//TXT解决方案2
//
this.txtSolution2.Location=新系统图点(222242);
this.txtSolution2.Name=“txtSolu
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();
}
}
}
}
if (txtA.Text == "" || txtB.Text == "" || txtC.Text == "")
{
string stringSol = "Please enter some values!";
txtSolution1.Text = stringSol;
txtSolution2.Text = stringSol;
return;
}
double userNumber = myNumUpDown.Value;
myNumUpDown.Minimum = 300;
myNumUpDown.Maximum = 500;
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();