none
Checkboxes and RadioButton RRS feed

  • Question

  • Hello Everyone

    I've been working with window application where I have to design an billing program in a restaurant. This application have three checkboxes for food menu items. And Two Radiobutton for options of drinks and textbox for input of quantity. Program have to calculate amount due based on customer selection. I tried to code the program but it only multiply quantity checkbox selected  and exclude RadioButton checked if options for drinks is selected. Please help. 

    Thursday, July 12, 2018 8:13 AM

All replies

  • Hi 

    Do you have any code show us? I will test it on my side.This is my code, it seems that there is no problem you mentioned.

    Public Class Form1
        Dim sum As Integer
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If CheckBox1.Checked And String.IsNullOrEmpty(TextBox1.Text) = False Then
                sum += 5 * CInt(TextBox1.Text)
            End If
            If CheckBox2.Checked And String.IsNullOrEmpty(TextBox2.Text) = False Then
                sum += 10 * CInt(TextBox2.Text)
            End If
            If CheckBox3.Checked And String.IsNullOrEmpty(TextBox3.Text) = False Then
                sum += 15 * CInt(TextBox3.Text)
            End If
            If RadioButton1.Checked And String.IsNullOrEmpty(TextBox4.Text) = False Then
                sum += 20 * CInt(TextBox4.Text)
            End If
            If RadioButton2.Checked And String.IsNullOrEmpty(TextBox4.Text) = False Then
                sum += 25 * CInt(TextBox4.Text)
            End If
            TextBox5.Text = sum.ToString()
            sum = 0
        End Sub
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, July 12, 2018 9:17 AM
  • Hello,

    If this was a real application you would not have hard coded values (in this case prices) set in the form but instead have the prices coming from a data source such as a database and menu items coming from the database which means that all controls that represent items to select would be dynamically placed on a form.

    But for starters to get an idea how this would work statically which then goes to dynamic you would start out placing controls on the form, each CheckBox in this case would have it's tag property set to the price but when done dynamically it would have a key that would go back to a table in the database to get the price, each label that goes with the item would display the price per the item.

    For recording quantity in a TextBox you need a numeric TextBox or code to validate the value is a number.

    In the code below, each CheckBox and RadioButton gets the price from it's tag property set at design time but remember for a real app the tag would have a key back to the database table.

    Public Class Form1
        Private _totalPurchase As Decimal = 0D
        Private Sub cmdPurchase_Click(sender As Object, e As EventArgs) Handles cmdPurchase.Click
            _totalPurchase = 0D
    
            Dim value As Integer
    
            If chkCheeseBurger.Checked Then
                If Integer.TryParse(txtCheeseBurger.Text, value) Then
                    _totalPurchase += CDec(chkCheeseBurger.Tag) * value
                End If
            End If
            If chkFries.Checked Then
                If Integer.TryParse(txtFries.Text, value) Then
                    _totalPurchase += CDec(chkFries.Tag) * value
                End If
            End If
            If chkPie.Checked Then
                If Integer.TryParse(txtPie.Text, value) Then
                    _totalPurchase += CDec(chkPie.Tag) * value
                End If
            End If
    
            If GroupBox1.Controls.OfType(Of RadioButton).Any(Function(rb) rb.Checked) Then
    
                If Integer.TryParse(txtDrinks.Text, value) Then
                    _totalPurchase += CDec(GroupBox1.Controls.OfType(Of RadioButton).
                                          FirstOrDefault().Tag) * value
                End If
            End If
    
            txtTotal.Text = _totalPurchase.ToString("C2")
    
        End Sub
    End Class

    Each control used here has a name, not so important for labels but good practice. In a dynamic environment names don't matter as the calculations and method of getting the values works differently. I would create a user control where each one has a TextBox and CheckBox combination.

    Also in a dynamic setup a validation setup would be in order that with one click on the order button all data would be validated for correctness.

    Anyways this is the start to build upon.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Thursday, July 12, 2018 10:19 AM
    Moderator
  • 'Program Name:      Billing Program
    'Programmer:        Tinyiko Baloyi
    'Date:              2018
    'Description:       This program calculate the total amount due based
    '                   on customer order/menu selection and display the 
    '                   summary customer orders.
    Public Class BillingForm
        'Declare module-level variables for summary information
        Private TotalDecimal As Decimal
        Private CustomerCountInteger As Integer
    
        'Declare constants.
        Const PRIMERIB_PRICE_Decimal As Decimal = 25.95
        Const CHICKEN_PRICE_Decimal As Decimal = 18.95
        Const PASTA_PRICE_Decimal As Decimal = 12.95
        Const OPENBAR_PRICE_Decimal As Decimal = 25.0
        Const WINEWITHDINNER_PRICE As Decimal = 8.0
    
        Private Sub CalculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateButton.Click
            'Calculate and dispaly current amounts and add to total.
    
            Dim PriceDecimal, OpenBarDecimal, WinewithdinnerDecimal, ItemAmountDecimal As Decimal
            Dim QuantityInteger As Integer
    
            'Find the price.
            If PrimeRibRadioButton.Checked Then
                PriceDecimal = PRIMERIB_PRICE_Decimal
            ElseIf ChickenRadioButton.Checked Then
                PriceDecimal = CHICKEN_PRICE_Decimal
            ElseIf PastaRadioButton.Checked Then
                PriceDecimal = PRIMERIB_PRICE_Decimal
            End If
    
            'Calculate the extended price and add to total.
            Try
                QuantityInteger = Integer.Parse(QuantityTextBox.Text)
                ItemAmountDecimal = PriceDecimal * QuantityInteger
                TotalDecimal += ItemAmountDecimal
    
                If OpenBarCheckBox.Checked Then
                    TotalDecimal = ItemAmountDecimal + OpenBarDecimal
                End If
    
                If WineWithDinnerCheckBox.Checked Then
                    TotalDecimal = ItemAmountDecimal + WinewithdinnerDecimal
    
                End If
                ItemAmountTextBox.Text = ItemAmountDecimal.ToString("C")
                TotalTextBox.Text = TotalDecimal.ToString("C")
                'Allow change for new order only.
                'Allow clear after new order is begun.
    
            Catch QuantityException As FormatException
                MessageBox.Show("Quantity must be numeric.", "Data entry error", MessageBoxButtons.OK, MessageBoxIcon.Information)
                With QuantityTextBox
                    .Focus()
                    .SelectAll()
                End With
    
            End Try
    
        End Sub
    
        Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click
            'Clear the appropriate controls.
            PrimeRibRadioButton.Checked = True 'All others are false.
            ItemAmountTextBox.Clear()
            With QuantityTextBox
                .Clear()
                .Focus()
            End With
        End Sub
    
        Private Sub SummaryButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SummaryButton.Click
            'Calculate the average and display the total.
    
            Dim AverageDecimal As Decimal
            Dim MessageString As String
    
            If TotalDecimal <> 0 Then
                'Make sure last order is counted.
            End If
            If CustomerCountInteger > 0 Then
                'Calculate the average.
                AverageDecimal = TotalDecimal / CustomerCountInteger
    
            End If
            'Concatenate the message string.
            MessageString = "Quantity:" & CustomerCountInteger.ToString() & Environment.NewLine & Environment.NewLine & "Total Sales: " & TotalDecimal.ToString("C") & Environment.NewLine & Environment.NewLine & "Average Sale: " & AverageDecimal.ToString("C")
            MessageBox.Show(MessageString, " Menu Selection Summary", MessageBoxButtons.OK, MessageBoxIcon.Information)
    
        End Sub
    
        Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
            'Terminate the project.
            Me.Close()
        End Sub
    End Class
    
    

    I made mistake of giving you insufficient information for you to have an idea on how to help with the question. I attache user form and the code I wrote. But it does not give the required outcome. That is to calculate total amount due including drinks option when Checkbox is checked. And display summary order when summary button is clicked. And clear all fields. This user form is done using excel for illustration purposes I have Visual Basic 2010 software that I use.

     I hope this is in order


    Friday, July 13, 2018 9:15 AM
  • I made mistake of giving you insufficient information for you to have an idea on how to help with the question. I attache user form and the code I wrote. But it does not give the required outcome. That is to calculate total amount due including drinks option when Checkbox is checked. And display summary order when summary button is clicked. And clear all fields. This user form is done using excel for illustration purposes I have Visual Basic 2010 software that I use.

     I hope this is in order

    Find user form and Code

    'Program Name:      Billing Program
    'Programmer:        Tinyiko Baloyi
    'Date:              2018
    'Description:       This program calculate the total amount due based
    '                   on customer order/menu selection and display the 
    '                   summary customer orders.
    Public Class BillingForm
        'Declare module-level variables for summary information
        Private TotalDecimal As Decimal
        Private CustomerCountInteger As Integer
    
        'Declare constants.
        Const PRIMERIB_PRICE_Decimal As Decimal = 25.95
        Const CHICKEN_PRICE_Decimal As Decimal = 18.95
        Const PASTA_PRICE_Decimal As Decimal = 12.95
        Const OPENBAR_PRICE_Decimal As Decimal = 25.0
        Const WINEWITHDINNER_PRICE As Decimal = 8.0
    
        Private Sub CalculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateButton.Click
            'Calculate and dispaly current amounts and add to total.
    
            Dim PriceDecimal, OpenBarDecimal, WinewithdinnerDecimal, ItemAmountDecimal As Decimal
            Dim QuantityInteger As Integer
    
            'Find the price.
            If PrimeRibRadioButton.Checked Then
                PriceDecimal = PRIMERIB_PRICE_Decimal
            ElseIf ChickenRadioButton.Checked Then
                PriceDecimal = CHICKEN_PRICE_Decimal
            ElseIf PastaRadioButton.Checked Then
                PriceDecimal = PRIMERIB_PRICE_Decimal
            End If
    
            'Calculate the extended price and add to total.
            Try
                QuantityInteger = Integer.Parse(QuantityTextBox.Text)
                ItemAmountDecimal = PriceDecimal * QuantityInteger
                TotalDecimal += ItemAmountDecimal
    
                If OpenBarCheckBox.Checked Then
                    TotalDecimal = ItemAmountDecimal + OpenBarDecimal
                End If
    
                If WineWithDinnerCheckBox.Checked Then
                    TotalDecimal = ItemAmountDecimal + WinewithdinnerDecimal
    
                End If
                ItemAmountTextBox.Text = ItemAmountDecimal.ToString("C")
                TotalTextBox.Text = TotalDecimal.ToString("C")
                'Allow change for new order only.
                'Allow clear after new order is begun.
    
            Catch QuantityException As FormatException
                MessageBox.Show("Quantity must be numeric.", "Data entry error", MessageBoxButtons.OK, MessageBoxIcon.Information)
                With QuantityTextBox
                    .Focus()
                    .SelectAll()
                End With
    
            End Try
    
        End Sub
    
        Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click
            'Clear the appropriate controls.
            PrimeRibRadioButton.Checked = True 'All others are false.
            ItemAmountTextBox.Clear()
            With QuantityTextBox
                .Clear()
                .Focus()
            End With
        End Sub
    
        Private Sub SummaryButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SummaryButton.Click
            'Calculate the average and display the total.
    
            Dim AverageDecimal As Decimal
            Dim MessageString As String
    
            If TotalDecimal <> 0 Then
                'Make sure last order is counted.
            End If
            If CustomerCountInteger > 0 Then
                'Calculate the average.
                AverageDecimal = TotalDecimal / CustomerCountInteger
    
            End If
            'Concatenate the message string.
            MessageString = "Quantity:" & CustomerCountInteger.ToString() & Environment.NewLine & Environment.NewLine & "Total Sales: " & TotalDecimal.ToString("C") & Environment.NewLine & Environment.NewLine & "Average Sale: " & AverageDecimal.ToString("C")
            MessageBox.Show(MessageString, " Menu Selection Summary", MessageBoxButtons.OK, MessageBoxIcon.Information)
    
        End Sub
    
        Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
            'Terminate the project.
            Me.Close()
        End Sub
    End Class
    
    

    Friday, July 13, 2018 9:18 AM