Welcome to Dream.In.Code
Become a VB Expert!

Join 137,228 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,944 people online right now. Registration is fast and FREE... Join Now!




Listbox values

 
Reply to this topicStart new topic

Listbox values

Work007
20 Mar, 2007 - 03:03 PM
Post #1

New D.I.C Head
*

Joined: 20 Mar, 2007
Posts: 1


My Contributions
Hi. I really need help on how to use a value stored in a listbox.

I have a form on which it has the following code. This form allows the user to add a value to the textbox which is on this page and then takes the user to the next page:
CODE

Public Class City
    Dim frm1 As ConfirmDestination          'Our instance of form2
    Dim items As New List(Of String)    'Internal collection of items
    Dim streetitem As New List(Of String)    'Internal collection of items
    Dim postcodeitem As New List(Of String)    'Internal collection of items

Private Sub Button23_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button23.Click
        Dim newItem As String = TextBox1.Text
        items.Add(newItem)      'Add new item to our list
        'If Form2 exists then add the item to it's listbox
        If frm1 IsNot Nothing AndAlso Not frm1.IsDisposed Then frm1.ListBox2.Items.Add(newItem)


        TextBox1.Text = ""

        If frm1 Is Nothing OrElse frm1.IsDisposed Then
            frm1 = New ConfirmDestination
            frm1.ListBox2.Items.AddRange(items.ToArray)
            frm1.Show()
        Else
            frm1.Visible = True
        End If

        If newItem = "OSTERLEY" Then
            Dim Item1 As String = "ST MARY'S CRESCENT"
            streetitem.Add(Item1)      'Add new item to our list
            'If Form2 exists then add the item to it's listbox
            If frm1 IsNot Nothing AndAlso Not frm1.IsDisposed Then frm1.ListBox1.Items.Add(Item1)
        End If

        If newItem = "OSTERLEY" Then
            Dim Item2 As String = "TW74NB"
            postcodeitem.Add(Item2)
            'If Form2 exists then add the item to it's listbox
            If frm1 IsNot Nothing AndAlso Not frm1.IsDisposed Then frm1.ListBox3.Items.Add(Item2)
        End If


    End Sub


Once the user has clicked the NEXT button to go to the next form, there are three listboxes. i want to be able to go through the listbox values and say the following:

if listbox1 contains ST MARY'S CRESCENT and listbox2 contains OSTERLEY and listbox3 contains TW74NB, then display form A

HOW do i do this?? Please help!
User is offlineProfile CardPM
+Quote Post

KeyWiz
RE: Listbox Values
21 Mar, 2007 - 09:06 PM
Post #2

D.I.C Regular
Group Icon

Joined: 26 Oct, 2006
Posts: 428


Dream Kudos: 125
My Contributions
ListBox.Text is whatever has been clicked. Unless you program it to do otherwise, ListBox.Text = "" untill an item in the list is clicked.

User is offlineProfile CardPM
+Quote Post

Jayman
RE: Listbox Values
21 Mar, 2007 - 10:05 PM
Post #3

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,984



Thanked: 44 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
A listbox stores values in the same manner as an Array. Each value is accessible through an index number that starts at 0. So with that in mind you can loop through all the values stored in the listbox using a loop. You can use the Count method of the ListBox class to determine the total number of iterations of the loop.

Now you can test what the value is at a specific index in the list. To access what that value is you can use the Item method to retrieve its value, again using the index number. Now compare it to the value you are looking for, in this case I used "Test" as the value being searched for.

In this example, if the value is found it will simply display a message and exit the subroutine. This should give you a pretty good idea of what to do. You will need to do it once for each Listbox that you have, in order to check for the correct values in all of them.

You could also create Booleans to keep track of which Listbox found the correct value and set it to "True". And then use those Booleans as you stated. if boolListbox1 and boolListbox2 and boolListbox3 then display form A
CODE

Dim x As Integer
Dim testValue As String = "Test"

For x = 0 To Me.ListBox1.Items.Count - 1
      If Me.ListBox1.Items.Item(x) = testValue Then
            MessageBox.Show("Found it")
            Exit Sub
      End If
Next x

User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/4/08 02:51PM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month