We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,725 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Newbie question - Button is not calculating integers!?

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

2
Contributors
3
Replies
22 Hours
Discussion Span
5 Months Ago
Last Updated
4
Views
Question
Answered
iseered
Newbie Poster
2 posts since Nov 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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

tinstaafl
Nearly a Posting Virtuoso
1,308 posts since Jun 2010
Reputation Points: 341
Solved Threads: 226
Skill Endorsements: 14

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.

tinstaafl
Nearly a Posting Virtuoso
1,308 posts since Jun 2010
Reputation Points: 341
Solved Threads: 226
Skill Endorsements: 14

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!.

iseered
Newbie Poster
2 posts since Nov 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 5 Months Ago by tinstaafl

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0660 seconds using 2.68MB