Prevent parent RichTextEditor form from stealing focus from new form

2 Answers 30 Views
RichTextEditor
Mihajlo
Top achievements
Rank 1
Iron
Mihajlo asked on 26 Jul 2024, 04:18 PM

Create new Word-inspired project and add CommandExecuting event handler with the following code:


private void radRichTextEditor1_CommandExecuting(object sender, Telerik.WinForms.Documents.RichTextBoxCommands.CommandExecutingEventArgs e)
{
	if (e.Command is Telerik.WinForms.Documents.RichTextBoxCommands.SaveCommand)
	{
		e.Cancel = true;

		var newForm = new MainForm();
		newForm.Show();
	}
}

When I click on Save quick-button I expect to see new form active and in focus, but what actually happens is that new form is created, gets in focus briefly, and then the old form steals the focus. How can I fix the focus steal? I need new form to be non-modal.

In theory I should be able to do it if I call Show(this) instead of Show(), and in OnShown overload set Owner = null, but even though the active window now is the new form, the keyboard focus is still on old form (type something to see). I suspect there is some OnTimer interference. Do you have an adequate solution?

 

2 Answers, 1 is accepted

Sort by
0
Accepted
Mihajlo
Top achievements
Rank 1
Iron
answered on 30 Jul 2024, 03:03 PM
I've found the solution. The command (in this example SaveCommand) must override ShouldFocusCaretAfterExecute, and return false there. Can't do that with SaveCommand, but SaveCommand was given here just as an example. I have my own command that can override the property mentioned above. And then the focus is not stolen anymore.
Nadya | Tech Support Engineer
Telerik team
commented on 31 Jul 2024, 02:31 PM

Hello, Mihajlo,

I am really glad to hear that you managed to find a solution that suits to your needs. Thank you for sharing it with the community.

In case you have any other questions, do not hesitate to ask.

0
Nadya | Tech Support Engineer
Telerik team
answered on 30 Jul 2024, 01:32 PM

Hello, Mihajlo,

Thank you for writing.

In order to show your newly created form to appear on top of the main form, you can set the TopMost property. It ensures that the form will be displayed as a topmost form. Use the following code snippet in CommandExecuting event handler:

var newForm = new MainForm();
newForm.TopMost = true;
newForm.Show();

I hope this helps. If you have any other questions do not hesitate to contact me. 

Regards,
Nadya | Tech Support Engineer
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Mihajlo
Top achievements
Rank 1
Iron
commented on 30 Jul 2024, 02:16 PM

The proposed solution does not solve the problem in three aspects.

First, while the new form is on top of the old form the keyboard focus is still on the old form. Click save, wait for new form to show, and type something. The text is not typed in new form, but in old form. The old form "stole" the keyboard focus from the new form.

Second, TopMost=true makes the new form on top of all other windows from all other apps. It stays on top even if other window is currently active. Nobody expects that. TopMost should not be used for 99% of windows, and the 1% where it can be used is for short-lived popup kind of windows.

Third, if you now click Save from the new form, you will get a third form that exhibits the same problem. The second form will steal focus from the third form. They are both TopMost, and so are equal in this regard. The problem is not solved.

Tags
RichTextEditor
Asked by
Mihajlo
Top achievements
Rank 1
Iron
Answers by
Mihajlo
Top achievements
Rank 1
Iron
Nadya | Tech Support Engineer
Telerik team
Share this question
or