none
Form Focus

    Question

  • Hi

    Im having a little trouble getting focus back to my main form after it is made visible again. My form is called from its Parent application "Autodesk Inventor" and only works and can only be called within Inventor (Its an addin). My current process is:

    • Inside Inventor, run my Addin
    • Addin form loads
    • Inside the Addin, my main form is made invisible to allow for a screen capture (excluding the main form)
    • My main form is made visible again (this is where i am having an issue)

    I cannot seem to focus back to the main form when it become visible. It reappears but sometimes depending on other applications, (for example: Visual Studio), that takes the focus instead. I need a guaranteed focus back to my main form with Inventor directly behind it.

    Im going to try loading the main form as ShowDialog to see if that helps but i really need the form not to be a dialog form because there is some interaction between the addin and the main application.

    Thanks

    Nacho


    Nacho is the derivative of Nigel - True fact! I am self taught in VB.Net. 50% of the time, I am right 100% of the time!

    Wednesday, December 06, 2017 11:37 PM

All replies

  • Hi

    I assume you have tried MainForm.Select when making it visible again.


    Regards Les, Livingston, Scotland

    Thursday, December 07, 2017 12:02 AM
  • I cannot seem to focus back to the main form when it become visible. It reappears but sometimes depending on other applications, (for example: Visual Studio), that takes the focus instead.

    That will depend on how addins work for that main form. If they work by starting an external process then there is no logical connection between the two applications, and Windows will manage the focus when the external application terminates.  That's probably why you are seeing varying behaviour - Windows takes notice of all events that occur, not just the two applications that you are concerned with.  If you can make the main form invisible then it's likely that you can force the focus back to it, but how you would do that would be application-specific.  Your best chance for a solution is with the documentation for managing add-ins for that main form - it's possible that a facility is provided for doing what you want.

    Thursday, December 07, 2017 12:27 AM
  • Hi NachoShaw,

    According to your description, perhaps Form.Activate Method () is helpful to you.

    https://msdn.microsoft.com/en-us/library/system.windows.forms.form.activate(v=vs.110).aspx

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, December 07, 2017 9:40 AM
    Moderator

  • That will depend on how addins work for that main form. If they work by starting an external process then there is no logical connection between the two applications, and Windows will manage the focus when the external application terminates.  That's probably why you are seeing varying behaviour - Windows takes notice of all events that occur, not just the two applications that you are concerned with.  If you can make the main form invisible then it's likely that you can force the focus back to it, but how you would do that would be application-specific.  Your best chance for a solution is with the documentation for managing add-ins for that main form - it's possible that a facility is provided for doing what you want.

    Hi

    The Addins work in the same process and not with an external process. They have be created as a DLL Class and placed into a specific location for them to work.

    Unfortunately the documentation isnt that thorough and a bit contradicting.... The API documentation only describes code and methods created in VBA inside inventor but the experts reccomend vb.net instead of VBA...

    Thanks


    Nacho is the derivative of Nigel - True fact! I am self taught in VB.Net. 50% of the time, I am right 100% of the time!

    Thursday, December 07, 2017 7:01 PM

  • According to your description, perhaps Form.Activate Method () is helpful to you.

    Hi

    Yes i have tried that. I have also tried 

    Form.Focus

    I am yet to try Form.Select which has been suggested :)

    Failing that, i will look at selecting the form by process

    Thanks


    Nacho is the derivative of Nigel - True fact! I am self taught in VB.Net. 50% of the time, I am right 100% of the time!

    Thursday, December 07, 2017 7:03 PM
  • Hi

    No yet.. Probably everything but. Im on it later, will give it a try

    Thanks


    Nacho is the derivative of Nigel - True fact! I am self taught in VB.Net. 50% of the time, I am right 100% of the time!

    Thursday, December 07, 2017 7:04 PM
  • Hi

    How about this in your main form code:

        Private Sub Form1_VisibleChanged(sender As Object, e As EventArgs) Handles MyBase.VisibleChanged
            If Visible Then
                [Select]()
            End If
        End Sub


    Regards Les, Livingston, Scotland

    Thursday, December 07, 2017 7:45 PM