Results 1 to 4 of 4

Thread: Having problems with a list box program...

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    14

    Having problems with a list box program...

    I'm writing a list box program to calculate the Registration and Lodging for a person who does a certain workshop at a certain location and for some reason, it's working fine except when you click on "How to Interview" from the Workshop List Box.
    When the total Lodging and Registration are calculated in the Costs List Box, Lodging comes out to 0 and Registration comes out to 0.

    All the other workshops work fine with all 6 of their locations and the calculations are correct except for "How to Interview"

    I'm new to list boxes and fear this might be some kind of logic error on my part.


    Can someone who's 100x more VB savvy help me out on this one?





    Here's my code:


    Private Sub btnWorkshop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWorkshop.Click

    Dim intDays As Integer = 0
    Dim intRegistration As Integer = 0
    Dim intLodging As Integer = 0

    If lstWorkshop.SelectedItem = "Handling Stress" Then
    intDays = 3
    intRegistration = 595
    ElseIf lstWorkshop.SelectedItem = "Time Management" Then
    intDays = 3
    intRegistration = 695
    ElseIf lstWorkshop.SelectedItem = "Supervision Skills" Then
    intDays = 3
    intRegistration = 995
    ElseIf lstWorkshop.SelectedItem = "Negotiation" Then
    intDays = 5
    intRegistration = 1295
    ElseIf lstWorkshop.SelectedItem = "How to Interview" Then
    intDays = 1
    intRegistration = 395
    ElseIf lstWorkshop.SelectedItem Is Nothing Then
    MessageBox.Show("Please Select a Workshop")
    End If

    If lstLocation.SelectedItem = "Austin" Then
    intLodging = intDays * 95
    ElseIf lstLocation.SelectedItem = "Chicago" Then
    intLodging = intDays * 125
    ElseIf lstLocation.SelectedItem = "Dallas" Then
    intLodging = intDays * 110
    ElseIf lstLocation.SelectedItem = "Orlando" Then
    intLodging = intDays * 100
    ElseIf lstLocation.SelectedItem = "Phoenix" Then
    intLodging = intDays * 92
    ElseIf lstLocation.SelectedItem = "Raleigh" Then
    intLodging = intDays * 90
    ElseIf lstLocation.SelectedItem Is Nothing Then
    MessageBox.Show("Please Select a Location")
    End If

    lstCosts.Items.Add("Lodging: " & intLodging )
    lstCosts.Items.Add("Registration: " & intRegistration)


    End Sub
    End Class

  2. #2
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Re: Having problems with a list box program...

    I do not know what is in this listbox, but a problem could be the "Case" of the item. SelectedItem = "" is case sensitive, so this will not work:
    Code:
    'listbox item selected: How to interview
    If lstWorkshop.SelectedItem = "How to Interview" Then
        'Code does not get executed since the 'I' of interview is lower case.
    End If
    A trailing space could also be an issue.

    If this is the case, change the case to match, or use "SelectedIndex" instead. You could also select a case:
    Code:
    Dim intDays As Integer = 0
    Dim intRegistration As Integer = 0
    Select Case lstWorkshop.SelectedItem.ToLower().Trim()
        Case "handling stress"
            intDays = 3
            intRegistration = 595
        Case "time management"
            intDays = 3
            intRegistration = 695
        Case 'etc
    End Select
    Ow and don't forget adding proper "item selected" checks:
    Code:
    If lstWorkshop.SelectedIndex = -1 Then
        MsgBox("Please select a workshop from the list.")
    ElseIf lstLocation.SelectedIndex = -1 Then
        MsgBox("Please select the location from the list.")
    Else
        'perform your coding here
    End If
    EDIT

    Just for the moderation stuff: next time please wrap your code in [CODE] tags.

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    14

    Re: Having problems with a list box program...

    OK thanks a lot for the help.

    One more question though. I want to get select items from the 3rd list box (the one with the Registration and Lodging fees) and add them together and output that number in a label.

    Do you know how I would do that?

  4. #4
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Having problems with a list box program...

    Another thought is to store information in a table taken from a database then populate each Listbox from the table information or from XML etc. Then when a selection is made you have all the information needed to determine their selections and numerics to do calculations.

    The example below uses a static datatable rather than a database table for the demo.

    Code:
        Private Sub DemoForm_Load() Handles MyBase.Load
            Dim dt As New DataTable
            dt.Columns.AddRange(New DataColumn() _
                { _
                 New DataColumn("Class", GetType(System.String)), _
                 New DataColumn("Days", GetType(System.Int32)), _
                 New DataColumn("Registration", GetType(System.Int32)) _
                 })
    
            dt.Rows.Add(New Object() {"Handling Stress", 3, 595})
            dt.Rows.Add(New Object() {"Time Management", 3, 695})
            dt.Rows.Add(New Object() {"Supervision Skills", 3, 995})
            dt.Rows.Add(New Object() {"Negotiation", 5, 1295})
            dt.Rows.Add(New Object() {"How to Interview", 1, 395})
    
            lstWorkshop.DisplayMember = "Class"
            lstWorkshop.DataSource = dt
            lstWorkshop.SelectedIndex = -1
        End Sub
        Private Sub cmdSelectWorkShop_Click() Handles cmdSelectWorkShop.Click
            If lstWorkshop.SelectedIndex <> -1 Then
                Dim WorkShopRow = DirectCast(lstWorkshop.SelectedItem, DataRowView).Row
    
                Console.WriteLine("Class [{0}] Days [{1}] Registration [{2}]", _
                        lstWorkshop.Text, _
                        WorkShopRow.Field(Of Int32)("Days"), _
                        WorkShopRow.Field(Of Int32)("Registration"))
            Else
                MsgBox("Please Select a Workshop")
            End If
        End Sub

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