length of string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cc

    #1

    length of string

    Hi,

    From a C# application am I writing some text in a Word.Table, in a Cell of
    the Table to be more precise.

    The cell has a certain width and any string written in the cell will cover
    one or more lines in the cell depending on its length. In other words, it
    will use wordwrapping

    Now, I have to determine the amount of lines that the text will take when
    written in the cell.
    How can I do this ?

    thnx
    Chris


  • dkode

    #2
    Re: length of string

    where is your string coming from before you insert it into the table?
    StringBuilder? String array?

    need some more info/code snippet to help

    Comment

    • Ignacio Machin \( .NET/ C# MVP \)

      #3
      Re: length of string

      Hi,

      You can use Graphics.Measur eString but you need to know the font, another
      problem is the wordwrapping , I bet you would have to build your own
      wrapping algorith and see how many lines you get


      --
      Ignacio Machin,
      ignacio.machin AT dot.state.fl.us
      Florida Department Of Transportation

      "cc" <cmrchs@yahoo.c om> wrote in message
      news:44170a6e$0 $15359$ba620e4c @news.skynet.be ...[color=blue]
      > Hi,
      >
      > From a C# application am I writing some text in a Word.Table, in a Cell of
      > the Table to be more precise.
      >
      > The cell has a certain width and any string written in the cell will cover
      > one or more lines in the cell depending on its length. In other words, it
      > will use wordwrapping
      >
      > Now, I have to determine the amount of lines that the text will take when
      > written in the cell.
      > How can I do this ?
      >
      > thnx
      > Chris
      >
      >[/color]


      Comment

      • cc

        #4
        Re: length of string

        Hi,

        it is just a string.

        some code :
        m_table1.Cell(3 , 4).Width = 200;
        m_table1.Cell(3 , 4).Range.Text = "This is my test string. It's length can
        vary..."

        "dkode" <dkode8@gmail.c om> wrote in message
        news:1142361785 .950597.211720@ j52g2000cwj.goo glegroups.com.. .[color=blue]
        > where is your string coming from before you insert it into the table?
        > StringBuilder? String array?
        >
        > need some more info/code snippet to help
        >[/color]


        Comment

        • dkode

          #5
          Re: length of string

          you can do the following:

          string myString = "This is my test string. It's length can vary.";

          m_table1.Cell(3 , 4).Width = 200;
          if (myString.Lengt h > 200) {
          // do processing or break apart the string into seperate lines I.E.
          string stringPart = myString.Substr ing(0, 200);
          string stringPart2 = myString.Substr ing(200, 200);
          }

          is this what your looking for?

          Comment

          • Ignacio Machin \( .NET/ C# MVP \)

            #6
            Re: length of string

            Hi,

            I don't think this is what the OP wants, he wants to know how many rows were
            used to display the string when it was drawn in the screen.

            The cell width is a pixel value, where the string length is in char, of
            course one char does not fit in one pixel , so there is where the thing gets
            nasty :)

            OP:
            See my other post to how to measure a string's representation in the screen


            --
            Ignacio Machin,
            ignacio.machin AT dot.state.fl.us
            Florida Department Of Transportation


            "dkode" <dkode8@gmail.c om> wrote in message
            news:1142363492 .766309.37610@p 10g2000cwp.goog legroups.com...[color=blue]
            > you can do the following:
            >
            > string myString = "This is my test string. It's length can vary.";
            >
            > m_table1.Cell(3 , 4).Width = 200;
            > if (myString.Lengt h > 200) {
            > // do processing or break apart the string into seperate lines I.E.
            > string stringPart = myString.Substr ing(0, 200);
            > string stringPart2 = myString.Substr ing(200, 200);
            > }
            >
            > is this what your looking for?
            >[/color]


            Comment

            • dotNuttah

              #7
              Re: length of string

              > "cc" <cmrchs@yahoo.c om> wrote in message[color=blue]
              > news:44170a6e$0 $15359$ba620e4c @news.skynet.be ...[color=green]
              >> Hi,
              >>
              >> From a C# application am I writing some text in a Word.Table, in a
              >> Cell of the Table to be more precise.
              >>
              >> The cell has a certain width and any string written in the cell will
              >> cover one or more lines in the cell depending on its length. In
              >> other words, it will use wordwrapping
              >>
              >> Now, I have to determine the amount of lines that the text will take
              >> when written in the cell.
              >> How can I do this ?
              >>
              >> thnx
              >> Chris[/color][/color]


              Ignacio Machin ( .NET/ C# MVP ) wrote:[color=blue]
              > Hi,
              >
              > You can use Graphics.Measur eString but you need to know the font,
              > another problem is the wordwrapping , I bet you would have to build
              > your own wrapping algorith and see how many lines you get[/color]


              Or give it the dimensions of the cell..

              Overloads Public Function MeasureString( _
              ByVal text As String, _
              ByVal font As Font, _
              ByVal layoutArea As SizeF _
              ) As SizeF

              :-)


              Comment

              • Dennis

                #8
                Re: length of string

                There is an overloaded .measurestring method that allows passing it the width
                and returning the size which will give you the height. Divide this by the
                font height and you will get the no. of lines.
                --
                Dennis in Houston


                "dotNuttah" wrote:
                [color=blue][color=green]
                > > "cc" <cmrchs@yahoo.c om> wrote in message
                > > news:44170a6e$0 $15359$ba620e4c @news.skynet.be ...[color=darkred]
                > >> Hi,
                > >>
                > >> From a C# application am I writing some text in a Word.Table, in a
                > >> Cell of the Table to be more precise.
                > >>
                > >> The cell has a certain width and any string written in the cell will
                > >> cover one or more lines in the cell depending on its length. In
                > >> other words, it will use wordwrapping
                > >>
                > >> Now, I have to determine the amount of lines that the text will take
                > >> when written in the cell.
                > >> How can I do this ?
                > >>
                > >> thnx
                > >> Chris[/color][/color]
                >
                >
                > Ignacio Machin ( .NET/ C# MVP ) wrote:[color=green]
                > > Hi,
                > >
                > > You can use Graphics.Measur eString but you need to know the font,
                > > another problem is the wordwrapping , I bet you would have to build
                > > your own wrapping algorith and see how many lines you get[/color]
                >
                >
                > Or give it the dimensions of the cell..
                >
                > Overloads Public Function MeasureString( _
                > ByVal text As String, _
                > ByVal font As Font, _
                > ByVal layoutArea As SizeF _
                > ) As SizeF
                >
                > :-)
                >
                >
                >[/color]

                Comment

                • Bill Butler

                  #9
                  Re: length of string

                  "Dennis" <Dennis@discuss ions.microsoft. com> wrote in message
                  news:F73678C0-C761-4569-A22D-8ADE6F786CF5@mi crosoft.com...[color=blue]
                  > There is an overloaded .measurestring method that allows passing it the width
                  > and returning the size which will give you the height. Divide this by the
                  > font height and you will get the no. of lines.
                  > --[/color]

                  Sure, that will work.
                  As long as you don't mind silly little things like line breaks in mid character.

                  You really need a more involved algorithm to do proper word wrapping.

                  Bill


                  Comment

                  • R. MacDonald

                    #10
                    Re: length of string

                    Hello, Chris,

                    Just a thought...

                    Do you really need to know the number of lines before you write to the
                    cell? Or could you write to the cell, and then ask: "How many lines are
                    in the cell?"

                    Cheers,
                    Randy


                    cc wrote:[color=blue]
                    > Hi,
                    >
                    > From a C# application am I writing some text in a Word.Table, in a Cell of
                    > the Table to be more precise.
                    >
                    > The cell has a certain width and any string written in the cell will cover
                    > one or more lines in the cell depending on its length. In other words, it
                    > will use wordwrapping
                    >
                    > Now, I have to determine the amount of lines that the text will take when
                    > written in the cell.
                    > How can I do this ?
                    >
                    > thnx
                    > Chris
                    >
                    >[/color]

                    Comment

                    • Dennis

                      #11
                      Re: length of string

                      It's really a pretty simple algorthim to break the string into multiple
                      strings at the line breaks then use measurestring on each substring and add
                      in lines for each line break. The user should be able to extrapolate from
                      measurestring method to this algorthim.

                      --
                      Dennis in Houston


                      "Bill Butler" wrote:
                      [color=blue]
                      > "Dennis" <Dennis@discuss ions.microsoft. com> wrote in message
                      > news:F73678C0-C761-4569-A22D-8ADE6F786CF5@mi crosoft.com...[color=green]
                      > > There is an overloaded .measurestring method that allows passing it the width
                      > > and returning the size which will give you the height. Divide this by the
                      > > font height and you will get the no. of lines.
                      > > --[/color]
                      >
                      > Sure, that will work.
                      > As long as you don't mind silly little things like line breaks in mid character.
                      >
                      > You really need a more involved algorithm to do proper word wrapping.
                      >
                      > Bill
                      >
                      >
                      >[/color]

                      Comment

                      • cc

                        #12
                        Re: length of string

                        That might be an option...

                        But I'll probably have a look at the Graphics.Measue string()

                        thanks to all of you !

                        Chris
                        "R. MacDonald" <scitec@NO-SP-AMcips.ca> wrote in message
                        news:4417d165$0 $88191$dbd45001 @news.wanadoo.n l...[color=blue]
                        > Hello, Chris,
                        >
                        > Just a thought...
                        >
                        > Do you really need to know the number of lines before you write to the
                        > cell? Or could you write to the cell, and then ask: "How many lines are
                        > in the cell?"
                        >
                        > Cheers,
                        > Randy
                        >
                        >
                        > cc wrote:[color=green]
                        > > Hi,
                        > >
                        > > From a C# application am I writing some text in a Word.Table, in a Cell[/color][/color]
                        of[color=blue][color=green]
                        > > the Table to be more precise.
                        > >
                        > > The cell has a certain width and any string written in the cell will[/color][/color]
                        cover[color=blue][color=green]
                        > > one or more lines in the cell depending on its length. In other words,[/color][/color]
                        it[color=blue][color=green]
                        > > will use wordwrapping
                        > >
                        > > Now, I have to determine the amount of lines that the text will take[/color][/color]
                        when[color=blue][color=green]
                        > > written in the cell.
                        > > How can I do this ?
                        > >
                        > > thnx
                        > > Chris
                        > >
                        > >[/color][/color]


                        Comment

                        Working...