Search code examples
winformstxtextcontrol

How do you avoid losing the formatting of text when populating a TXTextControl from a byte array?


I have a number of small "template" style TXTextControl documents which I combine to build a large document.

The templates are created in my app using a TXTextControl and the content is stored as a byte array in the database uisng the TXTextControl InternalFormat.

I retrieve each template document and build a new document using the Selection class to load the content of each one as follows;

tx.Selection.Load(myContent, BinaryStreamType.InternalFormat);

myContent is a Byte array read from the database.

Most of the template documents are just one line, but have some tabs altered in them, and may be centered, right justified etc.

What I've noticed is, if each small document ends with a linefeed (i.e. the user hits enter in the document before saving) the template document inserts correctly.

If there is no linefeed, the tabs and justification are lost. If the "template document"is more than one line, the formatting is lost for just the last line.

Worse still, if there is a TextField in the last position of the template document, then everything inserted into my new document after this template document, is inserted as if it is text within the TextField, making the new document look awful.

Is it possible to insert small amounts to TXTextControl InternalFormat style text from a byte array, and ensure it inserts exactly as it was created?

Alternatively, can I force the byte array I'm using to be terminated correctly - with a CR.LF or something to force it to be inserted correctly.


Solution

  • The only "workaround" I've managed to find for this is to append a new line after the inserted document.

    This only works though when building the text using a TXTextControl.TextControl object directly, NOT a reference to a selection object belong to a TextControl object.

    This works;

    TXTextControl.TextControl tx = new TXTextControl.TextControl();
    tx.CreateControl();
    
    tx.Selection.Start = -1;
    tx.Selection.Text = System.Enviornment.NewLine;
    

    But, passing a selection object into the process and passing that around to build the docoument within it's selection object, doesn't work. The formatting of the original control (and it's selection) is maintained, which probably makes sense I guess.