Reputation: 1
I messed up my code when I tried to put in the font color yellow. Please help!!
Instructions: Create a Windows application that contains two textboxes and two buttons. The textboxes should be used to allow the user to input two positive numeric values. The buttons should be labeled Add and Multiply. Create event-handler methods that retrieve the values, preform the calculations, and display the results of the calculations on a label. The result label should be initially be set to be invisible with a font color of yellow. If invalid data is entered, change the font color to red on the result label and display a message "Value must be numeric and >0." When the final answer is displayed, the font color should be yellow. Additional labels will be needed for the textboxes captions. Do not allow non-numeric characters to be entered. Invoke the TryParse() method to retrieve the values. All controls involved in program statements should be named. Right justify values in the textbox.
Here's what I got on my form1 page
namespace Add_and_Multiply
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int val1;
int val2;
val1 = Convert.ToInt32(textBox1.Text);
val2 = Convert.ToInt32(textBox2.Text);
label3.Text = Convert.ToString(val1 + val2); //after this line I tried putting the code to give me yellow color text and now I have so many errors:(
}
private void button2_Click(object sender, EventArgs e)
{
int val1;
int val2;
val1 = Convert.ToInt32(textBox1.Text);
val2 = Convert.ToInt32(textBox2.Text);
label3.Text = Convert.ToString(val1 * val2);
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Can anyone kindly help me out? I'm not too great at this. Thanks
Someone told me to fix it up like this.
namespace Add_and_Multiply
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int val1;
int val2;
if(!int.TryParse(textBox1.Text.Trim(), out val1))
{
lblYellow.ForeColor = Color.Yellow;
lblYellow.Visible = true;
lblYellow.Text = "First value is invalid";
return;
}
if(!int.TryParse(textBox2.Text.Trim(), out val2))
{
lblYellow.ForeColor = Color.Yellow;
lblYellow.Visible = true;
lblYellow.Text = "Second value is invalid";
return;
}
lblYellow.Visible = false;
label3.Text = Convert.ToString(val1 + val2);
}
private void button2_Click(object sender, EventArgs e)
{
int val1;
int val2;
if(!int.TryParse(textBox1.Text.Trim(), out val1))
{
lblYellow.ForeColor = Color.Yellow;
lblYellow.Visible = true;
lblYellow.Text = "First value is invalid";
return;
}
if(!int.TryParse(textBox2.Text.Trim(), out val2))
{
lblYellow.ForeColor = Color.Yellow;
lblYellow.Visible = true;
lblYellow.Text = "Second value is invalid";
return;
}
lblYellow.Visible = false;
label3.Text = Convert.ToString(val1 * val2);
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
but I'm getting an error "the name 'lblYellow' dose not exist in the current context Form1.cs" should I declare it before the private void button1_click line?
Upvotes: -2
Views: 3651
Reputation: 1
you should change the design name of that label to "lblYellow" then will work... goto property of that label and scroll-down and there is section named design and there you find "(Name)" changle that default name to "lblYellow"...
Upvotes: 0
Reputation: 107
I know this is two years later but Thought people still might be looking for help with this.
private void btnAdd_Click(object sender, EventArgs e)
{
int firstNumber, secondNumber;
if (!int.TryParse(txtBxFirstNumber.Text, out firstNumber) || firstNumber < 1)
{
lblResults.Text = "First number needs\n to be numeric and > 0";
lblResults.ForeColor = Color.Red;
lblResults.Visible = true;
txtBxFirstNumber.Clear();
return;
}
if (!int.TryParse(txtBxSecondNumber.Text, out secondNumber) || secondNumber < 1)
{
lblResults.Text = "Second number need\n to be numeric and > 0";
lblResults.ForeColor = Color.Red;
lblResults.Visible = true;
txtBxSecondNumber.Clear();
return;
}
lblResults.ForeColor = Color.Yellow;
lblResults.Visible = true;
lblResults.Text = Convert.ToString(firstNumber + secondNumber);
}
private void btnMultiply_Click(object sender, EventArgs e)
{
int firstNumber, secondNumber;
if (!int.TryParse(txtBxFirstNumber.Text, out firstNumber) || firstNumber < 1)
{
lblResults.Text = "First number needs\n to be numeric";
lblResults.ForeColor = Color.Red;
lblResults.Visible = true;
txtBxFirstNumber.Clear();
return;
}
if (!int.TryParse(txtBxSecondNumber.Text, out secondNumber) && secondNumber < 1)
{
lblResults.Text = "Second number need\n to be numeric";
lblResults.ForeColor = Color.Red;
lblResults.Visible = true;
txtBxSecondNumber.Clear();
return;
}
lblResults.ForeColor = Color.Yellow;
lblResults.Visible = true;
lblResults.Text = Convert.ToString(firstNumber * secondNumber);
}
Upvotes: 0
Reputation: 1
If anyone has trouble with this project then here is the final product. It looks pretty good once it's running.
Everything works great and how it should!
{ public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void button1_Click(object sender, EventArgs e)
{
int val1;
int val2;
int valid1 = 0;
int valid2 = 0;
if(int.TryParse(textBox1.Text.Trim(), out val1))
{
if(val1>0)
{
valid1 = 1;
caption1.ForeColor = Color.Yellow;
caption1.Visible = true;
caption1.Text = "First value is valid";
}
else
{
caption1.ForeColor = Color.Red;
caption1.Visible = true;
caption1.Text = "Value must be Greater than 0";
}
}
else
{
caption1.ForeColor = Color.Red;
caption1.Visible = true;
caption1.Text = "Value must be Numeric";
}
if(int.TryParse(textBox2.Text.Trim(), out val2))
{
if (val2 > 0)
{
valid2 = 1;
caption2.ForeColor = Color.Yellow;
caption2.Visible = true;
caption2.Text = "First value is valid";
}
else
{
caption2.ForeColor = Color.Red;
caption2.Visible = true;
caption2.Text = "Value must be Greater than 0";
}
}
else
{
caption2.ForeColor = Color.Red;
caption2.Visible = true;
caption2.Text = "Value must be Numeric";
}
if((valid1 > 0)&& (valid2 > 0))
{
label3.Visible = true;
label3.ForeColor = Color.Yellow;
label3.Text = Convert.ToString(val1 + val2);
}
else
{
label3.Visible = false;
}
}
private void button2_Click(object sender, EventArgs e)
{
int val1;
int val2;
int valid1 = 0;
int valid2 = 0;
if(int.TryParse(textBox1.Text.Trim(), out val1))
{
if (val1 > 0)
{
valid1 = 1;
caption1.ForeColor = Color.Yellow;
caption1.Visible = true;
caption1.Text = "First value is valid";
}
else
{
caption1.ForeColor = Color.Red;
caption1.Visible = true;
caption1.Text = "Value must be Greater than 0";
}
}
else
{
caption1.ForeColor = Color.Red;
caption1.Visible = true;
caption1.Text = "Value must be Numeric";
}
if(int.TryParse(textBox2.Text.Trim(), out val2))
{
if (val2 > 0)
{
valid2 = 1;
caption2.ForeColor = Color.Yellow;
caption2.Visible = true;
caption2.Text = "First value is valid";
}
else
{
caption2.ForeColor = Color.Red;
caption2.Visible = true;
caption2.Text = "Value must be Greater than 0";
}
}
else
{
caption2.ForeColor = Color.Red;
caption2.Visible = true;
caption2.Text = "Value must be Numeric";
}
if ((valid1 > 0) && (valid2 > 0))
{
label3.Visible = true;
label3.ForeColor = Color.Yellow;
label3.Text = Convert.ToString(val1 * val2);
}
else
{
label3.Visible = false;
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
Upvotes: 0
Reputation: 11
it looks like you're defining a method with no class or outside the class,please check that all methods are inside Form1 and please check for additional or missing brackets.
Upvotes: 0
Reputation: 33738
label3.ForeColor = Color.Yellow;
... should change the label color to yellow...
But you have another problem. You are not checking for invalid input to change the label to red, nor are you invoking TryParse
as the instructions command.
Additionally, the instructions state that the user should be able to enter any positive numeric value. That would include 8.9 and 216.75. I imagine if you want the best grade possible you will need to account for this. Take a look at the C# built-in data types.
Upvotes: 0
Reputation: 11
whats the errors you have ?, i think you have to delete one } after line :
label3.Text = Convert.ToString(val1 + val2);
Upvotes: 0