Question Having problems throwing an acception when zero is entered in the form for math

AdeptProgrammer

New member
Joined
Nov 5, 2015
Messages
2
Programming Experience
Beginner
This is for class, and I do not expect the answer, just pointed in the right direction. The code I have written is correct according to the current point in the class, but I just can not get an exception thrown into the lblResponse.txt when zero's have been entered. If a zero has been entered, then the same message should be displayed when....

while (int.TryParse(txtFirstNum.Text, out first) == false)

is thrown.

Here is my code, where the loop does work if non-numeric values are entered, which invokes this lblResponse.Text = "Value must be numeric and > 0";

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace AddMultiplyApp
{
public partial class AddMultiplyForm : Form
{
public AddMultiplyForm()
{
InitializeComponent();
}


private void Form1_Load(object sender, EventArgs e)
{


}


private void btnAdd_Click(object sender, EventArgs e)
{


int first,
second,
addAnswer;




while (int.TryParse(txtFirstNum.Text, out first) == false)
{

this.lblResponse.Visible = true;
this.lblResponse.Font = new System.Drawing.Font("Times New Roman", 14.25F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lblResponse.ForeColor = System.Drawing.Color.FromArgb
(((int) (((byte) (253)))), ((int) (((byte) (0)))), ((int) (((byte) (0)))));
this.lblResponse.Location = new System.Drawing.Point(255, 249);
this.lblResponse.Name = "lblResponse";
this.lblResponse.Size = new System.Drawing.Size(0, 21);
this.lblResponse.TabIndex = 5;
lblResponse.Text = "Value must be numeric and > 0";
txtFirstNum.Clear();
txtFirstNum.Focus();
return;

}



while (int.TryParse(txtSecondNum.Text, out second) == false)

{

this.lblResponse.AutoSize = true;
this.lblResponse.Visible = true;
this.lblResponse.Font = new System.Drawing.Font("Times New Roman", 14.25F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lblResponse.ForeColor = System.Drawing.Color.FromArgb
(((int)(((byte)(253)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))));
this.lblResponse.Location = new System.Drawing.Point(255, 249);
this.lblResponse.Name = "lblResponse";
this.lblResponse.Size = new System.Drawing.Size(0, 21);
this.lblResponse.TabIndex = 5;
lblResponse.Text = "Value must be numeric and > 0";
txtSecondNum.Clear();
txtSecondNum.Focus();
return;

}



{

addAnswer = (first + second);
lblResponse.Text = string.Format("The sum of {0} and {1} is: {2}",first, second, addAnswer).ToString();



}

}
}
}




Thank you for your time
JK
 
I coded the application like this, but it just seems like I might have missed something in a previous chapter, as we programmers like to make it simple. Just seems like to much code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace AddMultiplyApp
{
public partial class AddMultiplyForm : Form
{
public AddMultiplyForm()
{
InitializeComponent();
}


private void Form1_Load(object sender, EventArgs e)
{


}


private void btnAdd_Click(object sender, EventArgs e)
{


int first,
second,
addAnswer;




if (int.TryParse(txtFirstNum.Text, out first) == false)
{

this.lblResponse.Visible = true;
this.lblResponse.Font = new System.Drawing.Font("Times New Roman", 14.25F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lblResponse.ForeColor = System.Drawing.Color.FromArgb
(((int) (((byte) (253)))), ((int) (((byte) (0)))), ((int) (((byte) (0)))));
this.lblResponse.Location = new System.Drawing.Point(255, 249);
this.lblResponse.Name = "lblResponse";
this.lblResponse.Size = new System.Drawing.Size(0, 21);
this.lblResponse.TabIndex = 5;
lblResponse.Text = "Value must be numeric and > 0";
txtFirstNum.Clear();
txtFirstNum.Focus();
return;
}


if (first < 1)
{
this.lblResponse.Visible = true;
this.lblResponse.Font = new System.Drawing.Font("Times New Roman", 14.25F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lblResponse.ForeColor = System.Drawing.Color.FromArgb
(((int)(((byte)(253)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))));
this.lblResponse.Location = new System.Drawing.Point(255, 249);
this.lblResponse.Name = "lblResponse";
this.lblResponse.Size = new System.Drawing.Size(0, 21);
this.lblResponse.TabIndex = 5;
lblResponse.Text = "Value must be numeric and > 0";
txtFirstNum.Clear();
txtFirstNum.Focus();
return;
}

if (int.TryParse(txtSecondNum.Text, out second) == false)
{

this.lblResponse.AutoSize = true;
this.lblResponse.Visible = true;
this.lblResponse.Font = new System.Drawing.Font("Times New Roman", 14.25F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lblResponse.ForeColor = System.Drawing.Color.FromArgb
(((int)(((byte)(253)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))));
this.lblResponse.Location = new System.Drawing.Point(255, 249);
this.lblResponse.Name = "lblResponse";
this.lblResponse.Size = new System.Drawing.Size(0, 21);
this.lblResponse.TabIndex = 5;
lblResponse.Text = "Value must be numeric and > 0";
txtSecondNum.Clear();
txtSecondNum.Focus();
return;
}


if (second < 1)
{
this.lblResponse.Visible = true;
this.lblResponse.Font = new System.Drawing.Font("Times New Roman", 14.25F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lblResponse.ForeColor = System.Drawing.Color.FromArgb
(((int)(((byte)(253)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))));
this.lblResponse.Location = new System.Drawing.Point(255, 249);
this.lblResponse.Name = "lblResponse";
this.lblResponse.Size = new System.Drawing.Size(0, 21);
this.lblResponse.TabIndex = 5;
lblResponse.Text = "Value must be numeric and > 0";
txtSecondNum.Clear();
txtSecondNum.Focus();
return;
}

{
addAnswer = (first + second);
lblResponse.Text = string.Format("The sum of {0} and {1} is: {2}",first, second, addAnswer).ToString();



}

}
}
}
 

Latest posts

Back
Top Bottom