Hi, I'm getting an exception "Input string was not in a correct format." This happens only when any of the textbox is left empty that means when i click radioCircle on the form and only enter Radius value and leave all other textboxes empty then i get that error. But if i fill all the other text boxes and then click on radioCircle then it don't give any error and gives correct result for radius of circle only.
Same is when i tried to disable txtL.ReadOnly = true; and txtW.ReadOnly = true; upon checking event of Circle radio it again gave the same exception that code of making textboxes ReadOnly is now commented you can see. i don't get it why its giving me error on being empty textboxes or on ReadOnly please if any one can explain in a bit detail thanks in advance. Code is given. I searched and found Tryparse but this ain't working for me here is the code.

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 menu
{
    public partial class Form1 : Form
    {
        private int exp ;
        public Form1()
        {
            InitializeComponent();
        }

    private void btnResult_Click(object sender, EventArgs e)
    {
        float length = float.Parse(txtL.Text);
        float.TryParse(txtL.Text, out length);
        float width = float.Parse(txtW.Text);
        float.TryParse(txtW.Text, out width);
        float radius = float.Parse(txtR.Text);
        float.TryParse(txtR.Text, out radius);
        float tribase= float.Parse(txtB.Text);
        float.TryParse(txtB.Text, out tribase);
        float height = float.Parse(txtH.Text);
        float.TryParse(txtH.Text, out height);
        float area = 0;
        switch(exp){

            case 1:
                area = 3.14f* radius * radius;
                txtResult.Text = area.ToString();
                break;
            case 2:
                area = length * width;
                txtResult.Text = area.ToString(); ![Capture.PNG](/attachments/large/4/1a8b682c42b641b47465d3d9f97009da.PNG "align-center") 
                break;
            case 3:
                area = (tribase * height) / 2;
                txtResult.Text = area.ToString();
                break;

        }
    }

    private void radioCircle_CheckedChanged(object sender, EventArgs e)
    {
            txtL.ReadOnly = true;
            txtW.ReadOnly = true;
            txtH.ReadOnly = true;
            txtB.ReadOnly = true;

            exp = 1;
    }

    private void radioRect_CheckedChanged(object sender, EventArgs e)
    {

            txtH.ReadOnly = true;
            txtB.ReadOnly = true;
            txtR.ReadOnly = true;

            exp = 2;
    }

    private void radioTriangle_CheckedChanged(object sender, EventArgs e)
    {
            txtR.ReadOnly = true;
            txtL.ReadOnly = true;
            txtW.ReadOnly = true;  

            exp = 3;
    }
}

}

Recommended Answers

All 4 Replies

Why are you doing all your parsings before your switch case on line 33?
When you press the result button, start with your switch statement, by then you know the value of exp (which I should give a better name btw) In your case only parse the textboxes needed. Tryparse works great when I use it, it even returns a boolean if the parsing succeeded. Use it!
EDIT: remove all the readonlys.

This:

    float length = float.Parse(txtL.Text);
    float.TryParse(txtL.Text, out length);
    float width = float.Parse(txtW.Text);
    float.TryParse(txtW.Text, out width);
    float radius = float.Parse(txtR.Text);
    float.TryParse(txtR.Text, out radius);
    float tribase= float.Parse(txtB.Text);
    float.TryParse(txtB.Text, out tribase);
    float height = float.Parse(txtH.Text);
    float.TryParse(txtH.Text, out height);

looks very problematic. You're calling Parse, then calling TryParse. The Parse calls are giving you the error. Change the assignments to just 0.0F, and it should work better:

    float length = 0.0F;
    float.TryParse(txtL.Text, out length);
    float width = 0.0F;
    float.TryParse(txtW.Text, out width);
    float radius = 0.0F;
    float.TryParse(txtR.Text, out radius);
    float tribase= 0.0F;
    float.TryParse(txtB.Text, out tribase);
    float height = 0.0F;
    float.TryParse(txtH.Text, out height);

Now any TextBoxes that don't have valid values, the appropriate variable will have 0.

Rewrote your code and it works. Hope you can still recognise it.
Changed float to double, the Math class uses double too.
My code is certainly not what they call the 'nec plus ultra' but I'm gonna leave other improvements up to you. Happy programming. :)

using System;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        // an enum is better then your 'int exp', what does 'int exp' stands for?
        enum Shape  
        {
            Circle, Rectangle, Triangle
        }

        Shape geometry;

        public Form1()
        {
            InitializeComponent();
            radioCircle.Select();
            geometry = Shape.Circle;
        }

        private void btnResult_Click(object sender, EventArgs e)
        {
            double area = 0.0;
            double length, width, radius, tribase, height = 0.0;

            switch (geometry)
            {
                case Shape.Circle:
                    if (double.TryParse(txtR.Text, out radius))
                    {
                        area = Math.PI * radius * radius;
                        txtResult.Text = area.ToString("F3"); // F3: 3 digits after decimal
                    }
                    else
                    {
                        MessageBox.Show("Wrong string format in radius textbox.");
                    }
                    break;
                case Shape.Rectangle:
                    if (double.TryParse(txtL.Text, out length) && double.TryParse(txtW.Text, out width))
                    {
                        area = length * width;
                        txtResult.Text = area.ToString("F3");
                    }
                    else
                    {
                        MessageBox.Show("Wrong string format in length or width textbox.");
                    }
                    break;
                case Shape.Triangle:
                    if (double.TryParse(txtB.Text, out tribase) && double.TryParse(txtH.Text, out height))
                    {
                        area = (tribase * height) / 2;
                        txtResult.Text = area.ToString("F3");
                    }
                    else
                    {
                        MessageBox.Show("Wrong string format in tribase or height textbox.");
                    }
                    break;
                default:
                    break;
            }
        }

        private void radioCircle_CheckedChanged(object sender, EventArgs e)
        {
            geometry = Shape.Circle;
        }

        private void radioRect_CheckedChanged(object sender, EventArgs e)
        {
            geometry = Shape.Rectangle;
        }

        private void radioTriangle_CheckedChanged(object sender, EventArgs e)
        {
            geometry = Shape.Triangle;
        }
    }
}

thank you all of you i'll re-write the code keeping in view these thoughts thanks once again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.