Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add two values using tryparse

I have a program that calculates the area of a open field. I would like to understand how can I use the TryParse method to add two calculated values into a single text box called btnCalculate

Radiobutton when clicked, enables two textboxes E1 so that the user and input the values. These two textboxes are bounded in a groupbox.

I am wondering how can I add these two areas when button is clicked, one outside of the grpbox and the other within the grpbox using the TryParse method. Is it possible?

Thanks in advance.

For example:

    private void btnCalculate_Click(object sender, EventArgs e)
    {
            double width, length;
            bool status1 = double.TryParse(this.txbxWidth.Text, out width);
            bool status2 = double.TryParse(this.txbxLength.Text, out length);
            txbxArea.Text = string.Format("{0:f}", width * length);

        if (rdb1.Checked)
        {
            double e1width, e1length;
            bool status1 = double.TryParse(this.txbxE1width.Text, out e1width);
            bool status2 = double.TryParse(this.txbxE1length.Text, out e1length);
            txbxArea.Text = string.Format("{0:f}", e1width * e1length);

}

enter image description here

like image 908
Ralph Avatar asked Mar 28 '26 06:03

Ralph


1 Answers

TryParse has nothing to do with the addition, etc. It is just for parsing the values.

You can add two areas as follows:

 private void btnCalculate_Click(object sender, EventArgs e)
 {
        double width, length;
        bool status1 = double.TryParse(this.txbxWidth.Text, out width);
        bool status2 = double.TryParse(this.txbxLength.Text, out length);
        txbxArea.Text = string.Format("{0:f}", width * length);

        if (rdb1.Checked)
        {
            double e1width, e1length;
            bool status1 = double.TryParse(this.txbxE1width.Text, out e1width);
            bool status2 = double.TryParse(this.txbxE1length.Text, out e1length);
            txbxArea.Text = string.Format("{0:f}", (width * length) + (e1width * e1length));
        }
}
like image 160
Kashif Abdul Aleem Avatar answered Mar 29 '26 21:03

Kashif Abdul Aleem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!