Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB Visual-Studio
'Project Name:              Ship With Us
'Project Purpose:           Input Zip Code and find the associated shipping cost
'Created/Revised by:        

Option Explicit On
Option Strict Off
Option Infer Off
 
Public Class MainForm
 
    Private Sub MainForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'fills the list box with zip codes
        shippingoneListBox.Items.Add("60611")
        shippingoneListBox.Items.Add("60234")
        shippingoneListBox.Items.Add("56789")
        shippingoneListBox.Items.Add("23467")
        shippingoneListBox.Items.Add("60543")
        shippingoneListBox.Items.Add("60561")
        shippingoneListBox.Items.Add("55905")
        shippingoneListBox.Items.Add("89567")
        'fills the list box with second set of zip codes
        shippingtwoListBox.Items.Add("50978")
        shippingtwoListBox.Items.Add("78432")
        shippingtwoListBox.Items.Add("98432")
        shippingtwoListBox.Items.Add("97654")
        shippingtwoListBox.Items.Add("20245")
    End Sub
 
    Private Sub exitButton_Click(sender As Object, e As System.EventArgs) Handles exitButton.Click
        Me.Close()
    End Sub
 
    Private Sub displayshipButton_Click(sender As Object, e As System.EventArgs) Handles displayshipButton.Click
        'determine whether a list box contains a specific zip code
        Dim id As String = String.Empty
        'if the user picks a zip code in ListBox1 then the shipping charge is $15.00
        'and if zip code is chose in  ListBox2  shipping charge is $20.00

        If (shippingoneListBox.FindString(zipTextBox.Text >= 0)) Then
            shippingLabel.Text = "Shipping is $15"
            shippingLabel.Show()
        ElseIf (shippingtwoListBox.FindString(zipTextBox.Text >= 0)) Then
            shippingLabel.Text = "Shipping is $20"
            shippingLabel.Show()
        Else
            shippingLabel.Text = "The zipcode was not found!"
            shippingLabel.Show()
        End If
    End Sub
End Class
 
When entering a zip code that is listed in "shippingtwoListBox" it will still tell me the shipping cost should be $15 when in fact it should be $20. Do I have something coded wrong that it isn't searching the second list box? Also, when I enter a zip code that isn't added to the listboxes the label isn't displaying "The zipcode was not found!"
 
Any help would be much appreciated!
Posted 20-Apr-13 7:44am
Comments
Zach Blomstrom at 20-Apr-13 12:58pm
   
Even if I say (shippingtwoListBox.FindString(zipTextBox.Text = "50978")) it still tells me the shipping cost is $15 when that zip code is in the $20 listbox.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You have your closing parenthesis in the wrong place causing the first IF to be always true.
 
I also added a line of code to remove spaces from front and back of ziptextbox.Text in case user accidentally types spaces before or after the zip code.
 
Furthermore, the Label.Show method is for internal use only, You do not need it and should not use it in your code. See the documentation for Label.Show[^]
 
Corrected version
'if the user picks a zip code in ListBox1 then the shipping charge is $15.00
'and if zip code is chose in  ListBox2  shipping charge is $20.00

ziptextbox.Text = ziptextbox.Text.Trim
If shippingonelistbox.FindString(ziptextbox.Text) >= 0 Then
    shippinglabel.Text = "Shipping is $15"
ElseIf shippingtwolistbox.FindString(ziptextbox.Text) >= 0 Then
    shippinglabel.Text = "Shipping is $20"
Else
    shippinglabel.Text = "The zipcode was not found!"
End If
 
Tested: Visual Studio 2012
  Permalink  
v8
Comments
Zach Blomstrom at 21-Apr-13 11:43am
   
Thank-you very much!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 21 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100