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

lost Focus

1 Answer 528 Views
Spreadsheet
This is a migrated thread and some comments may be shown as answers.
Andreas
Top achievements
Rank 1
Andreas asked on 05 Sep 2020, 08:09 AM

why does the spreadsheet lose focus when you make an input? 

How can I guarantee that I only trigger a LostFocus event when I leave the control?

1 Answer, 1 is accepted

Sort by
0
Peter
Telerik team
answered on 09 Sep 2020, 03:42 PM

Hello,

The spreadsheet lost the focus because the hosted Cell's RichTextBox Editor got the focus in order to allow users input.

I can suggest handling LostFocus on the Cell's Editor as well and check in this event where the focus is. I have created a RadSpreadSheet inheritor which demonstrates this idea:

    public class MyRadSpreedsheet : RadSpreadsheet
    {
        protected override void OnLostFocus(EventArgs e)
        {
            Control ctrl = GetFocusedControl();

            if (ctrl!=null && ctrl.Parent == this)
            {
                foreach (Control control in this.Controls)
                {
                    control.LostFocus -= Control_LostFocus;
                    control.LostFocus += Control_LostFocus;                 
                }
            }
            else
            {
                base.OnLostFocus(e);
            }
        }

        private void Control_LostFocus(object sender, EventArgs e)
        {
            Control ctrl = GetFocusedControl();
            if (!ContainsSpread(ctrl))
            {
                this.OnLostFocus(e);
            }
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Winapi)]
        internal static extern IntPtr GetFocus();

        private Control GetFocusedControl()
        {
            Control focusedControl = null;           
            IntPtr focusedHandle = GetFocus();
            if (focusedHandle != IntPtr.Zero)
                focusedControl = Control.FromHandle(focusedHandle);
            return focusedControl;           
        }

        private bool ContainsSpread(Control control)
        {
            if(control == null)
            {
                return false;
            }
            if(control == this)
            {
                return true;
            }
            foreach(Control ctr in control.Controls)
            {
                if(ContainsSpread(ctr))
                {
                    return true;
                }
            }

            return false;
        }
    }

We will discuss this case in the team and maybe will consider to include a better way to handle it.

Regards,
Peter
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
Spreadsheet
Asked by
Andreas
Top achievements
Rank 1
Answers by
Peter
Telerik team
Share this question
or