C# – Sql Reporting Services 2005 continue a long string from one textbox to another textbox

creporting-servicesreportingservices-2005textbox

I've got a SQL Reporting Services 2005 report that includes a textbox on the first page. The string input for the textbox can be very large and include newlines. The textbox size is fixed and another textbox is included on the second page of the report to handle any spillover text that didn't fit in the first page's textbox. If the second page textbox becomes full, then I would like to include a "…" at the end of the text to show that some text was cut off.

I've tried using the TextRenderer.MeasureText() method but it appears to only work on single lines. I'm trying the following code

string str = string.Copy(commentString);
TextFormatFlags flags = TextFormatFlags.WordBreak |
                        TextFormatFlags.WordEllipsis |
                        TextFormatFlags.ModifyString;
float textBoxWidthInches = 3.8f;
float textBoxHeightInches = 3.4f;
Size size = new Size(
    (int)(textBoxWidthInches * 72 - 2),
    (int)(textBoxHeightInches * 72 - 2));
TextRenderer.MeasureText(str, new Font("Arial", 8), size, flags);

I then expect the str to include a '\0' at the point where I need to break my string, however, it isn't showing up. If I remove the WordBreak flag and input a string with a long first line, it does include the '\0' at the correct spot for the first line, but it is only working for a single line.

My questions are:

1) How do I "continue" text from one textbox to another in Sql Reporting Services 2005?

2) If not, how can I calculate where I need to break my string up so that it fits inside a textbox?

3) Optionally I'd like to include a "…" at the end of the second textbox for text that is longer than both textboxes can fit.

Edit:
What I'm trying to accomplish is something like this:

|------------------------|
|                        |
|     Header Page 1      |
|------------------------|
|        |               |
|        |               |
|TextBox1|               |
|        |               |
|--------|               |
|                        |
| Other Data             |
|                        |
|                        |
|                        |
|                        |
|                        |
|                        |
|                        |
|------------------------|


|------------------------|
|                        |
|     Header Page 2      |
|------------------------|
|        |               |
|        |               |
|TextBox2|               |
|        |               |
|--------|               |
|                        |
| Other Data             |
|                        |
|                        |
|                        |
|                        |
|                        |
|                        |
|                        |
|------------------------|

I then want TextBox1 to continue to TextBox2 on the second page. Using the CanGrow attribute will not get the desired behavior.

Best Answer

Here is what I ended up with:

  public static IList<string> WordWrap(
     string text,
     Font printFont,
     Graphics graphics,
     IList<SizeF> sizeF,
     string tooLongText)
  {
     List<string> list = new List<string>();
     int charsFit;
     int linesFilled;

     foreach (SizeF size in sizeF)
     {

        graphics.MeasureString(
           text,
           printFont,
           size,
           new StringFormat(),
           out charsFit,
           out linesFilled);

        char[] whitespace = new[] { ' ', '\t', '\r', '\n' };
        int index = charsFit;

        if (text.Length > charsFit)
           index = text.LastIndexOfAny(whitespace, charsFit);

        if (index < 0) index = charsFit;

        string rv = text.Substring(0, index).Trim();
        text = text.Substring(index).Trim();
        list.Add(rv);
     }

     if (!string.IsNullOrEmpty(text))
     {
        string lastText = list[list.Count - 1];
        SizeF size = sizeF[sizeF.Count - 1];
        charsFit = 0;
        string newLastText = lastText + Environment.NewLine + tooLongText;
        while (charsFit < newLastText.Length)
        {
           graphics.MeasureString(
              newLastText,
              printFont,
              size,
              new StringFormat(),
              out charsFit,
              out linesFilled);
           lastText = lastText.Substring(0, lastText.Length - 1);
           newLastText = lastText + Environment.NewLine + tooLongText;
        }

        list.RemoveAt(list.Count - 1);
        list.Add(newLastText);
     }

     return list;
  }

I then generate two public string properties for my two textboxes. If this function returns a list with count > 0, first textbox value = list[0]. If list count > 1, second textbox value = list[1]. Not the best code I'm sure, but it worked for my needs.