-
April 27th, 2015, 11:51 AM
#1
[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.
-
April 28th, 2015, 02:58 AM
#2
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
-
April 28th, 2015, 03:11 AM
#3
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.
-
April 28th, 2015, 01:53 PM
#4
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.
-
April 28th, 2015, 03:46 PM
#5
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.
-
April 28th, 2015, 04:07 PM
#6
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.
-
April 29th, 2015, 02:43 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|