VSTA "ReplaceSelf" programming issue/ clearing section fields challenge - InfoPath Dev
in

InfoPath Dev

Use our Google Custom Search for best site search results.

VSTA "ReplaceSelf" programming issue/ clearing section fields challenge

Last post 10-26-2009 06:40 AM by seidensc. 5 replies.
Page 1 of 1 (6 items)
Sort Posts: Previous Next
  • 10-22-2009 11:20 AM

    VSTA "ReplaceSelf" programming issue/ clearing section fields challenge

    This is my first InfoPath form.  We have InfoPath as part of our desktop install.  SharePoint is coming but will not be here until next year.  I also have VSTA installed.  I state this in case it makes a difference on this post.

    I have the following fields/containers within the myfields container:

    AcademicPerformance (Check box)   
    AcademicPerformanceGroup
        CurrentGrade*
        Tutor (Check box)
        TutorGroup
            TutorName
            TutorPersonID

    On the AcademicPerformanceGroup I have conditional formatting which hides the section if the AcademicPerformance check box is false.  What I have noticed is that if a "form filler" enters info into the Academic Performance and Tutor groups and then uncheck the Academic Performance check box the section is collapsed but the data remains.  I would like the default behavior of unchecking the box to throw up a warning (next programming iteration) and then clear the subordinate fields before collapsing.

    This is what I have come up with so far:

    using Microsoft.Office.InfoPath;
    using System;
    using System.Windows.Forms;
    using System.XML;
    using System.Xml.XPath;
    using mshtml;

    namespace StudentReferral
    {
      public partial class FormCode
      {
        private string initialAPData;
        public void InternalStartup()
        {
          EventManager.XmlEvents["/my:myFields/my:AcademicPerformance"].Changing += new XmlChangingEventHandler(AcademicPerformance_Changing);
          EventManager.FormEvents.Loading += new LoadingEventHandler(FormEvents_Loading);
        }
        public void AcademicPerformance_Changing(object sender, XmlChangingEventArgs e)
        {
          String fieldValue = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:AcademicPerformance", NamespaceManager).Value;
          if (fieldValue == "false")
          {
    MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:AcademicPerformanceGroup", NamespaceManager).ReplaceSelf(initialAPData);
          }
        }

        public void FormEvents_Loading(object sender, LoadingEventArgs e)
        {
          initialAPData = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:AcademicPerformanceGroup", NamespaceManager).OuterXml;
        }
      }
    }

    First of all, is there a better way of clearing several containers/fields within a section(s) rather than resorting to code? 
    If not, I am getting a little error that if someone can see I would greatly appreciate it.  Once I get beyond the error I plan to add an ok/cancel dialog and then add the code to all my expanding sections.

    When the "MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:AcademicPerformanceGroup", NamespaceManager).ReplaceSelf(initialAPData);" statement executes I receive the following error:

    A first chance exception of type 'System.InvalidOperationException' occurred in Microsoft.Office.InfoPath.Client.Internal.Host.Interop.dll

    InitialAPData (after the form load event) =
    <my:AcademicPerformanceGroup xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-09-28T17:12:30">
      <my:CurrentGrade></my:CurrentGrade>
      <my:Tutor>false</my:Tutor>
      <my:TutorGroup>
       <my:TutorName></my:TutorName>
       <my:TutorPersonID xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"></my:TutorPersonID>
      </my:TutorGroup>
     </my:AcademicPerformanceGroup>
     
    Any ideas why this error would be encountered? 

  • 10-22-2009 05:33 PM In reply to

    Re: VSTA "ReplaceSelf" programming issue/ clearing section fields challenge

     InfoPath will not allow you to change the contents of the data source in a Changing event.  Please try adding that code to AcademicPerformance's Changed event.

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 10-23-2009 12:19 PM In reply to

    Re: VSTA "ReplaceSelf" programming issue/ clearing section fields challenge

    Thanks for your reply Jimmy.

     I added the logic to the Changed event as you indicated.  It worked well. 

    On to adding the other groups.  This was not too successful.  I have to spend some time to see what I did wrong; not sure what, but all I did was copy the code and paste the names in from xcopy. 

     I also looked at the ok/cancel functionality.  So far I can't find out how to instantiate xdocument to get at the UI.  It looks like it should be automatic but under VS2008/InfoPath 2007 it is not.  I'm not familiar enough with the API to know if it was moved, changed, or what.

     Anyway, a big thank you for making my day brighter with the tibit you passed along.

    Steve

  • 10-23-2009 12:52 PM In reply to

    Re: VSTA "ReplaceSelf" programming issue/ clearing section fields challenge

    The other groups are now working too with a little help with the debugger. 

    I'm still stuck on how to throw up a ok/cancel that the fields are about to be cleared by collapsing a section.  If you can shed any light on how to do this it would be appreciated.  Note environment on my last post...

     

  • 10-23-2009 07:31 PM In reply to

    Re: VSTA "ReplaceSelf" programming issue/ clearing section fields challenge

     Are you saying you're looking for the thisXDocument.UI.confirm() function?

    In IP 2007 you should just use the usual System.Windows.Forms API:

    DialogResult response = MessageBox.Show("Are you sure you want to do this?", string.Empty,
        
    MessageBoxButtons.YesNo);
    if (response == DialogResult.Yes)
    {
        
    // Do stuff
    }

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 10-26-2009 06:40 AM In reply to

    Re: VSTA "ReplaceSelf" programming issue/ clearing section fields challenge

    Thanks again Jimmy; you've been very helpful!

     

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