Click here to Skip to main content
12,747,162 members (34,367 online)
Rate this:
Please Sign up or sign in to vote.
I have a ComboBox in a user control(wpf) where I have hooked on to the SelectionChangedEventHandler in so that a Sub is triggered whenever the combo box changes. This all works good and well, however I found something out. Apparently whatever triggers the ComboBox to update it's .Text value does not happen until after the the SlectionChangedEvent I am grabbing (from experience and Google) so when I have that text value report back I get the previous value instead of the newly selected value.

I have a couple leads as to how to solve this but I'm not sure how to do either.

Idea 1:
Perhaps I can take the .SelectedIndex value and derive the text associated to the index, I have no idea how to make this work if it is possible.

Idea 2:
I didn't understand how or why this worked, or even where to implement it. But somebody here is talking about using DependencyPropertyDescriptor as a solution.

Maybe neither of these is the right way to go about it, I don't know, so I appreciate any help .

Thank you,
Posted 18-May-11 6:03am
Updated 18-May-16 21:56pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You should understand that selection is not changed until you hit Enter (or double click), when the user sort of "confirms" the selection. Only this event is fired. There is no such event which would fire when you simply move your selection without confirmation.

If you need to capture this event, you should create it — it does not exist in ComboBox. There was a solution somewhere in the CodeProject. We process all keyboard and mouse events and on each event calculate where the selection is (which is not the same as SelectedIndex as the selection is not yet confirmed. This is pretty complex and consuming code. Are you sure you need it? Based on my explanations, please decide what exactly event you want to handle.

You Idea #1 is correct. This is the robust way to get the element of list. Text is not exactly the same thing. Take into account: the user can start typing in the text box part of the control; at this moment the text may not match any of the list items. As to #2, it simply captures the change of text. If you need to capture index change and get the items text, this is the different event. It all depends on what you want to get (see above).

Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Ultimately here is what I did to solve my problem. I don't know if there are any pitfalls in the way I am doing it but it seems to be working. The combobox is restricted to only predefined pulldown items.
Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
    End Sub

Private Sub ComboBox1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles ComboBox1.SelectionChanged
        Dim indexnum As Integer = ComboBox1.SelectedIndex
        MsgBox(indexnum & " " & ComboBox1.Items(indexnum))
    End Sub
End Class

This is test code but it proves the concept I think.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170215.1 | Last Updated 10 Jun 2016
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100