CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2015
    Posts
    8

    [RESOLVED] VB 2012 Modules and Forms help

    I have the following instructions for a homework assignment:

    Create a program that collects two numbers and performs a specified function: addition, subtraction, multiplication, or division. You will use modules to drive this calculator and return an on separate form.
    • The user will be asked to enter an integer in each text box.
    • The application should validate that integers are entered.
    • Functions and procedures will be created in a module to carry out addition, subtraction, multiplication, and division. The module should be named DriveCalc.vb.
    • The click of a button should call the module to carry out the mathematical equation and return an answer.
    • The results of division should contain only 3 decimal places.
    • Results should be displayed on a modal form named Results.vb (as seen in Figure 2.)


    Application functions
    1. A load event should announce that the text boxes only accept integers
    2. A module should be created named DriveCalc.vb containing the functions needed to support the addition, subtraction, multiplication, and division of two integers
    3. The click event of each button should rely on the module DriveCalc.vb to reach an answer
    4. The results of a click event should be displayed in a modal form named Results.vb by passing the values into the text properties of the labels

    I have successfully created the DriveCalc.vb module with the following code:

    Code:
     ' Declare variables to store values in the text boxes
     Dim decNumber1 As Decimal = 0D
     Dim decNumber2 As Decimal = 0D
    
     ' The Add function adds Number 1 and Number 2 and returns the sum. 
     Public Function Add(ByVal decAdd As Decimal) As Decimal
     Return decAdd = (decNumber1 + decNumber2)
     End Function
    
     (Subtract, Multiply, and Divide based on same format)
    
     This is code for the Add button:
    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
    
            ' To hold the sum
            Dim decSum As Decimal
    
            Try
                decSum = Add()
            Catch ex As Exception
                ' Display an error message if entry not an integer
                MessageBox.Show("Please enter only integers.")
            End Try
            ' Display the results in the lblAnswer text box
            lblAnswer.Text = decAdd.ToString()
    
            ' Display the results in the in the Equals label on form Results.vb
            Dim frmResults As New Results
            frmResults.lblEquals.Text = decAdd
    
        End Sub
    This is not working. Can anyone help me determine what is needed that I do not have in this code?

    Thanks.

  2. #2
    Join Date
    Apr 2012
    Posts
    43

    Re: VB 2012 Modules and Forms help

    This might be your problem:-
    Code:
    Public Function Add(ByVal decAdd As Decimal) As Decimal
       Return decAdd = (decNumber1 + decNumber2)
    End Function
    Firstly, you need two parameters if that function is supposed to add. And secondly, that Return statement is wrong. You're return the result of a Boolean comparison. You want this:-
    Code:
    Public Function Add(ByVal num1 As Decimal, ByVal num2 As Decimal) As Decimal
       Return num1 + num2
    End Function

  3. #3
    Join Date
    Apr 2012
    Posts
    43

    Re: VB 2012 Modules and Forms help

    Here a partial implementation of your assignment. Note that it doesn't adhere to all the requirements made by your instructor. You're gonna have to fix those:-
    Code:
    Public Class Form1
        Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnAdd.Click
    
            Try
                lblAns.Text = AddIntegers(tboxNum1.Text, tboxNum2.Text)
            Catch ex As Exception
                MessageBox.Show("Please enter only integers")
            End Try
        End Sub
    
    
    #Region "Designer code"
    
        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        <System.Diagnostics.DebuggerStepThrough()> _
        Private Sub InitializeComponent()
            Me.tboxNum1 = New System.Windows.Forms.TextBox()
            Me.tboxNum2 = New System.Windows.Forms.TextBox()
            Me.lblAns = New System.Windows.Forms.Label()
            Me.btnAdd = New System.Windows.Forms.Button()
            Me.SuspendLayout()
            '
            'tboxNum1
            '
            Me.tboxNum1.Location = New System.Drawing.Point(12, 12)
            Me.tboxNum1.Name = "tboxNum1"
            Me.tboxNum1.Size = New System.Drawing.Size(100, 20)
            Me.tboxNum1.TabIndex = 0
            '
            'tboxNum2
            '
            Me.tboxNum2.Location = New System.Drawing.Point(12, 38)
            Me.tboxNum2.Name = "tboxNum2"
            Me.tboxNum2.Size = New System.Drawing.Size(100, 20)
            Me.tboxNum2.TabIndex = 1
            '
            'lblAns
            '
            Me.lblAns.AutoSize = True
            Me.lblAns.Location = New System.Drawing.Point(13, 65)
            Me.lblAns.Name = "lblAns"
            Me.lblAns.Size = New System.Drawing.Size(39, 13)
            Me.lblAns.TabIndex = 2
            Me.lblAns.Text = "Label1"
            '
            'btnAdd
            '
            Me.btnAdd.Location = New System.Drawing.Point(118, 12)
            Me.btnAdd.Name = "btnAdd"
            Me.btnAdd.Size = New System.Drawing.Size(75, 23)
            Me.btnAdd.TabIndex = 3
            Me.btnAdd.Text = "Add"
            Me.btnAdd.UseVisualStyleBackColor = True
            '
            'Form1
            '
            Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
            Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
            Me.ClientSize = New System.Drawing.Size(543, 204)
            Me.Controls.Add(Me.btnAdd)
            Me.Controls.Add(Me.lblAns)
            Me.Controls.Add(Me.tboxNum2)
            Me.Controls.Add(Me.tboxNum1)
            Me.Name = "Form1"
            Me.Text = "Form1"
            Me.ResumeLayout(False)
            Me.PerformLayout()
    
        End Sub
        Friend WithEvents tboxNum1 As System.Windows.Forms.TextBox
        Friend WithEvents tboxNum2 As System.Windows.Forms.TextBox
        Friend WithEvents lblAns As System.Windows.Forms.Label
        Friend WithEvents btnAdd As System.Windows.Forms.Button
    #End Region
    End Class
    
    
    Friend Module DriveCalc
    
        Public Function AddIntegers(ByVal num1 As Integer, ByVal num2 As Integer) As Integer
            Return num1 + num2
        End Function
    
    
    End Module
    You can start a new project in Visual Studio and just paste all the code into Form1.vb.

  4. #4
    Join Date
    Apr 2015
    Posts
    8

    Exclamation Re: VB 2012 Modules and Forms help

    Thank you so much for the response. I have changed the module code to this:

    Code:
    Module DriveCalc
    
        ' Declare variables to store values in the text boxes
        Dim decNumber1 As Decimal = 0D
        Dim decNumber2 As Decimal = 0D
    
        ' The Add function adds Number 1 and Number 2 and returns the sum. 
        Public Function Add(ByVal Number1 As Decimal, number2 As Decimal) As Decimal
            Return Number1 + number2
        End Function
    This is my main form code:

    Code:
     Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
    
            ' Declare variables to store values in the text boxes
            Dim decNumber1 As Decimal = 0D
            Dim decNumber2 As Decimal = 0D
            decNumber1 = CDec(txtNumber1.Text)
            decNumber2 = CDec(txtNumber2.Text)
    
            ' Declare a variable to store the Add function
            Dim Add As Decimal = 0
    
            If IsNumeric(txtNumber1.Text) Then
                decNumber1 = CDec(txtNumber1.Text)
            Else
                MessageBox.Show("Please enter an integer.")
            End If
    
            ' Display the results in the lblAnswer text box
            lblAnswer.Text = Add.ToString()
    
            ' Display the Number 1 entry, Number 2 entry, and the operator used
            ' for the calculation on form Results.vb
            Dim frmResults As New Results
            frmResults.lblNumber1.Text = decNumber1
            frmResults.lblOperator.Text = "Plus"
            frmResults.lblNumber2.Text = decNumber2
    
            ' Display the results in the in the Equals label on form Results.vb
    
            frmResults.lblEquals.Text = Add.ToString()
            frmResults.ShowDialog()
    
        End Sub
    No matter what I try, the sum I get is 0 in the "Answer is" label text box on the main form (VB Function Calculator.vb) and in the "Equals" label text box on the Results.vb form.

    Can you explain why this is happening? You are the only person who has responded and I have asked in another forum and in an online study website Chegg.com.

  5. #5
    Join Date
    Apr 2012
    Posts
    43

    Re: VB 2012 Modules and Forms help

    There is no where in that code where you perform any addition. Of course it would be 0.

  6. #6
    Join Date
    Apr 2015
    Posts
    8

    Re: VB 2012 Modules and Forms help

    It is supposed to pull the Add function from the DriveCalc module. That is what I cannot figure out.

  7. #7
    Join Date
    Apr 2012
    Posts
    43

    Re: VB 2012 Modules and Forms help

    What ?....lol

    You're supposed to call Add:-
    Code:
    lblAnswer.Text = Add(decNumber1, decNumber2).ToString

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured