from these two questions in SO | 1 | and | 2 |(this one's my own) I've tried to solve but I'm running into some problems.
Take a look at where I am stuck!!
- I have successfully word-wrapped this string "Damodarmarg, Kusunti, Inside Ringroad, Lalitpur, Bagmati, Nepal" using a code (see my SECOND CODE)
- But I DO NOT KNOW how to get the number of lines that have been wrapped. (because it wraps automatically).
This is the screenshot of my PRINT PREVIEW:
I want to put some space between Residential Address and Permanent Address. To do that I need to know how mny lines are being word-wrapped.
Thats my problem. The word-wrap is happening but NOT the line spacing! I just want to know how I can calculate the number of lines that have been word-wrapped so that I can run a function to do appropriate line spacing between two fields!- I want to know the number of lines that have been word-wrapped (which you can observe, in the screenshot above, is clearly = 2 here).
- Why do I need this number? ==> If x is the number of lines word wrapped, I want to execute the function newline() x number of times. That's why. Its my own function whose purpose is to correct line spacing among different fields.
Example:
- For a string "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- IF the page margin only allows 10 characters per line then
- The output should be
ABCDEFGHIJ
KLMNOPQRST
UVWXYZ - and the number of lines used, stored by variable linesFilled(say), should be = 3
- The output should be
- IF the page margin only allows 10 characters per line then
- Obviously,this is just a run-down example. In real-practice, I would like to have no character limit per line. Instead I want MeasureString to automatically know how many words fit in a line and then word-wrap the rest that are not fitting to next consecutive line(s). For your information: I have already done this much . You see, I seek your help only to know how to get the number of lines that have word-wrapped. That has been really tricky to work around.
What I have tried so far:
FIRST CODE:
My code looks like this(for the line counting; which you need to help me with):
int charactersFitted;
int linesFilled;
SizeF stringSize = new SizeF();
stringSize = e.Graphics.MeasureString("Residential Address: " +
RAddressTextBox.Text, stringFont, layoutSize, newStringFormat,
out charactersFitted, out linesFilled);
textBox1.Text = Convert.ToString(stringSize);
textBox2.Text = Convert.ToString(stringSize.Width);
So, this first code is supposed to give me the number of lines that were wrapped around the print margin. Currently just gives the width of the part of string that occupied the whole line as opposed to the number of lines the string has occupied (can I get a method to know the number of lines?)
SECOND CODE:
Graphics RAddress = e.Graphics;
SizeF RAddressLength = RAddress.MeasureString("Residential Address: "+
RAddressTextBox.Text, stringFont,700);
RAddress.DrawString("Residential Address: " + RAddressTextBox.Text,
stringFont, Brushes.Black, new RectangleF(new Point(pagemarginX,newline()),
RAddressLength), StringFormat.GenericTypographic);
and this second code helps we actually wrap the string when it does not fit in page margin(this second code here works perfectly at the moment. It automatically word-wraps text NOT fitting in a line to next consecutive line(s) but it DOES NOT tell me how many lines have been word-wrapped. THATS my problem)
Note: newline() is my own function which leaves one line when called. And pagemarginX sets approapirate margin. Thats all. Do not be confused. As for why I havent used DrawString in my FIRST CODE; I have been using both codes. This one to display the string and the FIRST CODE to count the lines in string. I haven't been able to count the number of lines with this one. Sorry for the confusion.
SAMPLE OUTPUT(s) FOR YOUR INFORMATION:
Currently, output of stringSize.Width is 114.226.
As suggested in some of the comments,
I tried outputting linesFilled instead of stringSize.Width and the output was 5 .
Another suggestion was to try int numLines = Convert.ToInt32(Math.Ceiling(layoutSize.Width / stringSize.Width)); which gave me the output 7 .
As shown in the screenshot of my PRINT PREVIEW over there^^^, I obviously need the output=2 for my string. Please somebody help me!
I welcome:
- Solutions that augment and enhance my FIRST CODE to produce correct lines of word-wrap as output for the string: "Damodarmarg, Kusunti, Inside Ringroad, Lalitpur, Bagmati, Nepal" (which should be 2)
- Solutions that can modify my SECOND CODE to also produce the number of lines that have been wordwrapped (This type of solution would be awesome!!)
- Creative solution: A solution that takes input as string and wordwraps that string to a margin and also produces the number of lines word-wrapped. (OR, whatever you think that solves this problem also works!)
My specifications are NOT rigid. You can solve this problem any way you are comfortable with! I have edited my question to include as much detail as possible. If you'd like the whole module, you can simply ask me too. I am hoping for a solution. Thanks!