5 Replies - 19439 Views - Last Post: 22 November 2008 - 01:20 AM Rate Topic: -----

#1 XMEGA  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 135
  • Joined: 17-November 08

arrays and listbox, displaying arrays in the listbox?

Posted 21 November 2008 - 10:01 PM

I am making a reservation system

so when I click the select button/seat

I have two questions asked...

enterName = InputBox("Enter Name (Ex: John Doe): ")
selectspot = InputBox("Pick a Spot: ")

I have an array like so...

Dim ids(,) As String = {{enterName , "1"}, _
{enterName , 2}, _

now in the listbox if "1" is selected I want it to display in the listbox for spot 1

"firstname lastname, "1""

I am adding data entered like so...

Me.xSpot1ListBox.Items.Add(ids)

at the very top of the listbox in index 0 I believe...I am able to display just 1 either name or the spot number...just not both nicely beside each other...

Is This A Good Question/Topic? 0
  • +

Replies To: arrays and listbox, displaying arrays in the listbox?

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4187
  • View blog
  • Posts: 11,845
  • Joined: 18-April 07

Re: arrays and listbox, displaying arrays in the listbox?

Posted 21 November 2008 - 10:40 PM

You need to show us more of how you are adding to the listbox because what you have now is an array of arrays which when you put into a listbox using just the name of this array you are going to get String[,] Array showing up in the listbox. Why? Because you are adding an array of string arrays.

So show us the code where you take a value from your ids array and put it into the listbox and we can get a better idea of what you might be doing wrong. :)
Was This Post Helpful? 0
  • +
  • -

#3 XMEGA  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 135
  • Joined: 17-November 08

Re: arrays and listbox, displaying arrays in the listbox?

Posted 21 November 2008 - 10:50 PM

View PostMartyr2, on 21 Nov, 2008 - 09:40 PM, said:

You need to show us more of how you are adding to the listbox because what you have now is an array of arrays which when you put into a listbox using just the name of this array you are going to get String[,] Array showing up in the listbox. Why? Because you are adding an array of string arrays.

So show us the code where you take a value from your ids array and put it into the listbox and we can get a better idea of what you might be doing wrong. :)


okay, so this is what happens when you click select...yes I do get "String[,] Array" displaying up instead of what I want...basically what I type into the first inputbox box and the second inputbox should determine the listbox to use in this example listbox1...so when the 2nd input is "1" just take the string from inputbox1 and 2 and display it in the listbox for spot "1"...

	Private Sub xSelectButton_Click(ByVal sender As System.Object, _
		ByVal e As System.EventArgs) Handles xSelectButton.Click

		Dim enterName As String
		Dim selectspot As String

		enterName = InputBox("Enter Name (Ex: John Doe): ")
		selectspot = InputBox("Pick a Spot: ")

		Dim ids(,) As String = {{enterName, selectspot}, _
								{enterName, selectspot}}

		If selectspot = "1" Then
			Me.xSpot1ListBox.Items.Clear()
			Me.xSpot1ListBox.Items.Add(ids)
		End If

	End Sub



should I have two arrays for this kind of thing? 1 for the names and one for the spots?
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4187
  • View blog
  • Posts: 11,845
  • Joined: 18-April 07

Re: arrays and listbox, displaying arrays in the listbox?

Posted 21 November 2008 - 11:15 PM

Well remember when you add items to a listbox they are essentially put into an array. From your code I see there, you don't really need an array of arrays. Check out this code for your button...

' Define this outside the select button
private ids(10) As String

Private Sub xSelectButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim enterName As String
        Dim selectspot As String

        ' Collect the name and spot
        enterName = InputBox("Enter Name (Ex: John Doe): ")
        selectspot = InputBox("Pick a Spot: ")

        ' Use the spot chosen to input the name and spot together
        
        ids(selectspot) = enterName & " " & selectspot

        ' Then for each item in the string, enter an empty string or the reserved spot
        ' Result is that john doe 1 is added to index 1 of the listbox.
         Me.xSpot1ListBox.Items.Clear()

        For Each name As String In ids
            If String.IsNullOrEmpty(name) Then
                Me.xSpot1ListBox.Items.Add("")
            Else
                Me.xSpot1ListBox.Items.Add(name)
            End If
        Next

    End Sub



Here we prompt for a name and spot then using that spot we put the name and spot number into a string and place it into the ids array. We then loop through the array and either enter an empty string for open reservation spots or the actual name into the listbox. The result is that if we specify John Doe at spot 1, he will be in index 1 of the listbox. Of course if you want him to be in index 0, subtract 1 from the spot they specify.

One thing to watch out for is that you test the spot they entered is not bigger than the array. So if they entered 100, it should catch that as an error since there are only 10 spots available.

Hope this is what you were looking to do. :)

This post has been edited by Martyr2: 21 November 2008 - 11:16 PM

Was This Post Helpful? 1
  • +
  • -

#5 XMEGA  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 135
  • Joined: 17-November 08

Re: arrays and listbox, displaying arrays in the listbox?

Posted 21 November 2008 - 11:46 PM

It works like a charm :^: but I am a bit confused is this a form of an array? The format looks different then what I have seen before...Doing this way how can I do this for 2 listboxes? keep in mind this is a reservation/booking system. I changed ur code to make it work with what I will need it to do at the end just is there a better way with arrays?

		Dim enterName As String
		Dim selectspot As String

		enterName = InputBox("Enter Name (Ex: John Doe): ")
		selectspot = InputBox("Pick a Spot: ")

		ids(CInt(selectspot)) = enterName & " @ Spot" & selectspot

		For Each name As String In ids
			If selectspot = "1" Then
				If String.IsNullOrEmpty(name) Then
					Me.xSpot1ListBox.Items.Add("")
				Else
					Me.xSpot1ListBox.Items.Clear()
					Me.xSpot1ListBox.Items.Add(name)
				End If
			End If

			If selectspot = "2" Then
				If String.IsNullOrEmpty(name) Then
					Me.xSpot2ListBox.Items.Add("")
				Else
					Me.xSpot2ListBox.Items.Clear()
					Me.xSpot2ListBox.Items.Add(name)
				End If
			End If
		Next



At its very basic level this is just the select part I will have remove(delete entry), and update(waiting list when select spot is taken already) button. Doing it this way will I be able to search a name in the listbox1 if full...and be able to delete the entry?
Was This Post Helpful? 0
  • +
  • -

#6 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4187
  • View blog
  • Posts: 11,845
  • Joined: 18-April 07

Re: arrays and listbox, displaying arrays in the listbox?

Posted 22 November 2008 - 01:20 AM

Well you are using a standard array, a 1 dimensional straight forward array. Nothing fancy about it. Before you had a 2 dimensional array which was really not needed.

Now when you load the array into the listbox you can either search the listbox using a simple loop or search the array ids... whichever way you like.

I am a bit confused as to what you mean by better way with arrays because you are using an array. ids is still an array, we just changed its dimension from 2 to 1. Now if you are talking about some how reducing the need to determine the spot and add to the appropriate listbox, you could create an array of listbox items.

Either way you are looking like you are moving forward again and have some ideas to run on. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1