none
SelectionChanged event of WPF ComboBox returns Prior Selection not Current RRS feed

  • Question

  • There has to be something silly I'm doing here!

     

    When I process a ComboBox's SelectionChanged event and then look at the ctype(e.source,ComboBox).Text value, I really expect to get the value that the ComboBox is now supposed to be set to. Instead, I'm getting the prior value! Help! I need to figure out the new value in the ComboBox Text property after it's been changed!

     

    M

    Tuesday, February 26, 2008 1:34 PM

Answers

  • Here's another reasonable solution (I kick myself that I didn't think of this before). You need to go after the SelectedItem. In my case the ComboBox is bound to a DataTable so the SelectedItem is a DataRowView. In my example, I know that the first column is the DisplayMember:

     

     

    Code Snippet

    Private Sub cbox_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles cbox.SelectionChanged

     

            Dim drv As DataRowView = CType(CType(e.Source, ComboBox).SelectedItem, DataRowView)
            Select Case drv(0).ToString
                Case "a"

                Case "b"

            End Select

     

    End Sub

     

     

     

     

     

    If you rely on ctype(e.Source,ComboBox).Text, you get the value of  the TextBox portion of the ComboBox before it changes to the value that is the DisplayMember in the SelectedItem.

     

    M


    Tuesday, February 26, 2008 6:10 PM
  • The SelectionChangedEventArgs exposes two properties to deal with this,  AddedItems and RemovedItems. The former will give you the newly selected item, the latter will give you the previously selected item. Since only one item can be selected in a ComboBox, the index for either will always be zero (Note that this would not be the case with multi-select enabled controls).

     

    Hope that helps,

     

    Keith

     

    Tuesday, March 4, 2008 5:52 AM

All replies

  • Whoa! All that to get the value?

     

    1) Could you do it in VB? The Telerik Code Converter coughed a bit.

     

    2) Why would they do this? Just left it out?

     

    Tuesday, February 26, 2008 5:08 PM
  • So I did this: (I copied to your site too):

     

    Code Snippet

    Dim dpd As System.ComponentModel.DependencyPropertyDescriptor
    dpd = DependencyPropertyDescriptor.FromProperty(ComboBox.TextProperty, GetType(ComboBox))
    dpd.AddValueChanged(cb1, AddressOf OnTextChanged)

     

    Private Sub OnTextChanged(ByVal sender As Object, ByVal args As EventArgs)
    'Add your code here

    End Sub

     

     

    So, WHY would they do this? It seems that getting the value of the ComboBox.Text when the SelectionChanged  event is fired is a pretty basic!

     

    What other controls will I have problems getting the value of the text when the SelectionChanged event fires?

     

    M

     

    Tuesday, February 26, 2008 5:53 PM
  • Here's another reasonable solution (I kick myself that I didn't think of this before). You need to go after the SelectedItem. In my case the ComboBox is bound to a DataTable so the SelectedItem is a DataRowView. In my example, I know that the first column is the DisplayMember:

     

     

    Code Snippet

    Private Sub cbox_SelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles cbox.SelectionChanged

     

            Dim drv As DataRowView = CType(CType(e.Source, ComboBox).SelectedItem, DataRowView)
            Select Case drv(0).ToString
                Case "a"

                Case "b"

            End Select

     

    End Sub

     

     

     

     

     

    If you rely on ctype(e.Source,ComboBox).Text, you get the value of  the TextBox portion of the ComboBox before it changes to the value that is the DisplayMember in the SelectedItem.

     

    M


    Tuesday, February 26, 2008 6:10 PM
  • The combo box itself is listening to that event in order to update the text within. I'm also surprised it doesn't expose a TextChanged event but you've found the correct way of adding one yourself. There aren't any other controls like the ComboBox that you could encounter this with.

     

    Tuesday, February 26, 2008 9:54 PM
  • The SelectionChangedEventArgs exposes two properties to deal with this,  AddedItems and RemovedItems. The former will give you the newly selected item, the latter will give you the previously selected item. Since only one item can be selected in a ComboBox, the index for either will always be zero (Note that this would not be the case with multi-select enabled controls).

     

    Hope that helps,

     

    Keith

     

    Tuesday, March 4, 2008 5:52 AM
  • I do like this solution too. The following: ctype(e.AddedItems.Item(0),DataRowView)(0) works just fine to return the newly selected item in the ComboBox, as long as it's a simple, single selection.

     

    I need to explore a  bit more with RemovedItems and see how I can make use of this feature.

     

    Thanks.

     

    Wednesday, March 5, 2008 2:43 PM
  • Hi.

    An easy solution is described here.

    Hope this helps,

    David


    Web Developer
    • Proposed as answer by dw4lsh Friday, February 27, 2009 6:58 PM
    Friday, February 27, 2009 6:58 PM
  • Hi,

    It's so easy:

     

    Private Sub Combo1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles Combo1.SelectionChanged

    Dim oITM As ComboBoxItem = sender.SelectedItem
    If IsNothing(oITM) Then
        'do something...
    Else
        'use the content property to get the selecteditem text
        Dim SelectedItemText As String = oITM.Content 
    End If

    End
    Sub

    Cheers,

    Eric

     ------------------------
    Please check my small Ms SQL Server TSQL search tool SQL Digger www.sqldigger.com

    Friday, October 30, 2009 9:05 AM
  • I used DropDownClosed event - gives the selected item value.

    DG

    DG
    Thursday, January 21, 2010 4:05 PM
  • YOu should simply put your code in the mouseleave event

    private void comboBox1_MouseLeave(object sender, MouseEventArgs e)
            {

    }

    Tuesday, December 31, 2013 10:02 PM
  • Try this

    private void cboChitValue_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
     string tmp;
     if (e.AddedItems.Count > 0)
     {
      tmp = e.AddedItems[0].ToString();
     }
    }

    enjoy
    Sunday, January 26, 2014 11:54 AM