Jump to content

Working with Arrays and listbox to perform lookup

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
anglina

anglina

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
I need to display the Aisle number in a label. My big issue here is with Arrays. Outside of math class, I am, pretty terrible at them.

The idea is to select a genre from a listbox and press a button and the program performs a lookup of the array to display the aisle number according to the genre.

I need to use the listbox SelectedIndex property to find the aisle number.

This is what I have so far.

I want to understand how to do this and WHY it works rather than just an answer, although one would be appreciated..

I have struggled with Arrays in programming for years and if I don't figure it out I may get burned out.

I have the form/gui all done I am having problems with the code.






Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim tabledata(5, 2) As String
        tabledata(0, 0) = ("Aisle 1")
        tabledata(1, 0) = ("Aisle 2")
        tabledata(2, 0) = ("Aisle 3")
        tabledata(3, 0) = ("Aisle 4")
        tabledata(4, 0) = ("Aisle 5")
        tabledata(5, 0) = ("Back Wall")
        ''''''''''''''''''''''''''''''''''''''
        tabledata(0, 1) = ("Comedy")
        tabledata(1, 1) = ("Drama")
        tabledata(2, 1) = ("Action")
        tabledata(3, 1) = ("Sci-Fi")
        tabledata(4, 1) = ("Horror")
        tabledata(5, 1) = ("New Releases")
        '''''''''''''''''''''''''''''''''''''''
        tabledata(0, 2) = (0)
        tabledata(1, 2) = (1)
        tabledata(2, 2) = (2)
        tabledata(3, 2) = (3)
        tabledata(4, 2) = (4)
        tabledata(5, 2) = (5)

        Dim genre As String

        genre = ListBox1.SelectedItem

        If genre = tabledata(0, 1) Then
            Label1.Text = tabledata(0, 0)
        End If
    End Sub
End Class



#2
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
If you just want some help with arrays you can check out these two tutorials:
http://forum.codecal...-10-arrays.html
http://forum.codecal...nal-arrays.html

#3
anglina

anglina

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
I checked those first before I posted. I will go through them again and see if I get it this time.

#4
anglina

anglina

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts
I read through it again and I understand the basics it is trying to teach. I am still having a problem understanding how to implement my code though

#5
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
You could probably do something like this:

        Dim genre As String

        genre = ListBox1.SelectedItem
        For i As Integer = 0 To tabledata.GetUpperBound(0)
            If genre = tabledata(i, 1) Then
                Label1.Text = tabledata(i, 0)
                Exit For
            End If
        Next


http://forum.codecal...s-part-1-a.html

#6
anglina

anglina

    Learning Programmer

  • Members
  • PipPipPip
  • 76 posts

Vswe said:

You could probably do something like this:

        Dim genre As String

        genre = ListBox1.SelectedItem
        For i As Integer = 0 To tabledata.GetUpperBound(0)
            If genre = tabledata(i, 1) Then
                Label1.Text = tabledata(i, 0)
                Exit For
            End If
        Next
http://forum.codecal...s-part-1-a.html


Awesome! This is exactly what I was looking for.

I understand the loops, I just need to read about the .GetUpperBounds part

Thanks so much!

#7
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
Happy to help :)