programmatically create rectangle for your dynamic string via drawstring

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nothix9
    New Member
    • Jul 2007
    • 5

    programmatically create rectangle for your dynamic string via drawstring

    Hello guys, Ive been working this for 2 days, I am required to position a string using drawstring but the problem is its dynamic and changes depending on the database or source. A quick fix I thought was to position it at the end of the report but its required at the middle of other entries. Can you give me tips and different approach how to programmaticall y adjust the height of the rectangle depending on the string it would need to write?

    here's my approach:

    i used a data structure approach like of an stack but not literally a pushes and pops.

    i measured how many pixels it would take for a single line of string could be possible. and its 15. I made a method that returns how many lines (which is 15 pixels per line) it is possible for the string that you will pass as a parameter. if the string is all upper its possible to limit each line with a fixed number of characters posible by giving the constant of the rectangle's length. But the problem that I encounter is that it has special characters and spaces which mean it will adjust to the next line if it is not possible for it the write it from the previous line. If the string is too long again for the next line it would randomly concatenate it self to the next. I made again a method but what keeps me scratching my head is beacause there will be always no fix number of characters per line. im coding in c#. im also using devexpress xtraprinting library,

    <start of code snippet>
    float line=GetLine(ch ecknull,31) // string input then supposedly number of characters.

    rec = new RectangleF(230, y, 208, (float)(line * 13));
    e.Graph.DrawStr ing(/*tempstring*/checknull, Color.Black, rec, DevExpress.Xtra Printing.Border Side.None);

    private float GetLine(string tempstring, int size)
    {
    int ctr = 0;
    int line = 0;
    int posnext = 0;
    int pos = 0;
    bool space = false;
    int word = 0;
    bool cutoff = false;
    float retline = 0;



    CharEnumerator charEnum = tempstring.GetE numerator();
    while (charEnum.MoveN ext())
    {
    if (Convert.ToInt3 2(tempstring[ctr]) == 32)
    {
    space = true;
    pos++;
    posnext++;
    word++;
    }
    else
    {
    space = false;
    pos++;
    posnext++;
    word++;
    }
    if (posnext == size)
    {
    cutoff = true;
    pos = word;
    posnext = 0;
    }
    if (space == true && pos < size && cutoff==false )
    {
    line += word;
    word = 0;
    }
    if (space == true && cutoff == true)
    {
    if (word > size)
    {
    while (word > size)
    {
    word-=size;

    retline++;
    }

    line += word;

    if (line > size)
    {
    pos = line - size;
    retline++;
    line = pos;
    word = 0;
    }
    cutoff = false;
    }
    else
    {
    retline++;
    line = pos;
    word = 0;
    cutoff = false;
    }
    posnext = pos;
    }
    ctr++;
    }
    if (line < size)
    {
    retline += 2;
    }
    retline+=1;
    return retline;
    }

    <end of code snippet>
    here is a sample string input:
    1) MAX(PC!.ADVISED AMT, PC!.DETAMT)
    2) IF(PC!.ADCBCGRA DETP="BCGRADESC LEANING",IF(ISE RROR(VLOOKUP(PC !.ADCPOSI,PT.BC GRADESCLEANING, 2,FALSE)),"",VL OOKUP(PC!.ADCPO SI,PT.BCGRADESC LEANING,2,FALSE )),IF(PC!.ADCBC GRADETP="BCGRAD ESELECTRICAL",I F(ISERROR(VLOOK UP(PC!.ADCPOSI, PT.BCGRADESELEC TRICAL,2,FALSE) ),"",VLOOKUP(PC !.ADCPOSI,PT.BC GRADESELECTRICA L,2,FALSE)), IF(PC!.ADCBCGRA DETP="BCGRADESM EIBC",IF( ISERROR(VLOOKUP (PC!.ADCPOSI, PT.BCGRADESMEIB C,2,FALSE)),"", VLOOKUP(PC!.ADC POSI,PT.BCGRADE SMEIBC,2,FALSE) ),IF(PC!.ADCBCG RADETP="BCGRADE SMIBCO",IF( ISERROR(VLOOKUP (PC!.ADCPOSI, PT.BCGRADESMIBC O,2,FALSE)),"", VLOOKUP(PC!.ADC POSI,PT.BCGRADE SMIBCO,2,FALSE) ),IF(PC!.ADCBCG RADETP= "BCGRADESNRFBC" ,IF( ISERROR(VLOOKUP (PC!.ADCPOSI, PT.BCGRADESNRFB C,2,FALSE)), "", VLOOKUP(PC!.ADC POSI,PT.BCGRADE SNRFBC,2,FALSE) ),"")))))

    found this article here and scans it: http://www.thescripts.com/forum/thread262845.html

    First time to post on this forum. I'll reply as soon as posible tnx
    thanks sorry for my english. using xp. any suggetions?
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    The Graphics object's DrawString() is overloaded to do string wrapping for you, but you do need to pass it that rectangle. (Actually I think it wraps based on characters, so you might have to do some work)

    You seem to have a fixed width. That leaves the height (like you said) up for negotiation.
    I would say, measure the string and then divide that by a value (probably like 2-3pixels shy of your width, or more depending on if you want padding) that will give you a rough esitimate of how many lines there are if wrapping occured based on characters and not whole-words.
    After that my mathmatical approach gets fudgy.
    You could just add 1 to your line count and guess that it will be correct.
    You could count the number of words and for each one add another X amount of pixels to your original string length and then divide by your value (same as above, it'll be around your width) to get the number of lines.

    You could also take your string and iterate up throught the characters until you have reached the "last word-break before the maximum amount of pixels per line". If not word-break is found by the time you reach maximum pixels per line. Move back one character and add a "-" to denote that the word continues on the next line. That is one line. Keep repeating the process until you have gone through the whole string.

    Comment

    • nothix9
      New Member
      • Jul 2007
      • 5

      #3

      here's to make it clear. the total number of words in there are 5.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        So you already have a definitive method for knowing how many lines the given string will require?

        What's the problem then?

        Comment

        • nothix9
          New Member
          • Jul 2007
          • 5

          #5
          Originally posted by Plater
          So you already have a definitive method for knowing how many lines the given string will require?

          What's the problem then?
          that's my idea, but i have tried two shots to attain this, no luck for me.

          Comment

          Working...