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

NullReferenceException on OnLostFocus

3 Answers 101 Views
TextBox
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
André
Top achievements
Rank 1
André asked on 26 Sep 2014, 11:24 AM
Using Version 2014.2.617.3040

I've created a RadTextBox in XAML inside a Collapsed Grid and set the Focus programmatically using a Behavior (See FocusBehavior attached). The Grid won't be set to Visible.
Now I'm opening a RadMessageBox using "await RadMessageBox.ShowAsync()". That results in

{System.NullReferenceException: Object reference not set to an instance of an object.
   at Telerik.Windows.Controls.RadTextBox.OnLostFocus(RoutedEventArgs e)
   at System.Windows.Controls.Control.OnLostFocus(Control ctrl, EventArgs e)
   at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)}

How should I fix this? Removing the Focus programmatically before calling RadMessageBox.ShowAsync() ?

3 Answers, 1 is accepted

Sort by
0
André
Top achievements
Rank 1
answered on 26 Sep 2014, 11:26 AM
ups, forgot attachment ...

using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
 
namespace GKApp.Helper
{
    public class FocusBehavior : SafeBehavior<TextBox>
    {
        private bool _cursorChangeRunOnce = true;
 
        /// <summary>
        /// Called when [attached].
        /// </summary>
        protected override void OnAttached()
        {
            AssociatedObject.GotFocus += OnGotFocus;
            AssociatedObject.LostFocus += OnLostFocus;
            AssociatedObject.TextChanged += OnTextChanged;
            AssociatedObject.Loaded += OnLoaded;
            base.OnAttached();
        }
 
        /// <summary>
        /// Called when [detaching].
        /// </summary>
        protected override void OnDetaching()
        {
            base.OnDetaching();
            AssociatedObject.GotFocus -= OnGotFocus;
            AssociatedObject.LostFocus -= OnLostFocus;
            AssociatedObject.TextChanged -= OnTextChanged;
            AssociatedObject.Loaded -= OnLoaded;
        }
 
        /// <summary>
        /// Called when [loaded].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            Focus();
        }
 
        /// <summary>
        /// Called when [text changed].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="TextChangedEventArgs"/> instance containing the event data.</param>
        private void OnTextChanged(object sender, TextChangedEventArgs e)
        {
            if (_cursorChangeRunOnce && AssociatedObject.SelectionStart <= 0)
            {
                AssociatedObject.Select(AssociatedObject.Text.Length, 0);
            }
            _cursorChangeRunOnce = false;
        }
 
        /// <summary>
        /// Called when [lost focus].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void OnLostFocus(object sender, RoutedEventArgs e)
        {
            IsFocused = false;
            _cursorChangeRunOnce = true;
        }
 
        /// <summary>
        /// Called when [got focus].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void OnGotFocus(object sender, RoutedEventArgs e)
        {
            IsFocused = true;
        }
 
        /// <summary>
        /// Focuses this instance.
        /// </summary>
        public void Focus()
        {
            if (IsFocused) return;
 
            // Grab the dispatcher from the current executing thread
            var d = Dispatcher;
 
            // Tasks execute in a thread pool thread
            new Task(() =>
            {
                Thread.Sleep(100); // Delay
 
                // Use the dispatcher to asynchronously invoke
                // the action back on the original thread
                d.BeginInvoke(DoFocus);
 
            }).Start();
        }
 
        /// <summary>
        /// Does the focus.
        /// </summary>
        private void DoFocus()
        {
            AssociatedObject.Focus();
        }
 
        private static readonly DependencyProperty IsFocusedProperty =
            DependencyProperty.Register(
                "IsFocused",
                typeof(bool),
                typeof(FocusBehavior),
                new PropertyMetadata(false));
 
        /// <summary>
        /// Gets or sets a value indicating whether this instance is focused.
        /// </summary>
        /// <value>
        /// <c>true</c> if this instance is focused; otherwise, <c>false</c>.
        /// </value>
        private bool IsFocused
        {
            get { return (bool)GetValue(IsFocusedProperty); }
            set { SetValue(IsFocusedProperty, value); }
        }
    }
}
0
André
Top achievements
Rank 1
answered on 26 Sep 2014, 11:50 AM
I've done a simple Demo: https://just.4be.mobi/public.php?service=files&t=5654a0c22cf3f6eaf219cacfa4d307a1
Press one of the two message box buttons and it will crash.
0
Vladislav
Telerik team
answered on 01 Oct 2014, 08:45 AM
Hello André,

Thank you for this report.
This is actually a bug in the RadTextBox control. We have logged it and do our best to provide a fix for it.

Meanwhile, there is a possible workaround for it.
Inherit the RadTextBox control, override its "OnLostFocus" event and surround in "try-catch block" the invocation of the base.OnLostFocus(RoutedEventArgs e).
Please let us know if this is applicable/working in your case.

Your Telerik points are updated accordingly.

Regards,
Vladislav
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
TextBox
Asked by
André
Top achievements
Rank 1
Answers by
André
Top achievements
Rank 1
Vladislav
Telerik team
Share this question
or