13 Replies - 1844 Views - Last Post: 23 April 2013 - 03:38 PM Rate Topic: -----

#1 Crypters  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 01-April 13

Adding different listbox items when Item is selected in the combo box

Posted 22 April 2013 - 01:17 AM

So for my code, I am using a structure array because I was directed to do so. I have the combo box populated through the structure, but I am having a hard time trying to figure out how to populate the list box when a different combo box item is selected. For example, If the user chooses "Mammal" in the combo box, I want the list box to display "Cat" and "Dog". Then the user can select "Fish", which would clear the listbox of the Cat and dog, and instead display the items "Guppy" and "Tilipia".

Structure AninmalInfo
    Public intPrice As Integer
    Public CritterType As String
    Public Animal As String
End Structure

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim pricelist(6) As AninmalInfo
        Dim strSearchForAnimal As String


        'Populate structure 
        pricelist(0).Animal = "Dog"
        pricelist(0).CritterType = "Mammal"
        pricelist(0).intPrice = 50
        pricelist(1).Animal = "Cat"
        pricelist(1).CritterType = "Mammal"
        pricelist(1).intPrice = 25
        pricelist(2).Animal = "Parrot"
        pricelist(2).CritterType = "Bird"
        pricelist(2).intPrice = 30
        pricelist(3).Animal = "Tilapia"
        pricelist(3).CritterType = "Fish"
        pricelist(3).intPrice = 10
        pricelist(4).Animal = "Guppy"
        pricelist(4).CritterType = "Fish"
        pricelist(4).intPrice = 5
        pricelist(5).Animal = "Turtle"
        pricelist(5).CritterType = "Amphibian"
        pricelist(5).intPrice = 25
        pricelist(6).Animal = "Gator"
        pricelist(6).CritterType = "Amphibian"
        pricelist(6).intPrice = 40

        'Populates the Combo box and if the animal is already 
        'selected once, then the loop cancels out one
        For intX As Integer = 0 To pricelist.GetUpperBound(0)
            If Not cboCritterType.Items.Contains(pricelist(intX).CritterType) Then
                cboCritterType.Items.Add(pricelist(intX).CritterType)
            End If
        Next
      
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub
   
    Private Sub cboCritterType_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles                   cboCritterType.SelectedValueChanged
        'Me attempting to have the combo box item change add the animals to the listbox
         For intC As Integer = 0 To pricelist.GetUpperBound(0)
            If pricelist(intC).CritterType.Contains(cboCritterType.Text(intC)) Then
                lstCritters.Items.Add(pricelist(intC).Animal)
            End If
        Next
    End Sub
End Class 


Is This A Good Question/Topic? 0
  • +

Replies To: Adding different listbox items when Item is selected in the combo box

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1726
  • View blog
  • Posts: 5,704
  • Joined: 25-September 09

Re: Adding different listbox items when Item is selected in the combo box

Posted 22 April 2013 - 05:41 AM

You appear to be really close.

The first thing that sticks out is that pricelist is declared inside the form_load and is not available to the selectedValueChanged event where you appear to be trying to use it. Try moving that to a wider scope (ie, declare it in the class outside of any subs)
Was This Post Helpful? 0
  • +
  • -

#3 Crypters  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 01-April 13

Re: Adding different listbox items when Item is selected in the combo box

Posted 22 April 2013 - 09:57 AM

I tried moving it to a global setting but it is stating that I can't because a declaration is necessary.
Was This Post Helpful? 0
  • +
  • -

#4 Crypters  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 01-April 13

Re: Adding different listbox items when Item is selected in the combo box

Posted 22 April 2013 - 10:20 AM

Ya, I am totally clueless on how to access the structure's pricelist items.
Was This Post Helpful? 0
  • +
  • -

#5 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1726
  • View blog
  • Posts: 5,704
  • Joined: 25-September 09

Re: Adding different listbox items when Item is selected in the combo box

Posted 22 April 2013 - 11:09 AM

You tried moving what?

Dim pricelist(6) as AnimalInfo ?

or the 'Populate structure code?

You declare in the wider scope and then you can still fill the elements in the Form_Load

Try moving the Dim pricelist(6) as AnimalInfo line right up under your Public Class ??? line.
Was This Post Helpful? 0
  • +
  • -

#6 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1726
  • View blog
  • Posts: 5,704
  • Joined: 25-September 09

Re: Adding different listbox items when Item is selected in the combo box

Posted 22 April 2013 - 11:16 AM

also this doesn't look right
         For intC As Integer = 0 To pricelist.GetUpperBound(0)
            If pricelist(intC).CritterType.Contains(cboCritterType.Text(intC)) Then
                lstCritters.Items.Add(pricelist(intC).Animal)
            End If
         Next


This will probably throw an index out of bounds but it will confuse you because it's not talking about your array of structure. It's actually talking about the combobox.

IntC will increment through the array of the structure but you use cboCritterType.Text(intC) meaning that you want an element of the char Array of the .Text string. This is probably not what you're tring to achieve. I would think you would just want cboCritterType.Text there, as it appears you're saying if the crittertype contains the word in the combobox then add it to the list.

You could have probably just used = there to see if what you have in the combobox = the crittertype you've found in the loop, then add it.

Also... you might want to look at somewhere to clear that list before refilling it with the new combobox selection.

This post has been edited by CharlieMay: 22 April 2013 - 11:20 AM

Was This Post Helpful? 0
  • +
  • -

#7 Crypters  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 01-April 13

Re: Adding different listbox items when Item is selected in the combo box

Posted 22 April 2013 - 11:24 AM

Oh, it was because I was trying to move the
  pricelist(0).Animal = "Dog"
        pricelist(0).CritterType = "Mammal"
        pricelist(0).intPrice = 50
        pricelist(1).Animal = "Cat"
        pricelist(1).CritterType = "Mammal"
        pricelist(1).intPrice = 25
        pricelist(2).Animal = "Parrot"
        pricelist(2).CritterType = "Bird"
        pricelist(2).intPrice = 30
        pricelist(3).Animal = "Tilapia"
        pricelist(3).CritterType = "Fish"
        pricelist(3).intPrice = 10
        pricelist(4).Animal = "Guppy"
        pricelist(4).CritterType = "Fish"
        pricelist(4).intPrice = 5
        pricelist(5).Animal = "Turtle"
        pricelist(5).CritterType = "Amphibian"
        pricelist(5).intPrice = 25
        pricelist(6).Animal = "Gator"
        pricelist(6).CritterType = "Amphibian"
        pricelist(6).intPrice = 40
[/code} to the public class for as well and it wasn't allowing me to. So now the only problem is with my loop for my combobox selected_value change. [Code]For intC As Integer = 0 To pricelist.GetUpperBound(0)
            If pricelist(intC).CritterType.Equals(cboCritterType.Text(intC)) Then
                lstCritters.Items.Add(pricelist(intC).Animal)
            End If
        Next
    End Sub
It says that the index is out of range, but sense I used .Getupperbound, shouldn't it have already known the index max? Sorry for all the questions.

Thank you!!

Awesome, I got it up and running. Now I have the solid foundation that I was looking for! Thank you Charlie !!
Was This Post Helpful? 0
  • +
  • -

#8 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1726
  • View blog
  • Posts: 5,704
  • Joined: 25-September 09

Re: Adding different listbox items when Item is selected in the combo box

Posted 22 April 2013 - 11:38 AM

OK, now as far as how to get your price. Here is where you're going wrong.

In the structure, I would override the .ToString function to return the Animal.

This way, you can add the animalInfo to the list keeping the structure intact.

You have a structure that contains 3 fields.

Populate the combobox like you are currently doing.

Then instead of adding the Animal names to the listbox you would

For each item as AnimalInfo in PriceList 'See we are grabbing the entire Animal (Animal, Type, Price) 
   if itm.CritterType = cboCritterType.Text Then 'If the current animal is this type of critter
      LstCritters.Items.Add(itm) 'Add that animal structure to the list (not just the name of the animal)
   end if
Next


Now with the Override returning the name, it will appear the same to you. But underneath, each item in that list is a structure of the entire animal that it displays.

Which means you could use something like.
sub lstCritters_SelectedIndexChanged(......)

MsgBox(CType(lstCritters.SelectedItem, AninmalInfo).IntPrice) 'notice when you hit that last . you have all the fields avail
end sub


So what did that do?

It took the Object (structure) that you put in the listbox and cast it back into an actual Type (AnimalInfo) so that you could reference its properties.
Was This Post Helpful? 0
  • +
  • -

#9 Crypters  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 01-April 13

Re: Adding different listbox items when Item is selected in the combo box

Posted 22 April 2013 - 11:55 AM

ohhhh, Okay I see what you mean. I actually did it a little different than that. Here is what I am done with
Option Strict Off
Option Explicit On
Option Infer Off

Public Structure AninmalInfo
    Public intPrice As Integer
    Public CritterType As String
    Public Animal As String

End Structure
Public Class Form1
    Dim pricelist(6) As AninmalInfo
       
    Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Populate structure 
        pricelist(0).Animal = "Dog"
        pricelist(0).CritterType = "Mammal"
        pricelist(0).intPrice = 50
        pricelist(1).Animal = "Cat"
        pricelist(1).CritterType = "Mammal"
        pricelist(1).intPrice = 25
        pricelist(2).Animal = "Parrot"
        pricelist(2).CritterType = "Bird"
        pricelist(2).intPrice = 30
        pricelist(3).Animal = "Tilapia"
        pricelist(3).CritterType = "Fish"
        pricelist(3).intPrice = 10
        pricelist(4).Animal = "Guppy"
        pricelist(4).CritterType = "Fish"
        pricelist(4).intPrice = 5
        pricelist(5).Animal = "Turtle"
        pricelist(5).CritterType = "Amphibian"
        pricelist(5).intPrice = 25
        pricelist(6).Animal = "Gator"
        pricelist(6).CritterType = "Amphibian"
        pricelist(6).intPrice = 40

        'Populates the Combo box and if the animal is already 
        'selected once, then the loop cancels out one
        For intX As Integer = 0 To pricelist.GetUpperBound(0)
            If Not cboCritterType.Items.Contains(pricelist(intX).CritterType) Then
                cboCritterType.Items.Add(pricelist(intX).CritterType)
            End If
        Next
    End Sub

    Private Sub cboCritterType_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboCritterType.SelectedValueChanged
        lstCritters.Items.Clear()
        lblCritterType.Text = " "

        For intC As Integer = 0 To pricelist.GetUpperBound(0)
            If pricelist(intC).CritterType.Equals(cboCritterType.Text) Then
                lstCritters.Items.Add(pricelist(intC).Animal)
            End If
        Next
        'Obtains the number of items in the listbox and puts it into the label
        lblCritterType.Text = lstCritters.Items.Count.ToString

    End Sub

    Private Sub lstCritters_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstCritters.SelectedValueChanged
        'Declare the intB as comparative 
        For intB As Integer = 0 To pricelist.GetUpperBound(0)

            'This checks the pricelist structure and sets its according index with the price. 
            'of the array, like comparing "Dog" to "Dog".
            If pricelist(intB).Animal.Equals(lstCritters.SelectedItem) Then
                lblPrice.Text = pricelist(intB).intPrice.ToString("C2")
                Exit Sub
            End If
        Next
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub ResetAllToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ResetAllToolStripMenuItem.Click
        lstCritters.Items.Clear()
        lblCritterType.Text = " "
        cboCritterType.SelectedIndex = -1
        lblPrice.Text = " "
    End Sub
End Class



ohhhh, Okay I see what you mean. I actually did it a little differently than that. Here is what I am done with
Option Strict Off
Option Explicit On
Option Infer Off

Public Structure AninmalInfo
    Public intPrice As Integer
    Public CritterType As String
    Public Animal As String

End Structure
Public Class Form1
    Dim pricelist(6) As AninmalInfo
       
    Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Populate structure 
        pricelist(0).Animal = "Dog"
        pricelist(0).CritterType = "Mammal"
        pricelist(0).intPrice = 50
        pricelist(1).Animal = "Cat"
        pricelist(1).CritterType = "Mammal"
        pricelist(1).intPrice = 25
        pricelist(2).Animal = "Parrot"
        pricelist(2).CritterType = "Bird"
        pricelist(2).intPrice = 30
        pricelist(3).Animal = "Tilapia"
        pricelist(3).CritterType = "Fish"
        pricelist(3).intPrice = 10
        pricelist(4).Animal = "Guppy"
        pricelist(4).CritterType = "Fish"
        pricelist(4).intPrice = 5
        pricelist(5).Animal = "Turtle"
        pricelist(5).CritterType = "Amphibian"
        pricelist(5).intPrice = 25
        pricelist(6).Animal = "Gator"
        pricelist(6).CritterType = "Amphibian"
        pricelist(6).intPrice = 40

        'Populates the Combo box and if the animal is already 
        'selected once, then the loop cancels out one
        For intX As Integer = 0 To pricelist.GetUpperBound(0)
            If Not cboCritterType.Items.Contains(pricelist(intX).CritterType) Then
                cboCritterType.Items.Add(pricelist(intX).CritterType)
            End If
        Next
    End Sub

    Private Sub cboCritterType_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboCritterType.SelectedValueChanged
        lstCritters.Items.Clear()
        lblCritterType.Text = " "

        For intC As Integer = 0 To pricelist.GetUpperBound(0)
            If pricelist(intC).CritterType.Equals(cboCritterType.Text) Then
                lstCritters.Items.Add(pricelist(intC).Animal)
            End If
        Next
        'Obtains the number of items in the listbox and puts it into the label
        lblCritterType.Text = lstCritters.Items.Count.ToString

    End Sub

    Private Sub lstCritters_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstCritters.SelectedValueChanged
        'Declare the intB as comparative 
        For intB As Integer = 0 To pricelist.GetUpperBound(0)

            'This checks the pricelist structure and sets its according index with the price. 
            'of the array, like comparing "Dog" to "Dog".
            If pricelist(intB).Animal.Equals(lstCritters.SelectedItem) Then
                lblPrice.Text = pricelist(intB).intPrice.ToString("C2")
                Exit Sub
            End If
        Next
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub ResetAllToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ResetAllToolStripMenuItem.Click
        lstCritters.Items.Clear()
        lblCritterType.Text = " "
        cboCritterType.SelectedIndex = -1
        lblPrice.Text = " "
    End Sub
End Class


Sorry for the double post. I don't know how to edit my posts or delete them......is that option even possible?

This post has been edited by AdamSpeight2008: 23 April 2013 - 05:22 AM

Was This Post Helpful? 0
  • +
  • -

#10 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Adding different listbox items when Item is selected in the combo box

Posted 23 April 2013 - 05:28 AM

Have you also considered add a constructor to the AninmalInfo structure

Public Structure AninmalInfo
    Public intPrice As Integer
    Public CritterType As String
    Public Animal As String

    Public Sub New(Animal As String, GritterType As String, Price As Integer)
      Me.Animal = Animal
      Me.GritterType = GritterType
      Me.intPrice = Price
    End Sub

End Structure



Now you can define what an AninmalInfo with it parameters in one go.

   'Populate structure 
        pricelist(0) = New AninmalInfo( "Dog" , "Mammal" , 50 )
        pricelist(1) = New AninmalInfo( "Cat" , "Mammal" , 25 )
        pricelist(2) = New AninmalInfo( "Parrot" , "Bird" , 30 )
        pricelist(3) = New AninmalInfo( "Tilapia" , "Fish" , 10 )
        pricelist(4) = New AninmalInfo( "Guppy" , "Fish" , 5 )
        pricelist(5) = New AninmalInfo( "Turtle" , "Amphibian" , 25 )
        pricelist(6) = New AninmalInfo( "Gator" , "Amphibian" , 40 )


Was This Post Helpful? 0
  • +
  • -

#11 Crypters  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 01-April 13

Re: Adding different listbox items when Item is selected in the combo box

Posted 23 April 2013 - 11:12 AM

I didn't know that you could even do that Adam. It would make it a lot easier to refer to those items like that.
Was This Post Helpful? 0
  • +
  • -

#12 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6379
  • View blog
  • Posts: 25,774
  • Joined: 12-December 12

Re: Adding different listbox items when Item is selected in the combo box

Posted 23 April 2013 - 12:39 PM

View PostCrypters, on 23 April 2013 - 06:12 PM, said:

I didn't know that you could even do that Adam. It would make it a lot easier to refer to those items like that.

This can be achieved without a constructor as well:

priceList(0) = New AnimalInfo With {.Animal = "Dog", _ 
    .CritterType = "Mammal", .intPrice = 50}

The array can even be declared and filled in one statement :)

This post has been edited by andrewsw: 23 April 2013 - 12:39 PM

Was This Post Helpful? 0
  • +
  • -

#13 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Adding different listbox items when Item is selected in the combo box

Posted 23 April 2013 - 02:44 PM

andrewsw: That technique only works if the field or property is accessible from outside the class. Make the scope Private and it won't work.

I prefer my classes to be as immutable as possible, if a value change make a new instance.
Was This Post Helpful? 0
  • +
  • -

#14 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6379
  • View blog
  • Posts: 25,774
  • Joined: 12-December 12

Re: Adding different listbox items when Item is selected in the combo box

Posted 23 April 2013 - 03:38 PM

View PostAdamSpeight2008, on 23 April 2013 - 09:44 PM, said:

andrewsw: That technique only works if the field or property is accessible from outside the class. Make the scope Private and it won't work.

I prefer my classes to be as immutable as possible, if a value change make a new instance.

Yes, worth noting, thank you. Although, in this instance, the OP is using a simple Structure to store a few public fields as part of an assignment. Presumably he/she will move on to Classes :).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1