The Best C# Programmer In The World - Ben Perkins Member Login  | Newsletter | RSS Feeds


 
 
 
TheBestCSharpProgrammerInTheWorld.com 
 
The Best C# Programmer In The World - Ben Perkins
TextChanged vs. SelectionChange events in WPF  
 
TextChanged vs. SelectionChange events in WPF
 
Please rate:
 
I made the mistake of using the SelectionChange event on a textbox while implementing some filtering on a DataGrid. What happened, when using SelectionChanged, was that each time the textbox received focus I was executing the filter. Even initially when there was nothing to filter. My logic was checking if the contents of the textbox contained a value or not. However, because the datagrid needed to be reset when the user removed all the filter criteria, I still needed to execute the filter when there was nothing in the filter textbox.
 
 
 
                        if (textBox1.Text != "")
                        {
		                  //Filter using the entered criteria
                        }
                        else
                        {
                          //Reset the filter to null
                          //Repopulate the datagrid with unfiltered data
                        }
                       
                         
The else logic was being executed each time the textbox received focus.
 
The simple solution was to change the event I used to execute the filter from SelectionChange to TextChanged.
 
The TextChanged event only fires when the value in the filter textbox is modified. It does not fire when the control receives focus.
 
It’s important to give some time and thought to performance and usability from a user perspective. The ListCollectionView.Filter() method is very fast and there was only a 2 second delay initially with about +6000 rows and 30 columns.
 
However, I recognized the fact that the logic was doing something that it didn’t need to do. Therefore, I searched and implemented a solution that reduced the resource requirement and increased the performance of the system.
 
I suggest you do the same with your implementations and programs.
 
 
Feedback / Question
 
Your Name:Your Email:
 
Subject:
 
Feedback/Question:
 
 
 
I had to remove the capability to leave feedback due to this. Will be back soon.
 
 
page.Translate()
 
 
blog.Stats()
 
  Posts: 103
  Comments: 86
  Fundamentals: 13
 
my.Book()

 
me.About()
 
 
 
 
 
blog.Archive()
 
2012 February  (4)
2012 January  (5)
2011 December (2)
2011 November (6)
2011 October (7)
2011 September (7)
2011 August (9)
2011 July (9)
2011 June (8)
2011 May (9)
2011 April (7)
2011 March (9)
2011 February (8)
2011 January (8)
2010 December (7)
2010 November (8)
2010 October (4)
 
site.Visits()
 
free counters
 
tag.Cloud()
 
code.Disclaimer()
 
The sample code on this website is provided to illustrate a concept and should not be used in applications or Web sites without proper professional consultation, as it may not illustrate the safest coding practices. I assume no liability for incidental or consequential damages should the sample code be used for purposes other than as intended.
 
   


The Best C# Programmer In The World - Ben Perkins, © 2010, All Rights ReservedContact Ben