Most of you have already used BusyIndicator control in your Silverlight or WPF application while communicating with server or processing a huge data. Sometime developers want to focus to a particular control like TextBox once the busyIndicator stops.
I noticed in various forums that developers really suffer a lot focusing to a TextBox at the end of the processing and when the busy indication stops. Let’s read the trick to resolve this.
Let us first understand the problem statement, the solution is simple enough once you go thru this. Let us have a window with a search TextBox inside a Telerik RadBusyIndicator. When the user enter search key in the TextBox and hit search, it starts the BusyIndicator and gives a round trip to the server to search in the database. Once the service returns the data, you stop the BusyIndicator and want to place your cursor focused into the search TextBox.
At a first glance, it is very easy. Isn’t it? Just call the Focus() method of the TextBox once you set busyIndicator.IsBusy=”False”, but that will not work. The TextBox control will not be focused once the busy indication stops. The reason behind this is very reasonable. Once you set IsBusy=”False”, it starts an animation to close the busy indicator. Within that time, it executes the next line and focused the TextBox control, but then again changes it’s focus point to BusyIndicator to close it completely. Thus, you will not see the TextBox focused in the UI.
You might already know that, TextBox inherits from System.Windows.Controls.Control class. Control class has an event called IsEnabledChanged, which is the only option here to rescue you. When BusyIndicator starts, all controls inside it change their state to disabled automatically and become enabled once the busy indication stops completely. Take advantage of this event to focus your TextBox control.
If you know the TextBox (or any other control) that you want to focus once the busy indicator completes, just register the IsEnabledChanged event and focus that on processed. That’s all. Here is the code snippet for your reference:
To demonstrate the problem and it’s solution, let’s create one Silverlight project and add a reference to the assembly named Telerik.Windows.Control.dll in your Silverlight client project. In the MainPage.xaml, modify the code or replace the existing one with this:
And the related C# code is here for simulate the issue and the solution:
Here on click of the button, it actually simulates a service call or a huge data processing after starting the BusyIndicator service. After 2 seconds delay, it stops the busy indication. If you go with the same code, or enter firstTextBox.Focus() just after IsBusy=false, you will notice that the TextBox will not get focus once it stops.
To focus it, uncomment the line in the constructor which will focus the box back once the busy indication stops completely and the TextBox changes it’s status to enable.
I hope the post will help you if you faced similar problem in your Silverlight or WPF project with the BusyIndicator. Don’t forget to share your thoughts about this workaround.