none
Visual Basic - How do I assign values to SelectedIndex items in a drop-down list? RRS feed

  • Question

  • I've hit an obstacle. I just want to learn how I can assign SelectedIndex items with the declared costs? I feel like I am missing something obvious.

    I have declared the costs of the taco types but I am not sure how I can assign them to the SelectedIndex items for the `ddlTacoType` in Visual Basic. Is it a good idea to use a switch statement? Ideally, the output should display the cost as 8.99 when the user selects "Chipotle Chicken."

    Any help would be much appreciated.

    Cheers,

    Richard

    [Screenshot of Output][1]

    ```
    ' Project:  Street Tacos Order Form
    ' Author:   Richard Lew
    ' Date:     November 3, 2019
    ' Purpose:  The web application allows a customer to fill out a street taco order form.

    Public Class About
        Inherits Page
        Private _decChicken As Decimal = 8.99
        Private _decPork As Decimal = 9.99
        Private _strFish As Decimal = 12.99
        Private _strBeef As Decimal = 13.99
        Private _strExtra As Decimal = 0.99
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

        End Sub

        Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
            ' The btnSubmit click event will calculate the cost of the street tacos
            ' based on the type of tacos selected.

            ' Declare and initialize variables
            Dim decTacoCost As Decimal
            Dim decFinalCost As Decimal
            Dim decTotalCost As Decimal
            Dim decChickenCost As Decimal = 8.99D
            Dim decPorkCost As Decimal = 9.99D
            Dim decFishCost As Decimal = 12.99D
            Dim decBeefCost As Decimal = 13.99D
            Dim decExtraCost As Decimal = 0.99
            Dim strName As String
            Dim strAddress As String
            Dim strPhone As String
            Dim decOrderCost As Decimal = 0D
            Dim strMessage As String

            ' Trim additional spaces that are entered by the user
            strName = txtName.Text.Trim
            strAddress = txtAddress.Text.Trim
            strPhone = txtPhone.Text.Trim

            ' Clear the Order Message
            lblOrder.Text = ""

            ' Ensure a Taco Selection is Selected
            If ddlTacoType.SelectedIndex < 0 Then
                lblTacoTypeError.Visible = True
            Else
                lblTacoTypeError.Visible = False
            End If
            ' Ensure a Topping is Selected
            If Not (chkRedChili.Checked Or chkGreenChili.Checked Or chkBeans.Checked Or chkCream.Checked Or chkLime.Checked Or chkNoToppings.Checked) Then
                lblToppingsError.Visible = True
            Else
                lblToppingsError.Visible = False
            End If

            ' Calculate the cost of the Taco(s) selected by the user
            Select Case decTotalCost
                Case 0
                    decTacoCost = decChickenCost
                Case 1
                    decTacoCost = decPorkCost
                Case 2
                    decTacoCost = decFishCost
                Case 3
                    decTacoCost = decBeefCost
                Case 4
                    decTacoCost = decExtraCost
            End Select



        End Sub
    End Class
    ```

    When the user selects a taco type in the `ddlTacoType` drop-down list, the program should associate the item with its cost and calculate the total cost of the selected item at the end of the program.


      [1]: https://i.stack.imgur.com/cqVy6.png
    Thursday, October 31, 2019 10:20 PM

All replies