NullReferenceException on OnLostFocus

4 posts, 0 answers
  1. André
    André avatar
    8 posts
    Member since:
    May 2014

    Posted 26 Sep 2014 Link to this post

    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() ?
  2. André
    André avatar
    8 posts
    Member since:
    May 2014

    Posted 26 Sep 2014 Link to this post

    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); }
            }
        }
    }
  3. DevCraft banner
  4. André
    André avatar
    8 posts
    Member since:
    May 2014

    Posted 26 Sep 2014 in reply to André Link to this post

    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.
  5. Vladislav
    Admin
    Vladislav avatar
    275 posts

    Posted 01 Oct 2014 Link to this post

    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.

     
Back to Top
DevCraft banner