Line breaks when clipboarding a field through button form code - InfoPath Dev
in

InfoPath Dev

Line breaks when clipboarding a field through button form code

Last post 12-11-2009 09:09 PM by Cjpbolton. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 12-11-2009 09:44 AM

    Line breaks when clipboarding a field through button form code

    Hello Infopathdev! I have quite an interesting issue to share with you folks today. I'll start off by stating VB is not my strongpoint. But because the builder in infopath can literally teach you how to write VB i've gotten a pretty good grasp of the syntaxs and what not.

    I'll start off by explaining how I have my form laid out. I do not have this form set up for used to "submit" it in the traditional sense. Users fill out the fields in the form, once complete they press a button at the bottom of the form and it uses a formula to populate a text box with the information provided in the fields. That way they can just fill out the fields, click the button, and copy/paste from the textbox to notepad.

     I ran into some troubles yesterday with the button. It would post all of the fields properly, but the text box would not do line breaks as the button at "set the fields value" was a function. To do this I had to create and XML file and add it as a secondary source so that I could use @crlf fields in my function to red-neck a line break in the text box, which mind you, outside of a little extra legwork on building the functions, works fine. The field would correctly populate like this:

     Butter

    Eggs

    Milk

    Bread

    However, today I decided to create another button. This button, through some vb form coding, should allow me to copy the field to clipboard by simply clicking it. The code is as follows:

     Dim nodeToCopy As XPathNavigator = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:field10", NamespaceManager)

    If Not String.IsNullOrEmpty(nodeToCopy.Value) Then

    Clipboard.SetText(nodeToCopy.Value, TextDataFormat.CommaSeparatedValue)

    Else

    MessageBox.Show("There is no text to copy")

     

    Now heres the issue, after building it and saving my form, it will correctly copy the field to clipboard almost like it should, heres my problem. When I go to paste this info, it loses its linebreaks like

    ButterEggsMilkBread

    Iv'e tried many different text formats at "TextDataFormat.CommaSeparatedValue) and nothing seems to work.

    I'm open to any advice

  • 12-11-2009 06:47 PM In reply to

    Re: Line breaks when clipboarding a field through button form code

     It seems that even if you use that crlf trick, InfoPath changes the line breaks from CRLF to just LF at some point. Not sure why this is happening, but you can do the following to get around this.  This is C# but it sounds like you can figure out the VB equivalent:

    string value = nodeToCopy.Value;
    if(!value.Contains("\r\n"))
    {
       value = value.Replace("\n", "\r\n");
    }

    Clipboard.SetText(value);

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 12-11-2009 09:09 PM In reply to

    Re: Line breaks when clipboarding a field through button form code

     You are an absolute genious, thank you SO much.

     

    For anyone who may hit this site in the future, this code worked for me.

     

     

                Dim value As String = "/my:myFields/my:field10"
                If value.Contains("/characters/@crlf") Then
                    value.Replace("/characters/@crlf", "\n\r")
                End If

                Dim nodeToCopy As XPathNavigator = MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:field10", NamespaceManager)
                If Not String.IsNullOrEmpty(nodeToCopy.Value) Then

                    Clipboard.SetText(nodeToCopy.Value, TextDataFormat.Text)

                Else

                    MessageBox.Show("There is no text to copy")
                End If

     


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