Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3405590
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:31:26+00:00 2026-05-18T05:31:26+00:00

I have a simple Windows Forms application which is used to calculate the solutions

  • 0

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();
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T05:31:27+00:00Added an answer on May 18, 2026 at 5:31 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple Windows Forms application which binds a DataView to a ListBox.
I have a windows forms application and which provides a way to search the
I have a simple HTML (as HTA) application that shows strange behavior on Windows
I'm creating a simple Windows Forms application with NHibernate and I'm a bit confused
Ok So I'm creating a Windows Forms Application for some guy using C# and
I have the following projects: MVC Console application Class library Windows forms application COM
I have a simple .NET 2.0 windows form app that runs off of a
I have a binary file - Windows static library (*.lib). Is there a simple
I have a PropertyGrid in my application that is used for editing arbitrary objects.
At work we've got a very CPU-intensive Windows Forms application. It's running on a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.