Add And Multiply Two Textbox Values And Automatically Display The Result In Third Textbox In C#

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.
  1. private void txtt1_TextChanged(object sender, EventArgs e) {  
  2.     if (txtt1.Text.Length > 0 && txtt2.Text.Length > 0) {  
  3.         txtt3.Text = Convert.ToString(Convert.ToInt32(txtt1.Text) + Convert.ToInt32(txtt2.Text));  
  4.         txtt4.Text = Convert.ToString(Convert.ToInt32(txtt1.Text) * Convert.ToInt32(txtt2.Text));  
  5.     }  
  6.     if (txtt1.Text.Length > 0 && txtt2.Text.Length == 0) {  
  7.         txtt3.Text = "0";  
  8.         txtt4.Text = "0";  
  9.     }  
  10.     if (txtt1.Text.Length == 0 && txtt2.Text.Length > 0) {  
  11.         txtt3.Text = "0";  
  12.         txtt4.Text = "0";  
  13.     }  
  14.     if (txtt1.Text.Length == 0 && txtt2.Text.Length == 0) {  
  15.         txtt3.Text = "0";  
  16.         txtt4.Text = "0";  
  17.     }  
  18. }  
  19. private void txtt2_TextChanged(object sender, EventArgs e) {  
  20.     if (txtt1.Text.Length > 0 && txtt2.Text.Length > 0) {  
  21.         txtt3.Text = Convert.ToString(Convert.ToInt32(txtt1.Text) + Convert.ToInt32(txtt2.Text));  
  22.         txtt4.Text = Convert.ToString(Convert.ToInt32(txtt1.Text) * Convert.ToInt32(txtt2.Text));  
  23.     }  
  24.     if (txtt1.Text.Length > 0 && txtt2.Text.Length == 0) {  
  25.         txtt3.Text = "0";  
  26.         txtt4.Text = "0";  
  27.     }  
  28.     if (txtt1.Text.Length == 0 && txtt2.Text.Length > 0) {  
  29.         txtt3.Text = "0";  
  30.         txtt4.Text = "0";  
  31.     }  
  32.     if (txtt1.Text.Length == 0 && txtt2.Text.Length == 0) {  
  33.         txtt3.Text = "0";  
  34.         txtt4.Text = "0";  
  35.     }  
  36. }  
Step 4

Press F5 or "Build and Run" the application.

Thank you for reading this blog.