none
Combo Box Value Display Issues! RRS feed

  • Question

  • Hello  guys, i am new to visual basic and have assignment due and i am not sure how to do this! 

    I have 2 Combo Boxes and 3 Text Boxes and 1 Check Box 

    Text Box 1 Needs to display a value depending on what is selected in combo box 1 

    Example User selects Toy Car in combo box value of 50 shows in textbox1 

    Text Box 2 Needs to display a value depending on what is selected in combo box 2 

    Example User selects Toy Train in combo box value of 20 shows in textbox2 

    In Text Box 3 The total of the value of textbox1 and textbox2 need to be shown when the button is clicked. 

    Here is the textbox 1 code and text box 2 code 

    Private Sub txtJoiningFee_TextChanged(sender As Object, e As EventArgs) Handles txtJoiningFee.TextChanged
            Select Case cbTrainingPlan.Text
                Case "Weight Training"
                    txtJoiningFee.Clear()
                    txtJoiningFee.Text = "70"
                Case "Yoga Class"
                    txtJoiningFee.Clear()
                    txtJoiningFee.Text = "80"
                Case "Cardio Training"
                    txtJoiningFee.Clear()
                    txtJoiningFee.Text = "95"
                Case "Swimming Lessons"
                    txtJoiningFee.Clear()
                    txtJoiningFee.Text = "40"
                Case "Mind And Body Training"
                    txtJoiningFee.Clear()
                    txtJoiningFee.Text = "50"
            End Select
    End Sub 
    
    Private Sub txtJoiningFee2_TextChanged(sender As Object, e As EventArgs) Handles txtJoiningFee2.TextChanged
            Select Case cbTrainingPlan2.Text
                Case "Zumba"
                    txtJoiningFee2.Clear()
                    txtJoiningFee2.Text = "40"
                Case "BasketBall Fitness"
                    txtJoiningFee2.Clear()
                    txtJoiningFee2.Text = "30"
                Case "Yoga"
                    txtJoiningFee2.Clear()
                    txtJoiningFee2.Text = "50"
    
                Case "Spa"
                    txtJoiningFee2.Clear()
                    txtJoiningFee2.Text = "25"
            End Select
    End Sub 

    And here is the Calculate button code 

    Private Sub btnCalculateMembershipDetails_Click(sender As Object, e As EventArgs) Handles btnCalculateMembershipDetails.Click
            Dim JoiningFee1 As Integer = Val(txtJoiningFee.Text)
    
            Dim JoiningFee2 As Integer = Val(txtJoiningFee2.Text)
            Dim MembershipTypeFee As Integer = Val(txtMembershipTypeFee.Text)
            Dim JoiningFeeTotal As Integer = Val(txtMembershipTotal.Text)
    
    
            JoiningFeeTotal = JoiningFee1 + MembershipTypeFee + JoiningFee2
            txtMembershipTotal.Text = Val(JoiningFeeTotal)
            If cb12months.Checked = True And cb6Months.Checked = True And cbMonthly.Checked = True Then MessageBox.Show("Please Only Select One Membership Type!")
    
            cbTrainingPlan.Items.Add()
    End Sub 

    Your help would be much appreciated! 

    Thanks. 



    Friday, May 18, 2018 5:48 PM

Answers

  • The ComboBox selection should set the TextBox's text. Otherwise you can't be sure anything is selected in the ComboBox's.

    You can use a Dictionary(Of String, Integer) to add all the classes and values to. The first item in the Dictionary is a Key and the second is a Value. You can use the Key to return the Value. There can not be two of the same keys in the Dictionary.

    Then you can loop through the Dictionary adding only the Keys to your first ComboBox. And then use the ComboBox's Selected Index Changed event to retrieve the Value, based on a Key, for the TextBox.

    It's much simpler and no select case statements or TextBox text changed events are necessary.

    Dim ComboBox1Input As New Dictionary(Of String, Integer)
    
    ComboBox1Input.Add("Weight Training", 70)
    
    
    For i = 0 To ComboBox1Input.Count - 1
        ComboBox1.Items.Add(ComboBox1Input.Keys(i))
    Next


    La vida loca

    Friday, May 18, 2018 7:17 PM

All replies

  • Hi

    When posting code, please use the code block tool from the toolbar - it help immensely when trying to read the code.

    What is your current code doing that isn't right?

    Do you have any exceptions reported? If so what are they.

    What is the result you get versus the result you want?


    Regards Les, Livingston, Scotland

    Friday, May 18, 2018 6:24 PM
  • The ComboBox selection should set the TextBox's text. Otherwise you can't be sure anything is selected in the ComboBox's.

    You can use a Dictionary(Of String, Integer) to add all the classes and values to. The first item in the Dictionary is a Key and the second is a Value. You can use the Key to return the Value. There can not be two of the same keys in the Dictionary.

    Then you can loop through the Dictionary adding only the Keys to your first ComboBox. And then use the ComboBox's Selected Index Changed event to retrieve the Value, based on a Key, for the TextBox.

    It's much simpler and no select case statements or TextBox text changed events are necessary.

    Dim ComboBox1Input As New Dictionary(Of String, Integer)
    
    ComboBox1Input.Add("Weight Training", 70)
    
    
    For i = 0 To ComboBox1Input.Count - 1
        ComboBox1.Items.Add(ComboBox1Input.Keys(i))
    Next


    La vida loca

    Friday, May 18, 2018 7:17 PM
  • Thanks So Much!
    Friday, May 18, 2018 8:50 PM