working with combobox and listbox

how to write code for items in combobox

Page 1 of 1

11 Replies - 9626 Views - Last Post: 27 April 2010 - 07:24 AM Rate Topic: -----

#1 vkod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 31-March 10

working with combobox and listbox

Posted 22 April 2010 - 04:05 PM

Public Class Form1
    Dim just(5, 3) As Integer
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim row As Integer
        Dim col As Integer
        Dim vac As String()

        vac = New String(just.GetUpperBound(0)) {}


        ListBox1.Items.Clear()
        For row = 0 To just.GetUpperBound(0) - 1
            For col = 1 To just.GetUpperBound(1)


                vac(row) = "Vacant"
                If row = 0 And col = 1 Then
                    vac(row) = "Reserved"


                ElseIf row = 0 And col = 2 Then
                    vac(row) = "Reserved"

                ElseIf row = 2 And col = 1 Then
                    vac(row) = "Reserved"

                ElseIf row = 2 And col = 2 Then
                    vac(row) = "Reserved"
                End If

                ListBox1.Items.Add(row & "   " & Chr(64 + col).ToString & "   " & vac(row.ToString) & " ")
            Next

        Next



    End Sub

    Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick

        Dim reserd As String = ListBox1.SelectedItem.ToString
        If reserd.ToLower.EndsWith("vacant ") Then
            ListBox1.Items(ListBox1.SelectedIndex) = reserd.ToString.Substring(0, reserd.Length - 7) & "Reserved "
        Else
            ListBox1.Items(ListBox1.SelectedIndex) = reserd.ToString.Substring(0, reserd.Length - 9) & "Vacant "
        End If

    End Sub


    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim col As Integer
        Dim row As Integer
        Dim total As Integer
        For row = 0 To 5 - 1
            For col = 1 To 3
                total = row * col
                TextBox1.Text = total
            Next
        Next
    End Sub
    
End Class


I will want to include a combobox with some options of a. alltheseat b. Vacantseats., c. Reservedseats.
My question is how do I write the code that when vacant seat is selected from the combobox it will list only the vacant seats in the listbox and when reservedseats is selected it will list only on the reserved seats.
Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: working with combobox and listbox

#2 vkod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 31-March 10

Re: working with combobox and listbox

Posted 23 April 2010 - 12:14 AM

what am asking for is how to write the code for items in combobox.
for instance
if I want to use
Select case comboboxitems
case alltheseats
........ ' what code will i put here
case allvacantseats
.........' code
case allreservedseat
..... 'code
i dont know how to go about doing this with the above program.
thank you for helping
Was This Post Helpful? 0
  • +
  • -

#3 vkod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 31-March 10

Re: working with combobox and listbox

Posted 23 April 2010 - 06:33 AM

somebody shuld help me plz.
Was This Post Helpful? 0
  • +
  • -

#4 LoveIsNull  Icon User is offline

  • Recovering D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 646
  • Joined: 10-March 09

Re: working with combobox and listbox

Posted 23 April 2010 - 09:30 AM

Somebody should be a little more patient...
This can be done in various ways. The main thing is that you need a place to store your output instead of just adding it to a listbox. You put it all in a variable instead, this way you can refer to it and use it to help decide which information the user wants displayed. Otherwise you would probably just lose information, for instance if the person decides to view only the 'reserved' seats and then switches back to 'all'. If the data was only ever stored in a listbox you wouldn't be able to do this because you had previously removed everything except what was reserved.

To keep things simple I would suggest using a simple structure with two members: the string with the output and a boolean to indicate if it is vacant or reserved. You can then utilize generics here and declare a generic list of your structure.
    Structure Seat
        Dim output As String
        Dim occupied As Boolean
    End Structure
    'Generic list to store data...
    Dim seatCol As New Collections.Generic.List(Of Seat)

Make some modifications to your Button1_click handler and add a new method that will contain the code to load the info. Also add your combobox (mine is named cmb) and add three values to it (All, Vacant, Reserved).
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim row As Integer
        Dim col As Integer
        Dim vac As String()

        vac = New String(just.GetUpperBound(0)) {}
        seatCol = New Generic.List(Of Seat)

        For row = 0 To just.GetUpperBound(0) - 1
            For col = 1 To just.GetUpperBound(1)
                Dim curSeat As New Seat
                curSeat.occupied = False    'Start off empty

                vac(row) = "Vacant"
                If row = 0 And col = 1 Then     'Set to true if reserved
                    vac(row) = "Reserved"
                    curSeat.occupied = True
                ElseIf row = 0 And col = 2 Then
                    vac(row) = "Reserved"
                    curSeat.occupied = True
                ElseIf row = 2 And col = 1 Then
                    vac(row) = "Reserved"
                    curSeat.occupied = True
                ElseIf row = 2 And col = 2 Then
                    vac(row) = "Reserved"
                    curSeat.occupied = True
                End If
                'Store your output in curSeat.output
                curSeat.output = row & "   " & Chr(64 + col).ToString & "   " & vac(row.ToString) & " "
                'Add this instance of the seat structure to your collection
                seatCol.Add(curSeat)
            Next
        Next

        LoadOut()   'Load output
    End Sub

    Private Sub LoadOut()
        ListBox1.Items.Clear()
        For Each cSeat In seatCol       'Go through our seat collection
            If cmb.SelectedIndex = 1 Then       'Vacant only
                If cSeat.occupied = False Then
                    'Add output to listbox
                    ListBox1.Items.Add(cSeat.output)
                End If
            ElseIf cmb.SelectedIndex = 2 Then   'Reserved only
                If cSeat.occupied = True Then
                    ListBox1.Items.Add(cSeat.output)
                End If
            Else                                 'Add them all
                ListBox1.Items.Add(cSeat.output)
            End If
        Next
    End Sub

Finally you need to handle the SelectedIndexChanged event for your combobox. In there you simply make a call to LoadOut().
Was This Post Helpful? 0
  • +
  • -

#5 vkod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 31-March 10

Re: working with combobox and listbox

Posted 23 April 2010 - 11:45 AM

@ LoveIsNull , Thanks a lot. I will crosscheck and work with your method now. I will see how it goes.
Was This Post Helpful? 0
  • +
  • -

#6 vkod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 31-March 10

Re: working with combobox and listbox

Posted 23 April 2010 - 12:42 PM

@ LoveIsNull, I can see now that the code you give me is not interactive. I want it to be user interactive. Where the user can doubleclic to reserve or cancel a seat.
How do I add it with the method you gave me.
meanwhile, it works perfect for the selection of vacant or reserve seats.
thanks

This post has been edited by vkod: 23 April 2010 - 12:45 PM

Was This Post Helpful? 0
  • +
  • -

#7 vkod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 31-March 10

Re: working with combobox and listbox

Posted 23 April 2010 - 01:23 PM

I have tried to add my listbox code and it work out for doubleclicking but as LoveIsNull said, it is not stored and so does not have any effect when any option is selected from combobox.
Any suggestion?
Thanks
Was This Post Helpful? 0
  • +
  • -

#8 vkod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 31-March 10

Re: working with combobox and listbox

Posted 25 April 2010 - 03:17 PM

Anyone to still help out here. you dont need to use my procedures. you can suggest another method for me
thanks
Was This Post Helpful? 0
  • +
  • -

#9 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1623
  • View blog
  • Posts: 5,194
  • Joined: 25-September 09

Re: working with combobox and listbox

Posted 25 April 2010 - 06:53 PM

vkod, I think the biggest problem with you receiving any answers is your not showing anything you've attempted to do what you're wanting to do with the combobox. It appears that you are just waiting on someone to hand you code. Don't mean to sound harsh but all the code I've seen added since you started topics on this has been submitted by someone else and then you ask another question and wait for the code.

Here is the main problem that you have to overcome.

You are populating a listbox from a button click event that either lists a room as Reserved or Vacant. Then you want a combobbox so that you can select All, Vacant or Reserved. In order to do this, that list is going to have to be saved. This can be done by either iterating through the listbox and saving it to an array, or using List (of T) to create a saved version of what is in the list. Then you can iterate through the "Stored" data and check it against the combobox selection to see if you want to add that element of the data to the listbox.

The first thing I would work on at this point is to show us what you are doing to save the list so that you can re-construct it
Was This Post Helpful? 0
  • +
  • -

#10 vkod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 31-March 10

Re: working with combobox and listbox

Posted 26 April 2010 - 12:09 AM

View PostCharlieMay, on 25 April 2010 - 05:53 PM, said:

vkod, I think the biggest problem with you receiving any answers is your not showing anything you've attempted to do what you're wanting to do with the combobox. It appears that you are just waiting on someone to hand you code. Don't mean to sound harsh but all the code I've seen added since you started topics on this has been submitted by someone else and then you ask another question and wait for the code.

Here is the main problem that you have to overcome.

You are populating a listbox from a button click event that either lists a room as Reserved or Vacant. Then you want a combobbox so that you can select All, Vacant or Reserved. In order to do this, that list is going to have to be saved. This can be done by either iterating through the listbox and saving it to an array, or using List (of T) to create a saved version of what is in the list. Then you can iterate through the "Stored" data and check it against the combobox selection to see if you want to add that element of the data to the listbox.

The first thing I would work on at this point is to show us what you are doing to save the list so that you can re-construct it


Well, I do try first before I post the question here. and I sometimes get it on my own but want to confirm too as Im novice to the VB.net programming.
As for the combobox selection options, it work fine when all, Vacant or Reserved is selected but my problem is that any change made during double clicking of any item in the listbox are not saved and therefore, dont work when any option is selected from the combobox.
I just need to get the basis of this problem then I can fully stand on my own.
Thank you for your effort so far.
Was This Post Helpful? 0
  • +
  • -

#11 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1623
  • View blog
  • Posts: 5,194
  • Joined: 25-September 09

Re: working with combobox and listbox

Posted 26 April 2010 - 04:43 AM

OK, then in knowing this, I apologize and here is what I would try.

In the double-click event you can check your stored data and using the substring of the 2 columns of numbers since they are unique, iterate through your stored information and when it matches, update it with the new information.

So double-click to change vacancy status (at this point you have a listindex)

then for next through your saved information until the numbers match

then update the saved information from the new value of the selected listitem.

Hope this is clear.
Was This Post Helpful? 0
  • +
  • -

#12 vkod  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 31-March 10

Re: working with combobox and listbox

Posted 27 April 2010 - 07:24 AM

thanks guys! I am begining to understand some principle about VB.Net.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1