Transfer text from Rich Text Box

A

Andrew

Does anyone know of a way to transfer text from one rich text box to another
and being able to keep the return lines in there.

Thanks,
Andrew
 
A

Adam Harding

Hi

You can transfer the text but the carriage returns will not pass accross.

just set the second RTB's default value to the first job done.

Cheers Adam
 
S

S.Y.M. Wong-A-Ton

The contents of a rich text box is stored as XHTML. To preserve line breaks,
which are essentially achieved through the use of <DIV> elements within the
contents of the rich text box, you would have to copy the entire XML content
of the node pertaining to the first rich text box, create a new node, set the
XML content of the newly created node to the content copied from the first
rich text box, and then replace the node pertaining to the second rich text
box with the new node using replaceChild().
 
A

Andrew

It seems that InfoPath will not allow me to do this in code. I think I might
have to write a webservice to transfer the text.
 
S

S.Y.M. Wong-A-Ton

If your scenario includes simply copying text from one RTF to another on one
InfoPath form, then this is doable through code without using a web service.
I don't understand why you would need a web service. Is there a database
(that you haven't mentioned) somewhere in the equation?
 
A

Andrew

Nope no database. I have code behind in c#. I created a test form that just
has a Rich TextBox a and b. And when the user submits the form I would like
to transfer the Rich Text from A to B and beable to keep the line breaks.

I've tried to do something like this
Node =
thisXDocument.DOM.selectSingleNode("/my:myFields/my:a").cloneNode(False)
thisXDocument.DOM.selectSingleNode("/my:myFields/my:b").appendChild(Node)

I do understand why that won't work but I'm still new to XML.

and If I do this ...
thisXDocument.DOM.selectSingleNode("/my:myFields/my:b").text =
thisXDocument.DOM.selectSingleNode("/my:myFields/my:a").text

Then it copies the text over without keeping the line breaks.

Thanks for any help!
- Andrew
 
S

S.Y.M. Wong-A-Ton

If your RTF-a contains line breaks and you put a breakpoint on e.g.

string RTF-a = thisXDocument.DOM.selectSingleNode("/my:myFields/my:a").xml;

you will see a bunch of <DIV> elements contained in <my:a>. What worked for
me was to build a loop over the childNodes contained in <my:a> and then
compose a string from these childNodes. I haven't yet found a "nicer" way to
copy the XML inside a node. You can do a check on

thisXDocument.DOM.selectSingleNode("/my:myFields/my:a").hasChildNodes()

before starting the loop over the childNodes. Then you must retrieve the
nodeName, nodeType, and namespaceURI of <my:b> (these are all properties of a
node) and use them to create the new XML for this node as a string that
contains the inner XML copied from <my:a>.

You can then use thisXDocument.CreateDOM() to instantiate a new XML node
(we'll call this newChild), and then newChild.loadXML() to load the XML
string for the new <my:b> into this new XML node.

Then you retrieve the old <my:b> (let's call it oldChild) and with a
replaceChild(), you replace it with the new XML node for <my:b> like this

oldChild.parentNode.replaceChild(newChild.documentElement, oldChild);

Hope this helps. If you're still having difficulty getting this to work,
I'll convert your scenario to a "recipe" (=solution) and post it on my
website this weekend.
 
A

Andrew

Thank you for all of your help!! I got it working this morning. The only
difference I had to make was I also needed to keep everything that is in the
OldChile. So that the new text would be first then the old test second.

Oh you mentioned that you have a website, may I have the web address to
check it out?

Thank you again!!

- Andrew
 
G

Gustavo Hernandez

Hi My name is Gustavo from Monterrey mexico

I have the same problem, but i have one Rich Text field with tables, text in
bold, text in different size, etc...and i would like to copy the values from
one field to another field,
do you think this code could be used also to pass that kind of information?
 
S

S.Y.M. Wong-A-Ton

If the other field is a richt text field, then yes, the same principles
should apply. If you're using InfoPath 2007, you might even get away with
using the InnerXml property to transfer the entire contents in one step; I
haven't tried it out, though. I would say: Just give it a go and see if it
works.
 
G

Gustavo Hernandez

Thanks for your quick answer.

And yes both fields are rich text. I'm trying to use your suggestion about
how to transfer text from one field to another, but i'm having problems when
i want to do the replace child instruction. My field "B" does not have any
child node....so,
How can i create a new child for my field "B" where i want to pass the rich
text value from my field "A" ?, i mean i can do a loop for my field "A" and
get the XML data, but i don't understand how to pass every XML data for each
child of my field "A" to my field "B".
 
G

Gustavo Hernandez

I'm using InfoPath 2003. Finally i found a way to do it. A little different
than your suggestions, but following your instructions. One thing different
that i did was copy the xml of my field A into a string variable and only
replace the name of the child for the field2 and then do a replace child from
parent node of both fields.

IXMLDOMNode node1 =
doc.DOM.documentElement.selectSingleNode("./my:parentnode/my:Field1");

if (node1 != null)
{

IXMLDOMDocument newchild = doc.CreateDOM();
childtext = node1.xml.ToString();
string newtxtofnode2= childtext.Replace("NameOfChild_1",
"NameOfChild_2");
newchild.loadXML(newtxtofnode2);

oldchild =
doc.DOM.documentElement.selectSingleNode("./my:parentNode/my:Field2");

oldchild.parentNode.replaceChild(newchild.documentElement, oldchild);
}


And it works !!

Thanks for your help !
 
Joined
Jul 4, 2010
Messages
1
Reaction score
0
Copy rich text from one rich text box to another

I know this thread is quite old but I needed this solution. After reading this and many other solutions, I came up with mine for VB script. I am using InfoPath 2003.

Here is my solution.

Sub CopyRichText(ByRef RichTextNode1, ByRef RichTextNode2)
'Copies the richtext (xml) from one richtext box to another
Dim objXMLNodes, ParentNode2, XML, Nodename1, Nodename2, xmlDoc

Nodename1 = RichTextNode1.nodeName
Nodename2 = RichTextNode2.nodeName

XML = RichTextNode1.xml
XML = Replace(XML, Nodename1, Nodename2)

'Create new temporary node with new XML
Set xmlDoc = XDocument.CreateDOM()
xmlDoc.loadxml XML

Set ParentNode2 = RichTextNode2.selectSingleNode("..")
ParentNode2.replacechild xmlDoc.documentElement, RichTextNode2
End Sub

You can use it basically like this.

Node1 = XDocument.DOM.selectSingleNode("./my:myFields/my:Richtextfield1")
Node2 = XDocument.DOM.selectSingleNode("./my:myFields/my:Richtextfield2")

CopyRichText Node1, Node2

It works great for me.
--Jim--
 
Last edited:
S

scottdavis.photography

I was able to get this working with a single line of VB code:

Me.MainDataSource.CreateNavigator.SelectSingleNode("/my:b",NamespaceManager).InnerXML = Me.MainDataSource.CreateNavigator.SelectSingleNode("/my:a",NamespaceManager).InnerXML

This is in InfoPath2010, creating a 2007-compatible form.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top