In tutorial 5, we told you to try out one exercise. In that tutorial, we put values of variables in code by ourselves, which made our code static. It means, pressing the button, no matter how many times and when, is going to create similar answer every time. As we are developing these forms for user interaction, we must allow user to put two values that will be directly assigned to our variables. To make it happen, insert one more textbox to our previous form. It will look like this:
Now select our new textbox and locate its “Name” property in property box located on right bottom corner. Change it to txtVariable2. Check following image to avoid any mistakes:
Similarly, change the name of first textbox to txtVariable1. Now we are set, so let us look into code. Double click on button to enter coding window and write following code. Erase previous code if there is any.
Dim Number1 As Integer Dim Number2 As Integer Dim Number3 As Integer Number1 = txtVariable1.Text Number2 = txtVariable2.Text Number3 = Number1 + Number2 MsgBox(Number3)
Debug the program and form will appear on screen. Enter any two integer values into two textbox and click on button. Result of addition will appear in popped up message box. You can also add third textbox and make result appear into it as we have seen in last tutorial.
Many aspects and tricks of VB can put you into surprise like the one we are going to present now. Add a new button into your form and change its “Text” property to Click! Double click on second button and add following code into its coding window:
txtVariable1.Text = Button2.Text
Debug the code and click on second button to see following effect in your form.
In this case, we used text property of button to display text in textbox. Once we move ahead, you will understand this code with more details. In next section, we will use this technique to design calculator in VB.NET. A lot more to come, so stay tuned!