Results 1 to 4 of 4

Thread: Store different values from a single ComboBox and a single NumericUpDown

  1. #1

    Thread Starter
    New Member Ricky1ST's Avatar
    Join Date
    May 2014
    Location
    Puerto Rico
    Posts
    7

    Store different values from a single ComboBox and a single NumericUpDown

    Hello all! I made some time ago a not very well organized question about my database driven application. My primary language is Spanish so I will try to be concise and clear. I have an ADD button in a MENU form, 4 TABS with each TAB having its own NUMERICUPDOWN, COMBOBOX and PRICE LABEL. When you open the COMBOBOX it fills up with different food names from a database made in MySQL. You choose one of the names in the combo box and AUTOMATICALLY the labels display it's price (Stored in the database). The problem is that if I press ADD a Sub Total label outside the TabControl keeps record of the added prices, and it works fine between tabs, but when I press ADD when selecting ANOTHER item from the SAME COMBOBOX inside one tab, the price in subtotal gets overridden by the new value (Only happens when selecting items between one combobox). How do I stop this? How to I sum the different prices coming from one combobox with only a global ADD button?

    This is the code for the button ADD:

    Code:
    Private Sub btnAddAll_Click(sender As System.Object, e As System.EventArgs) Handles btnAddAll.Click
            Dim winesPrice As Decimal
            Dim appetizersPrice As Decimal
            Dim dishesPrice As Decimal
            Dim dessertsPrice As Decimal
    
            dessertsPrice = dessertsPrice + (numDesserts.Value * CDec(Val(lblPriceDesserts.Text)))
            dishesPrice = dishesPrice + (numDishes.Value * CDec(Val(lblPriceDishes.Text)))
            winesPrice = winesPrice + (numWines.Value * CDec(Val(lblPriceWines.Text)))
            appetizersPrice = appetizersPrice + (numAppetizers.Value * CDec(Val(lblPriceAppetizers.Text)))
    
            globalDesserts = dessertsPrice
            globalWines = winesPrice
            globalDishes = dishesPrice
            globalAppetizers = appetizersPrice
    
            globalSubTotal = globalDishes + globalDesserts + globalWines + globalAppetizers
            lblSubTotal.Text = globalSubTotal
    
        End Sub

    This is the CODE for one of the comboboxes (They all share the same kind of code):

    Code:
     Try
                MySQLConn.Open()
                Dim query As String
                query = "select * from database.meals where MealName = '" & cmbDesserts.Text & "'" 'Se busca la tabla de meals
                command = New MySqlCommand(query, MySQLConn)
                reader = command.ExecuteReader
                While reader.Read
                    lblDesDesserts.Text = reader.GetString("Description")
                    lblPriceDesserts.Text = reader.GetDecimal("Price")
                    If cmbDesserts.Text = "Panna Cotta" Then
                        Me.picDesserts.Image = Image.FromFile(driveLetter & imagePath & "\Panna Cotta (Dessert).jpg")
                    ElseIf cmbDesserts.Text = "Bellini Granita" Then
                        Me.picDesserts.Image = Image.FromFile(driveLetter & imagePath & "\Bellini Granita (Desserts).jpg")
                    ElseIf cmbDesserts.Text = "Della Parfait" Then
                        Me.picDesserts.Image = Image.FromFile(driveLetter & imagePath & "\Parfait (Dessert).jpg")      
                    End If
    
                End While
                MySQLConn.Close() 'Cerrar coneccion
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            Finally
                MySQLConn.Dispose()
            End Try
        End Sub

    This is the final ORDER BUTTON:

    Code:
    Private Sub btnOrder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOrder.Click
            Dim winesPrice As Decimal
            Dim appetizersPrice As Decimal
            Dim dishesPrice As Decimal
            Dim dessertsPrice As Decimal
    
    
            dessertsPrice = dessertsPrice + (numDishes.Value * CDec(Val(lblPriceDesserts.Text)))
            dishesPrice = dishesPrice + (numDishes.Value * CDec(Val(lblPriceDishes.Text)))
            winesPrice = winesPrice + (numWines.Value * CDec(Val(lblPriceWines.Text)))
            appetizersPrice = appetizersPrice + (numAppetizers.Value * CDec(Val(lblPriceAppetizers.Text)))
    
            globalDesserts = dessertsPrice
            globalWines = winesPrice
            globalDishes = dishesPrice
            globalAppetizers = appetizersPrice
    
            globalSubTotal = globalDishes + globalDesserts + globalWines + globalAppetizers
            lblSubTotal.Text = globalSubTotal
    
            GlobalVariables.globalPrice = globalSubTotal
    
            If GlobalVariables.globalPrice = 0 Then
                MsgBox("You haven't chosen any dishes!", MsgBoxStyle.Critical)
            Else
                Me.Hide()
                frmPayment.Show() 'Ir a ventana de pago
            End If
        End Sub

    This is the FORM LOAD CODES and Global Variables that pass values to the next form:

    Code:
    Imports MySql.Data.MySqlClient 'Añade como referencia MySQL.Data.dll
    Imports System.IO
    
    Public Class frmMenu
        Public Class GlobalVariables
            Public Shared globalPrice As Decimal 'Variable de acceso global
        End Class
        Dim reader As MySqlDataReader
        Dim MySQLConn As MySqlConnection
        Dim command As MySqlCommand
    
        Dim drivePath As DriveInfo 'Direccion actual del proyecto
        Dim imagePath As String = "\Universidad\Systems Development & Implementation\Asignaciones\Proyecto Final\Iconos y Fotos"
        Dim driveLetter As String
    
        Dim globalSubTotal As Decimal
        Dim globalDishes As Decimal 'Variables de acceso local
        Dim globalWines As Decimal
        Dim globalAppetizers As Decimal
        Dim globalDesserts As Decimal
    
        Private Sub frmMenu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            For Each Me.drivePath In DriveInfo.GetDrives 'Busca la letra del "drive" del USB/CDROM y lo pasa a una variable
                If drivePath.IsReady Then
                    If drivePath.DriveType = IO.DriveType.Removable Then
                        driveLetter = drivePath.Name
                    ElseIf drivePath.DriveType = IO.DriveType.CDRom Then
                        driveLetter = drivePath.Name
                    End If
                End If
            Next

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Store different values from a single ComboBox and a single NumericUpDown

    When you click the Add button you can test which TabPage is selected and then you know to add to the subtotal for that page. You could use a Dictionary or an array or just discrete variables to keep the subtotals separate.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member Ricky1ST's Avatar
    Join Date
    May 2014
    Location
    Puerto Rico
    Posts
    7

    Re: Store different values from a single ComboBox and a single NumericUpDown

    I haven't tried Arrays nor know how to use Dictionary yet. Can you tell me an example on how to do it? I am also overusing variables and I know I am being redundant in my code, but I will take the step by step approach and make the application smoother. I tried to make a variable every time the user clicks on a name from the combobox, then add an IF statement in the ADD button that would ask " If Not variable = 0 then" but it didn't work. I am trying to reduce code as much as possible, because before, I had 4 ADD buttons in each TabPage and still had the same problem).
    Last edited by Ricky1ST; May 21st, 2014 at 12:10 AM. Reason: More information.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Store different values from a single ComboBox and a single NumericUpDown

    Quote Originally Posted by Ricky1ST View Post
    I haven't tried Arrays nor know how to use Dictionary yet. Can you tell me an example on how to do it? I am also overusing variables and I know I am being redundant in my code, but I will take the step by step approach and make the application smoother. I tried to make a variable every time the user clicks on a name from the combobox, then add an IF statement in the ADD button that would ask " If Not variable = 0 then" but it didn't work. I am trying to reduce code as much as possible, because before, I had 4 ADD buttons in each TabPage and still had the same problem).
    You would create an array with the same number of elements as there are TabPages in the TabControl. You can then use the SelectedIndex of the TabControl to access the corresponding element of the array, to add something to it or whatever.

    If you don't know how to use arrays then I suggest that you follow the Tutorial link in my signature below.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Tags for this Thread

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