Designing a Calculator in Visual Basic 2013 Part 2

In our previous post, we have shown you the interface of the calculator and the code that can display numbers on the panel. In this article, we will delve into the code that makes calculator in visual basic 2013 works.

Let’s review the first part of code that displays numbers on the display panel when he user click on the number buttons.

Private Sub Btn1_Click(sender As Object, e As EventArgs) Handles Btn0.Click, Btn1.Click, Btn2.Click, Btn3.Click,
Btn4.Click, Btn5.Click, Btn6.Click, Btn7.Click, Btn8.Click, Btn9.Click

If LblPanel.Text = “0” Then LblPanel.Text = “”
LblPanel.Text += sender.text

End Sub

To display numbers on the code, we just need to write one block of code for all the number buttons instead of one block of code for each and every button. The method is by using the sender object and by placing click event for all the buttons after the Handles keyword, separated by commas.

The meaning of sender As Object is the object that responses when the user clicks on a particular button. For example, if the user clicks on Btn2 number button, the sender is Btn2. The action is to display the text of the sender on the display panel, in this case it is 2 if the text on the display button is 0, otherwise the digit 2 will be placed in front of the previous digit.

We shall continue our discussion in next article….