In this blog, we will learn how to add and mutliply two textbox values and automatically display add function result in third textbox value and mutliply function result in fourth textbox value.
Step1
Open Visual Studio->New Project->Templates->Visual C#->Windows->WindowForm Application.
Select WindowForm Application.. Then, give Project Name and Project Location.
Step2
View->Select ToolBox,Using the Toolbox , Design the Form in Window Applicaton.
Step3
Click the Textbox Properties and add the TextChanged Event.
- private void txtt1_TextChanged(object sender, EventArgs e) {
- if (txtt1.Text.Length > 0 && txtt2.Text.Length > 0) {
- txtt3.Text = Convert.ToString(Convert.ToInt32(txtt1.Text) + Convert.ToInt32(txtt2.Text));
- txtt4.Text = Convert.ToString(Convert.ToInt32(txtt1.Text) * Convert.ToInt32(txtt2.Text));
- }
- if (txtt1.Text.Length > 0 && txtt2.Text.Length == 0) {
- txtt3.Text = "0";
- txtt4.Text = "0";
- }
- if (txtt1.Text.Length == 0 && txtt2.Text.Length > 0) {
- txtt3.Text = "0";
- txtt4.Text = "0";
- }
- if (txtt1.Text.Length == 0 && txtt2.Text.Length == 0) {
- txtt3.Text = "0";
- txtt4.Text = "0";
- }
- }
- private void txtt2_TextChanged(object sender, EventArgs e) {
- if (txtt1.Text.Length > 0 && txtt2.Text.Length > 0) {
- txtt3.Text = Convert.ToString(Convert.ToInt32(txtt1.Text) + Convert.ToInt32(txtt2.Text));
- txtt4.Text = Convert.ToString(Convert.ToInt32(txtt1.Text) * Convert.ToInt32(txtt2.Text));
- }
- if (txtt1.Text.Length > 0 && txtt2.Text.Length == 0) {
- txtt3.Text = "0";
- txtt4.Text = "0";
- }
- if (txtt1.Text.Length == 0 && txtt2.Text.Length > 0) {
- txtt3.Text = "0";
- txtt4.Text = "0";
- }
- if (txtt1.Text.Length == 0 && txtt2.Text.Length == 0) {
- txtt3.Text = "0";
- txtt4.Text = "0";
- }
- }
Step 4
Press F5 or "Build and Run" the application.
Thank you for reading this blog.