TextField on the sheet: is it possible to fill it?

Hello everyone,

I have an question, is it possible to manipulate the "Text box" element from c# trough FlexCel?

For example I need to set the correct text into the element.


You can do this with SetObjectText.  For example, if you have a textbox named "mybox", you could use code like this:

            XlsFile xls = new XlsFile("r:\test.xlsx", true);
            xls.SetObjectText(-1, "@mybox", "hello world");

You can use this code to set the text of any object, not just a textbox.

Note, that in this example I expect to have a textbox named "mybox". If the textbox is not named, it will show in Excel like "TextBox 1" or similar: Even if Excel shows a name, the textbox doesn't have any, and the code:
            xls.SetObjectText(-1, "@TextBox 1", "hello world");
will probably fail. You need to name the combobox.

If you can't name it, then you'll have to find out the object index, and use
            xls.SetObjectText(objectIndex, null, "hello world");


Hello Adrian,

thank you for your help, it works fine. Now I have another problem, the font of my "Text Box" has been changed after this call. Is it possible to set the font for FlexCel.Core.TRichString that I will send into the ExcelFile.SetObjectText  ?

this code throws an exception also:

                    FlexCel.Core.TRichString caption = new FlexCel.Core.TRichString ("Hello world");

                    for (int i = 1; i <= caption.Length; i++)
                        FlexCel.Core.TFlxFont fontLetter = caption.GetFont(i);
                        fontLetter.Size20 = 480;

        // Summary:
        //     Return the font for character i.
        // Parameters:
        //   i:
        //     index of the font.


Changing the text shouldn't change the font. Investigating I've just found that there is a bug that could be causing this, we'll be making a fix probably today.

Email me to adrian@tmssoftware.com and I'll send you a fix.

About the other code: the problem is that the index in TRichStrings is 0 based, not 1 based (I've just updated the docs to say this explicitly)

It should be:
 for (int i = 0; i < caption.Length; i++)

But I think you are misunderstanding the use of TRichString. You don't need to change every single letter, every run applies from position to the end of the string.

So you could just need to set the font at position 0. You could do it with:
 TRTFRun run = new TRTFRun();
            run.FirstChar = 0;
            TFlxFont fnt = xls.GetDefaultFont;
            fnt.Size20 = 480;
            run.FontIndex = xls.AddFont(fnt);
            FlexCel.Core.TRichString caption = new FlexCel.Core.TRichString("Hello world", new TRTFRun[] {run}, xls);

But well, once we fix this it shouldn't be necessary. If you set a string, the font should be preserved.

Thank you for the fix update. Now it works perfectly.