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

Set focus to a TextBox

6 Answers 136 Views
BusyIndicator
This is a migrated thread and some comments may be shown as answers.
Missing User
Missing User asked on 05 Jan 2012, 12:46 PM
Hello.

I have a Ui with a RadBusyIndicator and a few TextBox's. The UI works in this way, the user input a ZIP Code in a specific textbox, when the user clicks in the "search" button, i call a WCF service that returns the Address of the ZIP Code. Untill the service call doesn't returns I set the IsBusy of the RadBusyIndicator to True so the user know that something is loading.

My problem is, after the service returns with the data, I need to validate each everyone of the TextBoxes and if anyone is with no value I need to setfocus on it, but with the RadBusyIndicator it doesn't work.

I searched in the documentation and the forum and i found some people with the same issue but i think the XAML approach discribed here:
http://www.telerik.com/help/silverlight/radbusyindicator-how-to-restore-the-focus.html

Doesn't fit in my case because i don't know what textbox need to receive the focus.
This is the Method that is fired after the service call returns, and here i need to set the focus.

private void PreencherEnderecoCompleto()
        {
            if (this.ViewModel.CEPEncontrado)
            {
                this.ViewModel.CombosVisibility = Visibility.Collapsed;
                this.ViewModel.TextBoxVisibility = Visibility.Visible;
 
                if (this.txtBairro.Text == "")
                {
                    this.txtBairro.Focus();
                }
 
                else if (this.txtEndereco.Text == "")
                {
                    this.txtEndereco.Focus();
                }
                else if (this.txtNumero.Text == "")
                {
                    this.txtNumero.Focus();
                }
            }
              }

Is there another workaround for this?

Thanks!

6 Answers, 1 is accepted

Sort by
0
Konstantina
Telerik team
answered on 10 Jan 2012, 11:08 AM
Hello Joao,

In order to achieve this you need to track when the RadBusyIndicator is enabled and that can be achieved through the IsBusyIndicationVisible boolean property. Since, you are performing an validation, I can suggest you to use the Validation.GetErrors() method and pass your control to it. Then, create the attached behaviour as explained in the help article and in OnEnsureFocusChanged in the IF statement check whether there are errors or not using the GetErrors() method. More information about it you can find in this blog post: http://www.michaelsnow.com/2010/05/28/silverlight-tip-of-the-day-25-detecting-validation-errors-on-submit/

Hope this helps.

Regards,
Konstantina
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
0
danparker276
Top achievements
Rank 2
answered on 29 Jan 2013, 03:16 AM
After spending all day trying to figure out why my OOB windows was losing it's focus, I narrowed it down to the busy indicator and saw this. The busy indicator is on my OOB and it sets the focus to the main page navigation content behind it.  And the link above doesn't work now.

And why doesn't this work?
  this.busyIndicator.IsBusy = false;
            while (this.busyIndicator.IsBusyIndicationVisible == true)
            {
                for (int jj = 0; jj < 30000; jj++)
                {
                    int a = jj + 1;
                    string j = a.ToString();
                }
            }

In LOB setting the focus is very important.  I don't want to add that OnEnsureFocusChanged on every control.  Is there going to be any fix for this?  What's the use of using the busy signal if I have to jump through all these hoops after it finishes?
0
danparker276
Top achievements
Rank 2
answered on 30 Jan 2013, 12:46 AM
Maybe what I should do is instead of putting the busy around the layoutroot, I should just put it inside the grid.

before:
 <telerik:RadBusyIndicator ...
<Grid x:Name="LayoutRoot"  ...

after:
<Grid x:Name="LayoutRoot"  ...
<telerik:RadBusyIndicator Name="bi" Grid.Row=0 Grid.Column=0 (set the spans)...

Then in the c#
bi.IsBusy=true;
this.IsEnabled = false;
.........
bi.IsBusy=false;
this.IsEnabled=true;

Would this work just as good and not have to deal with the focus problems?
I'm currently just going to just is this.IsEnabled and not use the busy control.  It's not worth any potential problems for me or adding additional classes.



0
Konstantina
Telerik team
answered on 31 Jan 2013, 08:54 AM
Hello Dan,

We are aware of the issue and it is logged in our bug tracking system. However, the task for Q1 2013 are already planned. We will do our best to include the fix of this issue in the plan for the Q2 2013 version. You can track its status and vote for it in PITS. The item will update once we start working on it.
Sorry for the caused inconvenience.

Greetings,
Konstantina
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Scott Waye
Top achievements
Rank 2
Veteran
Iron
answered on 01 Jun 2013, 10:47 AM
Hi,

Want to share some code for restoring the focus to whatever had the focus before the radbusyindicator was shown.  I've done this as an attached property and used some of the other examples as guides.  Fits well with MVVM.  Not exhaustively tested:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Interactivity;
using Telerik.Windows.Controls;
 
namespace FocusBusy.Web
{
    public class RestoreBusyFocus : Behavior<RadBusyIndicator>
    {
        Control focusedElement;
        protected override void OnAttached()
        {
            base.OnAttached();
            BindingOperations.SetBinding(AssociatedObject,
                             RadBusyIndicator.IsBusyProperty,
                             new Binding
                             {
                                 Path = new PropertyPath("IsBusy"),
                                 Source = this,
                                 Mode = BindingMode.TwoWay
                             });
        }
 
        bool isBusy;
        public bool IsBusy
        {
            get { return isBusy; }
            set
            {
                isBusy = value;
                if (isBusy)
                {
                    focusedElement = FocusManagerHelper.GetFocusedElement(AssociatedObject) as Control;
                }
                else
                {
                    if (focusedElement != null)
                    {
                        focusedElement.IsEnabledChanged += FocusedElementIsEnabledChanged;
                    }
                }
            }
        }
 
        private void FocusedElementIsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            focusedElement.Focus();
            focusedElement.IsEnabledChanged -= FocusedElementIsEnabledChanged;
        }
    }
}

Use it like this:

<telerik:RadBusyIndicator x:Name="BusyIndicator">
    <i:Interaction.Behaviors>
        <Web:RestoreBusyFocus/>
    </i:Interaction.Behaviors>
    <StackPanel>
0
Kunal Chowdhury
Top achievements
Rank 2
answered on 16 Aug 2013, 06:34 PM
Hi,

I blogged about the simplest and quickest solution for it here: http://www.kunal-chowdhury.com/2013/08/focus-textbox-once-telerik-busyindicator-stops.html Hope, that helps.

Regards,
Kunal Chowdhury
Tags
BusyIndicator
Asked by
Missing User
Answers by
Konstantina
Telerik team
danparker276
Top achievements
Rank 2
Scott Waye
Top achievements
Rank 2
Veteran
Iron
Kunal Chowdhury
Top achievements
Rank 2
Share this question
or