PC Review Forums Newsgroups Microsoft DotNet Microsoft VB .NET length of string

Reply

length of string

 
Thread Tools Rate Thread
Old 14-03-2006, 06:26 PM   #1
cc
Guest
 
Posts: n/a
Default 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


  Reply With Quote
Old 14-03-2006, 06:43 PM   #2
dkode
Guest
 
Posts: n/a
Default 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

  Reply With Quote
Old 14-03-2006, 06:45 PM   #3
Ignacio Machin \( .NET/ C# MVP \)
Guest
 
Posts: n/a
Default Re: length of string

Hi,

You can use Graphics.MeasureString 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.com> wrote in message
news:44170a6e$0$15359$ba620e4c@news.skynet.be...
> 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
>
>



  Reply With Quote
Old 14-03-2006, 07:04 PM   #4
cc
Guest
 
Posts: n/a
Default 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.com> wrote in message
news:1142361785.950597.211720@j52g2000cwj.googlegroups.com...
> where is your string coming from before you insert it into the table?
> StringBuilder? String array?
>
> need some more info/code snippet to help
>



  Reply With Quote
Old 14-03-2006, 07:11 PM   #5
dkode
Guest
 
Posts: n/a
Default 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.Length > 200) {
// do processing or break apart the string into seperate lines I.E.
string stringPart = myString.Substring(0, 200);
string stringPart2 = myString.Substring(200, 200);
}

is this what your looking for?

  Reply With Quote
Old 14-03-2006, 08:41 PM   #6
Ignacio Machin \( .NET/ C# MVP \)
Guest
 
Posts: n/a
Default 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.com> wrote in message
news:1142363492.766309.37610@p10g2000cwp.googlegroups.com...
> 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.Length > 200) {
> // do processing or break apart the string into seperate lines I.E.
> string stringPart = myString.Substring(0, 200);
> string stringPart2 = myString.Substring(200, 200);
> }
>
> is this what your looking for?
>



  Reply With Quote
Old 14-03-2006, 10:27 PM   #7
dotNuttah
Guest
 
Posts: n/a
Default Re: length of string

> "cc" <cmrchs@yahoo.com> wrote in message
> news:44170a6e$0$15359$ba620e4c@news.skynet.be...
>> 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



Ignacio Machin ( .NET/ C# MVP ) wrote:
> Hi,
>
> You can use Graphics.MeasureString 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



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

:-)


  Reply With Quote
Old 15-03-2006, 12:49 AM   #8
=?Utf-8?B?RGVubmlz?=
Guest
 
Posts: n/a
Default 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:

> > "cc" <cmrchs@yahoo.com> wrote in message
> > news:44170a6e$0$15359$ba620e4c@news.skynet.be...
> >> 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

>
>
> Ignacio Machin ( .NET/ C# MVP ) wrote:
> > Hi,
> >
> > You can use Graphics.MeasureString 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

>
>
> 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
>
> :-)
>
>
>

  Reply With Quote
Old 15-03-2006, 01:24 AM   #9
Bill Butler
Guest
 
Posts: n/a
Default Re: length of string

"Dennis" <Dennis@discussions.microsoft.com> wrote in message
news:F73678C0-C761-4569-A22D-8ADE6F786CF5@microsoft.com...
> 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.
> --


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


  Reply With Quote
Old 15-03-2006, 08:33 AM   #10
R. MacDonald
Guest
 
Posts: n/a
Default 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:
> 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
>
>

  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off