Add a field's value when new line is created.... - InfoPath Dev
in

InfoPath Dev

Add a field's value when new line is created....

Last post 08-13-2008 08:54 AM by Will Dieterich. 10 replies.
Page 1 of 1 (11 items)
Sort Posts: Previous Next
  • 07-31-2008 10:01 AM

    • dlocc
    • Top 500 Contributor
    • Joined on 06-23-2008
    • San Diego
    • Posts 28

    Add a field's value when new line is created....

    Hello,

    I have followed along with the following tutorial showing how to add lines of text to a rich text box here: http://enterprise-solutions.swits.net/blog/?p=26#comment-282 

    the code works great and adds the text "Line 1" and "Line 2"

    XPathNavigator nav = MainDataSource.CreateNavigator();
    XPathNavigator rtfNav = nav.SelectSingleNode("//my:rtfField", NamespaceManager);
    using (XmlWriter rtfWriter = rtfNav.AppendChild())
    {
      rtfWriter.WriteStartElement("div", "http://www.w3.org/1999/xhtml");
      rtfWriter.WriteString("Line 1");
      rtfWriter.WriteEndElement();
      rtfWriter.WriteStartElement("div", "http://www.w3.org/1999/xhtml");
      rtfWriter.WriteString("Line 2");
      rtfWriter.WriteEndElement();
      rtfWriter.Close();
    }

     

    What I need though is to have the value of a Drop Down List show instead of the text "Line 1".  So when a user selects the value in the drop down and clicks the button it adds the value to the new line?  Get it?  Let me know if you have any questions. Thanks!

     

     

    Devin Walker
    http://www.dlocc.com
  • 08-05-2008 05:51 AM In reply to

    Re: Add a field's value when new line is created....

    I don't quite understand. Would the drop down list be inside or outside of the rich text box? If it's outside, you would just replace "Line 1" with a .Text value from the node or attribute associated with the drop down box a similar way to what you have done for rtfNav.

    I would imagine that to be the case.

  • 08-05-2008 08:47 AM In reply to

    • dlocc
    • Top 500 Contributor
    • Joined on 06-23-2008
    • San Diego
    • Posts 28

    Re: Add a field's value when new line is created....

    Hello Ben Oates,

    The drop down would be outsite of the rich text box.  So basically, whenever someone selects a value in the drop down and clicks the buton it adds the text to the rich text box.  I know it's a simple code solution, and it sounds like replacing the "line 1" with .Text value.  I have some idea how to do this, but I have always been getting errors... do you have a code example I could go from or some tips?  Thanks again.

     

    Devin Walker
    http://www.dlocc.com
  • 08-05-2008 09:04 AM In reply to

    Re: Add a field's value when new line is created....

    I don't have 2007 so I can't send you an exact example of what you would be looking for, but you have already navigated to a node with the following line:

    XPathNavigator rtfNav = nav.SelectSingleNode("//my:rtfField", NamespaceManager); 

    So what you would need to do is set the address of your drop down box in the place of your "//my:rtfField" option and then use Text or Value to get its content.

    If you still get an error, break out and add a watch on nav.select etc. and find out what the name of the value is.

    rtfWriter.WriteString(nav.SelectSingleNode("//my:dropDownListField", NamespaceManager).Text);

    Remember that JScript/C# etc. are case sensitive... I don't know if SelectSingleNode or Text should start with lower case letters and I don't know if Text should be Value etc.. You'll have to work that out if you get an error saying the method isn't available.

  • 08-05-2008 09:11 AM In reply to

    • dlocc
    • Top 500 Contributor
    • Joined on 06-23-2008
    • San Diego
    • Posts 28

    Re: Add a field's value when new line is created....

    Thanks for the prompt reply, I think I know how to tackle this problem now... I will post the final code (hopefully) when I get it working.  If not, maybe another question :]

    Devin Walker
    http://www.dlocc.com
  • 08-05-2008 11:29 AM In reply to

    • dlocc
    • Top 500 Contributor
    • Joined on 06-23-2008
    • San Diego
    • Posts 28

    Re: Add a field's value when new line is created....

          XPathNavigator nav = MainDataSource.CreateNavigator();
                XPathNavigator rtfNav = nav.SelectSingleNode("//my:rtfField", NamespaceManager);
                using (XmlWriter rtfWriter = rtfNav.AppendChild())
                {
                    rtfWriter.WriteStartElement("div", "http://www.w3.org/1999/xhtml");
                    rtfWriter.WriteString(nav.SelectSingleNode("//my:Actions", NamespaceManager).Text);
                    rtfWriter.WriteEndElement();
                    rtfWriter.WriteStartElement("div", "http://www.w3.org/1999/xhtml");
                    rtfWriter.WriteString("Line 2");
                    rtfWriter.WriteEndElement();
                    rtfWriter.Close();
                }

     

    here is the code I am using, I get a build error and it says the ".Text" does not contain a value... and comes back underlined in VSTA

    Devin Walker
    http://www.dlocc.com
  • 08-05-2008 11:37 AM In reply to

    Re: Add a field's value when new line is created....

    .Value for IP2007 Object Model, instead of .Text

    Hilary Stoupa

  • 08-05-2008 11:45 AM In reply to

    • dlocc
    • Top 500 Contributor
    • Joined on 06-23-2008
    • San Diego
    • Posts 28

    Re: Add a field's value when new line is created....

    Awesome, thanks a lot! I got it working!

    Devin Walker
    http://www.dlocc.com
  • 08-05-2008 05:42 PM In reply to

    • dlocc
    • Top 500 Contributor
    • Joined on 06-23-2008
    • San Diego
    • Posts 28

    Re: Add a field's value when new line is created....

    Hello again,

     sorry to keep this thread going, but I have another problem... when I use the above code on a new form with just a rich text box field, a button, and a drop down list it works perfectly.  The fields are added like so:

    Line 1

    Line 2

     

    But when I try to implement the same exact rich text box, drop down, button and code in an already existing form the items do not add line breaks so the data displays side-by-side:

    "Line 1Line 2Line 1Line 2Line 1Line 2"

     No idea why this is happening... any advice is appreciated as always. :]

     

     

    Devin Walker
    http://www.dlocc.com
  • 08-06-2008 01:47 AM In reply to

    Re: Add a field's value when new line is created....

    Hi dlocc,

    Without seeing your code I'm going to have to imagine you have been careful to make sure it is identical, but have you checked the Properties of the Rich Text Box? Under the display tab I believe there is a paragraph breaks tick box that may not have a tick in it.

    Hope this helps,

    Ben

  • 08-13-2008 08:54 AM In reply to

    Re: Add a field's value when new line is created....

     Since you are just using the values of a drop down list and not fancy images,etc how about dropping the rich text and just going with a text box.  The standard text box can be made multi-line and it is alot easier to add text and the text added keeps newlines, spaces,etc.

     As for the problem of items going on the same line you are probably not creating a new object and changing the value of the existing innertext.

     

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