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?