locked
Drop down list RRS feed

  • Question

  • I wanted to populate a drop down list  with different names and if a certain name is selected and a button is clicked then it will return a value i have everythiing down except the drop down list how do you specify  the different things in the list box and once they are in there how do you use that liek lets say I have a list of shapes in the drop down box like

    circle

    square

    triangle

    etc...

    and the person selected circle and pressed the submit button it could have something like

    submitbutton_Clicked

    If droplistbox1.value = circle then jdjdklksadlksd

    else if droplistbox1.value = suqare then jkljfkdlsjfsdjlfkds

    else if ..........

    Monday, October 8, 2012 2:37 PM

Answers

  • Yet another way to tackle it

    ' New Default Form with a ComboBox1, Label1, Label2
    ' Replace all default code with this.
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
            Dim myList As New List(Of String)
            myList.AddRange({"Circle", "Square", "Triangle", "etc"})
            Me.ComboBox1.DataSource = myList ' fill CB with data
        End Sub
    
        Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
            Dim cb As ComboBox = DirectCast(sender, ComboBox)
    
            Me.Label1.Text = cb.SelectedItem.ToString ' put item into label1
    
            Dim ind As Integer = cb.SelectedIndex
            Select Case ind
                Case 0 ' first item in list
                    Test("item0 selected") ' call procedure for item 0 
                Case 1
                    Test("item1 selected") ' could be a  different sub
                Case 2
                    Test("item2 selected") ' could be a  different sub
                Case 3
                    Test("item3 selected") ' could be a  different sub
            End Select
        End Sub
    
        Private Sub Test(s As String)
            Me.Label2.Text = s
        End Sub
    End Class
    
    
    
    


    Regards Les, Livingston, Scotland

    • Proposed as answer by Cor Ligthert Monday, October 8, 2012 4:16 PM
    • Marked as answer by drummerpolo15 Monday, October 8, 2012 5:10 PM
    Monday, October 8, 2012 3:55 PM

All replies

  • you could use a class + a dictionary:

    Public Class Form1
    
        Dim d As New Dictionary(Of String, gibberish)
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            d.Add("circle", New gibberish With {.defValue = "jdjdklksadlksd"})
            d.Add("square", New gibberish With {.defValue = "jkljfkdlsjfsdjlfkds"})
            ListBox1.Items.AddRange(d.Keys.ToArray)
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If ListBox1.SelectedIndex > -1 Then
                MsgBox(d(ListBox1.Text).defValue)
            End If
        End Sub
    
    End Class
    

    Public Class gibberish
    
        Private _defValue As String
        Property defValue() As String
            Get
                Return _defValue
            End Get
            Set(ByVal value As String)
                _defValue = value
            End Set
        End Property
    
    End Class
    


    thanks for any help

    • Proposed as answer by Cor Ligthert Monday, October 8, 2012 4:16 PM
    Monday, October 8, 2012 3:47 PM
  • Yet another way to tackle it

    ' New Default Form with a ComboBox1, Label1, Label2
    ' Replace all default code with this.
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
            Dim myList As New List(Of String)
            myList.AddRange({"Circle", "Square", "Triangle", "etc"})
            Me.ComboBox1.DataSource = myList ' fill CB with data
        End Sub
    
        Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
            Dim cb As ComboBox = DirectCast(sender, ComboBox)
    
            Me.Label1.Text = cb.SelectedItem.ToString ' put item into label1
    
            Dim ind As Integer = cb.SelectedIndex
            Select Case ind
                Case 0 ' first item in list
                    Test("item0 selected") ' call procedure for item 0 
                Case 1
                    Test("item1 selected") ' could be a  different sub
                Case 2
                    Test("item2 selected") ' could be a  different sub
                Case 3
                    Test("item3 selected") ' could be a  different sub
            End Select
        End Sub
    
        Private Sub Test(s As String)
            Me.Label2.Text = s
        End Sub
    End Class
    
    
    
    


    Regards Les, Livingston, Scotland

    • Proposed as answer by Cor Ligthert Monday, October 8, 2012 4:16 PM
    • Marked as answer by drummerpolo15 Monday, October 8, 2012 5:10 PM
    Monday, October 8, 2012 3:55 PM
  • @Paul,

    Just a simple attention. 

    In the newer versions you can use automatic properties for those you use

    Public Class Gibberish 'As convention a Class starts with an uppercase
        Public Property DefValue as string
    End Class

    There is not any difference than regarding to your code. 

    If it is a readonly property, you have to do it in the way you do without the Set.


    Success
    Cor

    Monday, October 8, 2012 4:19 PM
  • @Cor

    I have the pro VS versions of 2005/08/10/12, but i use 2008 most of the time.

    I was aware vb2010 + 12 have 1 line properties


    thanks for any help

    Monday, October 8, 2012 4:45 PM