Switch focus back to editor from editor view part

This issue is sort of a follow-up to my question about updating the target segment of a TU from a custom editor view part (https://community.sdl.com/developers/language-developers/f/57/t/5133)

I have a view part that contains a DataGridView control.  When the user navigates to a segment in the translation editor, the data grid gets populated with some data corresponding to the current segment.  When that happens, the focus gets automatically moved to my view part, and specifically to the data grid, which is in fact the desired behavior.

However the problem is that after using the data grid to manipulate the related data and using an event in the data grid to update the target segment using UpdateSegmentPair, the focus stays on the data grid in my view part, but I would like to move the focus back to the translation editor.  Unfortunately I can't see a direct way to do that.  Right now I'm using SendKeys("{TAB}") several times, but it is very buggy and only works some of the time.

Is there a way to directly place the focus back on the translation editor from code?

Parents
  • Update:  as it turns out, my workaround mentioned in a previous post is inconsistent in various use cases and so is unsuitable for a release version.  What I am doing now is using System.Reflection and Sdl.DesktopEditor.EditorApi.IContentSelection to get a reference to the control and then calling Focus() on it, which seems to work.  Credit to Jesse Good for giving me the idea, related to a different problem here: 

    https://community.sdl.com/developers/language-developers/f/57/t/5881

    Here is the code in case anyone is interested:

    var y = doc.Selection.Target;
    PropertyInfo pi = y.GetType().GetProperty("ContentSelection", BindingFlags.NonPublic | BindingFlags.Instance);
    var x = pi.GetValue(y);
    IContentSelection cs = (IContentSelection)x;
    Control cc = (Control)cs.Control;
    cc.Focus();

     

Reply
  • Update:  as it turns out, my workaround mentioned in a previous post is inconsistent in various use cases and so is unsuitable for a release version.  What I am doing now is using System.Reflection and Sdl.DesktopEditor.EditorApi.IContentSelection to get a reference to the control and then calling Focus() on it, which seems to work.  Credit to Jesse Good for giving me the idea, related to a different problem here: 

    https://community.sdl.com/developers/language-developers/f/57/t/5881

    Here is the code in case anyone is interested:

    var y = doc.Selection.Target;
    PropertyInfo pi = y.GetType().GetProperty("ContentSelection", BindingFlags.NonPublic | BindingFlags.Instance);
    var x = pi.GetValue(y);
    IContentSelection cs = (IContentSelection)x;
    Control cc = (Control)cs.Control;
    cc.Focus();

     

Children
  • Thanks Patrick, brilliant, that seems to work for me as well (although being miles above my non-programmer head).

    I do not only want to move focus back but need to go to a specific segment so my work current workaround was SendKeys.Send("{F6}") after SetActiveSegmentPair, but for obvious reasons that is far from ideal (default setting means that F6 is toggling source/target, but I guess the user could change F6 to mean virtually anything).

    Question: Does someone know if it is possible to check the current Studio keyboard shortcuts somehow?

    Romulus, as you may already have discovered SetActiveSegmentPair will only make the segment in question active - it will not move focus/cursor there.
  • Former Member
    0 Former Member in reply to Patrick Porter
    It looks good.
    Can I use the same theory at my situation ?
    Mine is located at FilesView not at EditorView.
    This view losts focus so easily.
    -I am not good at reflection.
    Thanks