Another post about closing a form via code - InfoPath Dev
in

InfoPath Dev

Use our Google Custom Search for best site search results.

Another post about closing a form via code

Last post 04-06-2009 08:31 AM by b.harrison. 8 replies.
Page 1 of 1 (9 items)
Sort Posts: Previous Next
  • 04-03-2009 08:00 AM

    Another post about closing a form via code

    I have two forms.  Form A has a link that opens Form B.  When B is opened it verifies some various user input, which is done in Form B's OnAfterChange() event code.  If the verification fails I want to close Form B.  I have tried all of the following:

    thisApplication.XDocuments.Close(1);
    thisApplication.XDocuments.Close(thisApplication.XDocuments[1]);
    thisApplication.XDocuments.Close(thisApplication.XDocuments[1].URI);
    thisApplication.XDocuments.Close(e.XDocument);
    thisApplication.XDocuments.Close(e.XDocument.URI);
    thisXDocument.View.Window.Close(true);
    thisXDocument.View.Window.Close(false);
    thisApplication.ActiveWindow.Close(true);
    thisApplication.ActiveWindow.Close(false);

    None of them work.  I get "Invalid context for OM call." every time.

    I looked at the posts http://www.infopathdev.com/forums/p/6165/22767.aspx#22767 and http://www.infopathdev.com/forums/p/3543/14114.aspx#14114 but neither provided a solution for this specific problem.  Is it just not possible to close a form from its own event function (I have also tried closing a form in the OnSubmit event code and had the same results)?  If I need to, I can just make a new view with an error message and a button that will close the form, and switch to this view if the validation fails, but I would really like to be able to do it all in code.  Both forms are running in Full Trust, if that matters.

    Any help or ideas are appreciated!

  • 04-03-2009 08:06 AM In reply to

    Re: Another post about closing a form via code

    Well, I just tried making a new viewbut when I try to switch to it in OnAfterChange I get the error: ""InfoPath cannot execute any view-related object model calls. The view is not ready."

     I think I am stuck.

  • 04-06-2009 07:32 AM In reply to

    Re: Another post about closing a form via code

    What version of InfoPath are you using? 2003 or 2007? C# for your code?

    Hilary Stoupa

  • 04-06-2009 07:38 AM In reply to

    Re: Another post about closing a form via code

    2007 and C#

  • 04-06-2009 07:48 AM In reply to

    Re: Another post about closing a form via code

    this.Close(); will close the form.

    Hilary Stoupa

  • 04-06-2009 08:03 AM In reply to

    Re: Another post about closing a form via code

    Thanks, Hilary, but I am getting a build error when I try that.

    When I type just "this." in visual studio, the intellisense brings up the list of all of the methods and members of the current class.  Is there something I need to do to make the this refer to the current document and not the current class?

    I just remembered that, although we are developing on 2007, we are using the backward compatibility option with 2003 (most of our users only have 2003 on their PCs).  This might be why it does not work?

  • 04-06-2009 08:14 AM In reply to

    Re: Another post about closing a form via code

    If you read through the documentation you discover:

    "the Close method can only be used with the OnSubmitRequest and OnClick event handlers. If used with any other type of event handler, it will return an error."

    I hope that helps.

  • 04-06-2009 08:15 AM In reply to

    Re: Another post about closing a form via code

    b.harrison:
    I just remembered that, although we are developing on 2007, we are using the backward compatibility option with 2003 (most of our users only have 2003 on their PCs).  This might be why it does not work?

    Sure is :-)

     thisXDocument.View.Window.Close(true);

    should do the trick.

    Edit:  I see you already tried that.  As Greg pointed out, you can only close the form on certain events. Perhaps you can find a workaround to meet that criterion?

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 04-06-2009 08:31 AM In reply to

    Re: Another post about closing a form via code

    Greg, Jimmy, thanks for your input!

    I did find a workaround for it.  If my custom validation fails, I set a field value in the newly opened form (Form B) to "Abort".  From the opening form (A), I can check this field value and if it is "Abort" I can close Form B from there using one of the methods above.

Page 1 of 1 (9 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.