Textbox Linecount

N2DFire

New member
Joined
Jul 7, 2003
Messages
3
O.K. A Co-Worker and I need help.

He is working on a major web interfaced application. As a part of the design criteria, the PTB (Powers That Be) want the app to generate a formatted printed report from the information the users have entered.

Problem - by formatted they mean that if a person typed 7 paragraphs in one text box on a form - that entire set of 7 paragraphs must:
1) all print on the same page or
2) break at a logical point (i.e. not mid paragraph)

He gotten 99% of this problem licked except he needs to be able to determine how many lines are in the text box. The data entry forms are wysiwyg so the data is already formatted correctly for printing, he just needs to know how many lines of data are in the text box so hell know when to insert page breaks, headers, and such.

HELP !!!

Any trick or tips will be GREATLY appreciated.
 
SORRY, but you cant do this!

You dont know which printer is on the client. Is it HP or Canon, is it A3, A4 or Plotter........

Each printer has other margins and so on....


But if you anyway want to know how many lines typed in the Multilinetextbox this code will solve your problem:

Code:
int LinesCount = TextBox1.Text.Split((char)13).Length;

Thats it!

-WebJumper
 
Ill blame it on the fact that I have a hard time explaining a problem clearly without being able to draw pictures, but - it isnt that simple. I did forget to mention the text boxes have word wrap turned on so just counting hard returns wont work.

Using 1 specific instance in his program . . .
There is a text box that is only 30 Characters wide. The previous sentence would then be 2 "lines" in this text box (because it gets auto-wraped for exceeding the 30 character length).

Heres the rub. Like I said before - the input forms are pretty much wysiwig, but the data, as it gets printed on the report, is well columnar is the best way I can think of to describe it.

Each Entry is a "Step", each Step has parts which get printed side by side in fixed width areas (hence the columns thing). The problem is that ALL the information in each part of the step has to be printed together. (Design Parameter no getting around it)

So lets say that step 6 Part 1 is 3 lines, Part 3 is 2 Lines, and part 3 is 6 lines. He has to know how many lines he needs for the entire step so hell know if it will fit on the current page or if he needs to page break, write headers and then print the step.

Also - I get what youre saying about the different printers but he says hes got that figured out already.

Im just a VB programmer (and not a great one at that :D ) and I helped him come up with a solution that hes happy with.

Below is the code for all to see (and dissect and hopefully make better).

Thanks for the help though, Sorry for the cruddy info the first time around.

Code:
Function LineCount(InStr as String, intMaxWidth as integer) as Integer
         Dim LastSpace as Integer
         Dim X as Integer
         Dim Z as integer
         Dim Count as Integer
         dim i as integer
         dim intMaxLength as integer = Len(InStr)

         dim rx as regex
         dim m as match

         rx = new Regex("\n", RegexOptions.IgnoreCase)

         Count = 0

         Do until len(InStr) = 0
             LastSpace = 0
             if Len(InStr)> intMaxWidth then Z = intMaxWidth Else Z = Len(InStr)
             For X = 1 to Z + 1
                 m = rx.Match(Mid(InStr,X,2))
                 if m.success then
                         Count += 1
                         InStr = Mid(InStr,X+2,Len(InStr))

                         LastSpace = -99
                         exit for
                 end if


                 if Mid(InStr,X,1) = " "
                     LastSpace = X
                 end if

             Next

             Select Case LastSpace
                 Case is = 0
                     Count += 1
                     InStr = Mid(InStr,intMaxWidth+1,Len(InStr))

                 Case is > 0
                     Count += 1
                     if Z < intMaxWidth then
                        InStr =""
                     else
                        InStr = Mid(InStr,LastSpace+1,Len(InStr))
                     end if
             End Select
         Loop
         return Count
     End Function

Sorry this isnt commented yet - we were writing on the fly and just finished about an hour ago.
 


Write your reply...
Back
Top