Event Handler to clear textboxes I\'m trying to create an event handler to clear
ID: 3592618 • Letter: E
Question
Event Handler to clear textboxes
I'm trying to create an event handler to clear the "Results" textbox if the text in any other box is changed. It has to be a wiring event and I am unsure exactly how to do this.
Here is my code (it works flawlessly minus the clearing Results):
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 Lab6_7_200
{
public partial class Lab6 : Form
{
public Lab6()
{
InitializeComponent();
}
private void Lab6_Load(object sender, EventArgs e)
{
}
private void btnCalculate_Click(object sender, EventArgs e)
{
decimal value1 = Convert.ToDecimal(this.txtValue1.Text); //convert text to decimal
decimal value2 = Convert.ToDecimal(this.txtValue2.Text); //convert text to decimal
string operator1 = Convert.ToString(this.txtOperator.Text); //convert operator input to string
decimal totalResult = this.GetResult(value1, value2, operator1); //sending values to method and calling it
txtResult.Text = "" + totalResult; //writing into result textbox
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close(); //closing the program
}
private decimal GetResult(decimal value1, decimal value2, string operator1) //creating the method
{
decimal result = 0;
if (operator1 == "x") //determining the operator to be used
{
result = value1 * value2; //multiplication of the values
}
else if (operator1 == "+")
{
result = value1 + value2; //addition of the values
}
else if (operator1 == "-")
{
result = value1 - value2; //subtraction of the values
}
else if (operator1 == "/")
{
result = value1 / value2; //division of the values
}
return result;
}
}
}
Explanation / Answer
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 Lab6_7_200
{
public partial class Lab6 : Form
{
public Lab6()
{
InitializeComponent();
}
private void Lab6_Load(object sender, EventArgs e)
{
}
private void btnCalculate_Click(object sender, EventArgs e)
{
try
{
decimal value1 = Convert.ToDecimal(this.txtValue1.Text); //convert text to decimal
decimal value2 = Convert.ToDecimal(this.txtValue2.Text); //convert text to decimal
string operator1 = Convert.ToString(this.txtOperator.Text); //convert operator input to string
decimal totalResult = this.GetResult(value1, value2, operator1); //sending values to method and calling it
if (totalResult !=null)
{
txtResult.Text = "" + totalResult; //writing into result textbox
}
}
catch (Exception ex)
{
throw ex;
}
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close(); //closing the program
}
private decimal GetResult(decimal value1, decimal value2, string operator1) //creating the method
{
try
{
decimal result = 0;
if (operator1 != null)
{
if (operator1 == "x") //determining the operator to be used
{
result = value1 * value2; //multiplication of the values
}
else if (operator1 == "+")
{
result = value1 + value2; //addition of the values
}
else if (operator1 == "-")
{
result = value1 - value2; //subtraction of the values
}
else if (operator1 == "/")
{
result = value1 / value2; //division of the values
}
}
return result;
}
catch (Exception ex) //Handling the exception in this case
{
throw ex;
}
}
}
}
Thanks & Regards,
If any doubts please feel free to reach me.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.