Quantcast
Channel: DaniWeb Solved Topics
Viewing all articles
Browse latest Browse all 587

Input string was not in a correct format. Tryparse not working

$
0
0

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

}

Viewing all articles
Browse latest Browse all 587

Trending Articles