Home

Example Application:
Elementary Addition

 

Elementary Addition

Introduction

This exercise applies the characteristics of a label to assist you with performing elementary additions.

In this application, two numbers are selected randomly. Each number is less than 100. After the form displays these two numbers, the user (the student) is asked to add them and enter the result. The user/student can then click Check to check the result.

Windows Controls:

Practical LearningPractical Learning: Introducing Text Boxes

  1. Create a Window Forms Application named ElementaryAddition2
  2. Design the form as follows:
     
    Elementary Addition
    Control Text Name TextAlign Font Additional Properties
    Label 00 lblOperand1 Center Name: Tahoma
    Size:   48
    Bold:   True
    AutoSize: True
    ForeColor: Blue
    Label +   Center Name: Arial
    Size:   50
    Bold:   True
    AutoSize: True
    ForeColor: Maroon
    Label 00 lblOperand2 Center Name: Tahoma
    Size:   48
    Bold:   True
    AutoSize: True
    ForeColor: Blue
    Label =   Center Name: Arial
    Size:   50
    Bold:   True
    AutoSize: True
    ForeColor: Green
    TextBox 000 txtResult Center Name: Tahoma
    Size:   48
    Bold:   True
     
    Label New Operation lblNewOperation Center Name: Tahoma, Size: 28 Bold: True AutoSize: True
    BorderStyle: Fixed3D
    ForeColor: White
    BackColor:Maroon 
    Label Check lblCheckAnswer Center Name: Tahoma, Size: 28 Bold: True AutoSize: True
    BorderStyle: Fixed3D
    ForeColor: White
    BackColor:Maroon 
    Label Quit lblQuit Center Name: Tahoma, Size: 28 Bold: True AutoSize: True
    BorderStyle: FixedSingle
  3. Double-click the New Operation label and implement its event as follows:
     
    Private Sub lblNewOperationClick(ByVal sender As System.Object, 
                                          ByVal e As System.EventArgs) 
                                          Handles lblNewOperation.Click
            Dim Operand1 As Integer
            Dim Operand2 As Integer
    
            Dim rndNumber As Random = New Random()
    
            Operand1 = rndNumber.Next(99)
            Operand2 = rndNumber.Next(99)
            Dim Result As Integer = operand1 + operand2
    
            lblOperand1.Text = CStr(Operand1)
            lblOperand2.Text = CStr(Operand2)
            txtResult.Text = ""
            txtResult.Focus()
    End Sub
  4. In the Class Name combo box, select lblCheckAnswer
  5. In the Method Name combo box, select Click and implement its event as follows:
     
    Private Sub lblCheckAnswerClick(ByVal sender As Object, 
                                         ByVal e As System.EventArgs) 
                                         Handles lblCheckAnswer.Click
            Dim Operand1 As Integer
            Dim Operand2 As Integer
            Dim Result As Integer
    
            Try
    
                Operand1 = CInt(lblOperand1.Text)
            Catch
                MsgBox("Invalid Value")
            End Try
    
            Try
                Operand2 = CInt(lblOperand2.Text)
            Catch
                MsgBox("Invalid Value")
            End Try
    
            Try
                Result = CInt(txtResult.Text)
    
                If Result = (Operand1 + Operand2) Then
                    MsgBox("WOW - Good Answer")
                Else
                    MsgBox("PSSST - Wrong Answer")
                End If
            Catch
                MsgBox("Invalid Answer")
            End Try
    
            lblNewOperationClick(sender, e)
    End Sub
  6. In the Class Name combo box, select lblQuit
  7. In the Method Name combo box, select Click and implement its event as follows:
     
    private void lblQuitClick(object sender, EventArgs e)
    
                end
    End Sub
  8. Execute the application and test it
  9. Click the Close button to close the form and return to your programming environment

Download

 

Home Copyright © 2008-2012 FunctionX