I cannot make my program calculate the total of two integers (one on the main form, and the other on a different form). It keeps putting in the value "0", but when I tried it without a button it'd always just put one of the chosen integers instead of adding them. Below is the code for the form that has the Dormatory costs and where the Total label is, the other form is below it. Am I missing something completely obvious?

FORM 1
` Public Class frmDorm

    Friend Shared DormCalc As Integer
    Private Sub btnMeal_Click(sender As System.Object, e As System.EventArgs) Handles btnMeal.Click
        Dim frmMeal As New frmMeal

        frmMeal.ShowDialog()

    End Sub

    Private Sub Label3_Click(sender As System.Object, e As System.EventArgs) Handles lblTotalT.Click
    End Sub

    Private Sub lblTotal_Click(sender As System.Object, e As System.EventArgs) Handles lblTotal.Click
    End Sub

    Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Public Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cboDorms.SelectedIndexChanged

        'Dorm pricing
        Dim DormFathing As Integer = 1200.0
        Dim DormAllen As Integer = 1500.0
        Dim DormPike As Integer = 1600.0
        Dim DormUnivSuites As Integer = 1800.0
        Dim DormCalc As Integer


        'No Dorm selected
        If cboDorms.SelectedIndex = -1 Then
            MessageBox.Show("No Dorm was selected!", "Error", MessageBoxButtons.OK)
        End If

        'Index's of cboDorms, sent to lblDormTotal for calculation
        If cboDorms.SelectedIndex = 0 Then
            DormCalc = DormAllen
            lblDormTotal.Text = DormAllen.ToString
        ElseIf cboDorms.SelectedIndex = 1 Then
            DormCalc = DormPike
            lblDormTotal.Text = DormPike.ToString
        ElseIf cboDorms.SelectedIndex = 2 Then
            DormCalc = DormFathing
            lblDormTotal.Text = DormFathing.ToString
        ElseIf cboDorms.SelectedIndex = 3 Then
            DormCalc = DormUnivSuites
            lblDormTotal.Text = DormUnivSuites.ToString
        End If
    End Sub

    Private Sub lblMealPlanTotal_Click(sender As System.Object, e As System.EventArgs) Handles lblMealPlanTotal.Click
    End Sub

    Private Sub lblDormTotal_Click(sender As System.Object, e As System.EventArgs) Handles lblDormTotal.Click
    End Sub

    Private Sub btnCalc_Click(sender As System.Object, e As System.EventArgs) Handles btnCalc.Click

        lblTotal.Text = frmMeal.MealCalc + DormCalc

    End Sub

    Private Sub Calc_Popup(sender As System.Object, e As System.Windows.Forms.PopupEventArgs) Handles Calc.Popup
    End Sub
End Class 

`

FORM 2
Public Class frmMeal

Friend Shared MealCalc As Integer

Private Sub btnMeal_Click(sender As System.Object, e As System.EventArgs) Handles btnReturnDorm.Click
    Me.Close()
End Sub

Public Sub cboDorms_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cboMeals.SelectedIndexChanged
    'Meal pricing
    Dim MealWeek As Integer = 1200
    Dim MealBiWeek As Integer = 1500
    Dim MealUnlimited As Integer = 1600
    Dim MealCalc As Integer

    If cboMeals.SelectedIndex = -1 Then
        MessageBox.Show("No meal plan was selected!", "Error", MessageBoxButtons.OK)
    End If

    'Index's of cboMeals, sent to lblMealTotal for calculation
    If cboMeals.SelectedIndex = 0 Then
        MealCalc = MealBiWeek
        lblMealTotal.Text = MealBiWeek.ToString
    ElseIf cboMeals.SelectedIndex = 1 Then
        MealCalc = MealUnlimited
        lblMealTotal.Text = MealUnlimited.ToString
    ElseIf cboMeals.SelectedIndex = 2 Then
        MealCalc = MealWeek
        lblMealTotal.Text = MealWeek.ToString
    End If

    frmDorm.lblMealPlanTotal.Text = MealCalc

End Sub

Private Sub frmMeal_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
End Sub

End Class

Recommended Answers

All 3 Replies

You've created 2 'MealCalc' variables. Remove the one on line 12 and you should see a difference.

Just took another look. I'm not sure, but closing the form might erase that variable. Try putting your global variables in a Module, if you still have probs.

Thanks rinstaafl! I had to make a module for it (I didn't know what that was used for until now), now it works perfectly fine!.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.