This is a migrated thread and some comments may be shown as answers.

text changed event firing before the selectedvalue changed

6 Answers 1249 Views
ComboBox and ListBox (obsolete as of Q2 2010)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Roy
Top achievements
Rank 1
Roy asked on 16 Feb 2009, 03:37 PM
i have a normal radcombobox (not multicolumn), and i populate it using the textchanged event , through a stored proc that searches on text entered,  the stored proc and datalayer returns an custom object collection and it fills the radcomboo correctly. i cannot use the autocomplete mode as the amount of data that will eventually be in the combobox will be very cumbersome, as well as the search is not a simple text search as it searches on more than one column in the Database.
however now when i select an item in the combobox either by pressing down or by selecting an item with the mouse it then selects that item and puts the text into the radcombo.text field, however it is my understanding that the event that fires this off is the selectedvalue changed? however the text changes and txtchanged is run before selectedvaluechanged  and this is causing problems for me? is this the correct order of events firing and if so how can i get around this ?

here is sample of similar code
   private void rcboInfo_TextChanged(object sender, EventArgs e) 
        { 
            List<InfoComboDetails> allInfos = controller.GetInfoBySearchString(searchstr); 
            foreach (info i in allInfos) 
            { 
 
                RadComboBoxItem rcbi = new RadComboBoxItem(i.Column1 + " - " + i.colum2 , i.ID); 
                rcboInfo.Items.Add(rcbi); 
            } 
              
        } 
 
 private void rcboInfo_SelectedValueChanged(object sender, EventArgs e) 
 { 
rcboinfo2.SelectedValue = controller.GetInfo2ByInfoID((int)rcboInfo.SelectedValue).ID; 
 

6 Answers, 1 is accepted

Sort by
0
Roy
Top achievements
Rank 1
answered on 17 Feb 2009, 11:06 AM
hi i really need some help with this issue , as its quite urgent. i have tried so many different resolutions and have come up empty handed, it appears you cannot populate a radcombobox using the textchanged event and then save the selected value in the selected value changed event concurrently, at the moment it is one or the other which does not serve my purposes.
please help

0
Nick
Telerik team
answered on 17 Feb 2009, 11:37 AM
Hello Roy,

In the standard combobox the TextChanged event is fired before the ValueChanged event and in this regard RadCombobox works correctly. Your code is predicated on a wrong assumption and unfortunately it has to be changed. Populating data in TextChanged event is a hack in general and should be avoided. 

Please extend a bit on your scenario so that we can think of a work-around that actually works for you. As far as I understand, you need to present a subset of all your data at any given moment. Do you think that you can use two comboboxes. When the first subset is selected in the first combobox the second one gets updated? I am looking forward to your reply.

Sincerely yours,
Nick
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Roy
Top achievements
Rank 1
answered on 17 Feb 2009, 12:27 PM

The problem has come about from a requirement to do list item filtering in a RadComboBox; the principle of which is as follows: The RadComboBox is text-editable such that a user may move their carat into the text field portion of the combo box and start to type. As the user types, the list of RadComboBoxItems in its Items collection will update to reflect values appropriate with the user’s input text.

 

This portion works correctly and a user can type into the box and have the Items collection of the combo box update from the RadComboBox’s TextChanged event. In the code that handle’s this event, all values passed into the event as well as the instantiated RadComboBox and its associated Items collection are traversable and correct. It needs to be stated, however, that this TextChanged event fires twice; first with the correct data being send to the event handler and then, immediately thereafter, a second time with a bunce of blank Strings and null values being passed through. We have coded around this double event firing.

 

However, a second event is also required that will cater for the user actually making a selection from the list of RadComboBoxItems that are available at the time and such a selection will be made through mouse action on the drop down list presented to them.

 

We have unsuccessfully attempted to use both the exposed SelectedValueChanged event and the SelectedIndexChanged event handlers for these controls and in both situations the scenario is the same. No matter what is done, the earlier TextChanged event fires (twice, once with garbage) before the SelectedValueChanged event fires. Stepping through the code shows some interesting behaviour in the way in which the event synchronicity is handled; the first TextChanged event fires and executes until a call to a separate method is required, at which point the second event propagates and starts processing. After the second TextChanged event is processed the SelectedValueChanged event is fired (again, twice) and that event will complete execution before the first TextChanges event un-pauses and finally completes.

 

The problem thus is as follows: TextChanged is an event that is used to rebuild the Items collection and SelectedIndexChanged is an event that is used to populate the rest of the form based on the user’s choice. Simply put, the TextChanges event firing before a SelectedIndexChanged event is causing the collection to be rebuilt and thus causing the RadComboBox control to lose its state and the ever important SelectedValue property that we hope to retrieve there.

0
Roy
Top achievements
Rank 1
answered on 17 Feb 2009, 12:38 PM
a basic description of the problem is i have an accout table with a code field and name field  and i need to populate the the combo with both fields and ahve a stored proc that searches on both the code and the name using the search criteria, and whether each of the code or the name starts with the text entered into the text box then refills the combo with the results returned. i cant use the autocomplete mode as that will only search on the code or whichever value is populated first, i have the code + " - " + name populated as the display member and the id populated as the value
0
Nick
Telerik team
answered on 17 Feb 2009, 02:09 PM
Hi Roy,

Thank you for contacting me back.

Can you avoid rebuilding the combobox items in the following way:

 private void radComboBox1_TextChanged(object sender, EventArgs e) 
        { 
            if (this.radComboBox1.Text.ToLower() == "abc"
            { 
                this.radComboBox1.Items.Clear(); 
                RadComboBoxItem rcbi = new RadComboBoxItem(); 
                rcbi.Text = "New Item"
                radComboBox1.Items.Add(rcbi); 
                RadComboBoxItem rcbi2 = new RadComboBoxItem("Second new item"); 
                radComboBox1.Items.Add(rcbi2); 
            } 
            if (this.radComboBox1.Text.ToLower() == "cba"
            { 
                this.radComboBox1.Items.Clear(); 
                RadComboBoxItem rcbi = new RadComboBoxItem(); 
                rcbi.Text = "New Item 2"
                radComboBox1.Items.Add(rcbi); 
                RadComboBoxItem rcbi2 = new RadComboBoxItem("Second new item 2"); 
                radComboBox1.Items.Add(rcbi2); 
            } 
 
        } 
 
        private void radComboBox1_SelectedIndexChanged(object sender, EventArgs e) 
        { 
            if (this.radComboBox1.SelectedIndex == -1) { 
                return
            } 
            Console.WriteLine(this.radComboBox1.SelectedIndex); 
             
        } 
 
        private void radComboBox1_SelectedValueChanged(object sender, EventArgs e) 
        { 
            if (this.radComboBox1.SelectedText == "abc" || 
                this.radComboBox1.SelectedText == "cba"
                return
            Console.WriteLine(this.radComboBox1.SelectedText); 
        } 

I confirm the double firing of events and we are going to address the issue in one of our future releases. I have updated your Telerik points for bringing our attention to this matter.

Please write me back with comments on the source I gave.

All the best,
Nick
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Roy
Top achievements
Rank 1
answered on 23 Feb 2009, 07:50 AM
Thanks for the help, managed to find a workaround whereby i set the selected index to 0 if the return collection count = 1

thanks very much

Tags
ComboBox and ListBox (obsolete as of Q2 2010)
Asked by
Roy
Top achievements
Rank 1
Answers by
Roy
Top achievements
Rank 1
Nick
Telerik team
Share this question
or