none
Process Question - ComboBox events RRS feed

  • Question

  • I want to run a query on the dropdown selected value of a combobox, which event should this be called in, I have tried SelectionchangeCommitted, DropDown, SelectedIndexChanged and SelectedValueChanged but all they do is give me the first result in the box and then change to the selected value, but it always seems to use the top item in the text box.
    Thursday, September 26, 2019 8:30 PM

Answers

  • I found my issue, I was referencing the code in both the DropDown and SelectedIndexChanged, sorry, didn't realize it until I switched over to the designer view as it was coded further up in the code.
    • Marked as answer by Ryan Lashway Friday, September 27, 2019 5:15 PM
    Friday, September 27, 2019 5:15 PM
  • This works for me. I don't use a Handles clause as this would trigger the event upon loading the DataSource of the ComboBox, otherwise when a item is changed the correct one shows in the Console.WriteLine

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
            ComboBox1.DataSource = New List(Of String) From {"Karen", "Mary", "Mike", "Jim"}
            AddHandler ComboBox1.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged
        End Sub
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
            Console.WriteLine(ComboBox1.Text)
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Ryan Lashway Friday, September 27, 2019 5:17 PM
    Thursday, September 26, 2019 10:13 PM
    Moderator

All replies

  • Hi

    Show your code - we have all lost our crystal balls!



    Regards Les, Livingston, Scotland

    Thursday, September 26, 2019 10:11 PM
  • This works for me. I don't use a Handles clause as this would trigger the event upon loading the DataSource of the ComboBox, otherwise when a item is changed the correct one shows in the Console.WriteLine

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
            ComboBox1.DataSource = New List(Of String) From {"Karen", "Mary", "Mike", "Jim"}
            AddHandler ComboBox1.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged
        End Sub
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
            Console.WriteLine(ComboBox1.Text)
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Ryan Lashway Friday, September 27, 2019 5:17 PM
    Thursday, September 26, 2019 10:13 PM
    Moderator
  • Hi

    Here is one way: put a ComboBox and a multiline textBox on a Form1 and try this code.

    ' Form1 with ComboBox1 and
    ' multiline TextBox1
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		With ComboBox1
    			For item As Integer = 0 To 19
    				.Items.Add("Item " & item.ToString)
    			Next
    		End With
    	End Sub
    	Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    		Dim cb As ComboBox = CType(sender, ComboBox)
    		With TextBox1
    			.AppendText("Selected Index = " & cb.SelectedIndex.ToString & vbCrLf)
    			.AppendText("Selected Item = " & cb.SelectedItem.ToString & vbCrLf)
    			.AppendText(StrDup(15, "=") & vbCrLf)
    		End With
    	End Sub
    End Class


    Regards Les, Livingston, Scotland

    Thursday, September 26, 2019 10:25 PM
  • Hi,

    Have you solved this problem now?

    I think the above reply can provide you with a solution, have you tried it?

    If so, hope you can close this thread by marking the reply as answer as this will help others looking for the same or similar issues down the road.

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, September 27, 2019 9:23 AM
    Moderator
  • I found my issue, I was referencing the code in both the DropDown and SelectedIndexChanged, sorry, didn't realize it until I switched over to the designer view as it was coded further up in the code.
    • Marked as answer by Ryan Lashway Friday, September 27, 2019 5:15 PM
    Friday, September 27, 2019 5:15 PM
  • As both answers provided were the same, I am marking Karen's as the answer as it is the same as Leshay's as it references the usage of SelectedIndexChanged as being the answer
    Friday, September 27, 2019 5:17 PM