Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
When I Enter some text in the text box I want to break the text into new line (7 characters in 1 line) when printing.

The code is given below

e.Graphics.DrawString(textBox24.Text, new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(32, 260));

What i will do

What I have tried:

When I Enter some text in the text box I want to break the text into new line (7 characters in 1 line) when printing.

The code is given below

e.Graphics.DrawString(textBox24.Text, new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(32, 260));

What i will do
Posted
Updated 28-Dec-17 23:09pm

Use the RectangleF overload: Graphics.DrawString Method (String, Font, Brush, RectangleF) (System.Drawing)[^] - it will wrap text so that it remains in the rectangle you supply.

BTW: Do yourself a favour, and stop using Visual Studio default names for everything - you may remember that "TextBox8" is the mobile number today, but when you have to modify it in three weeks time, will you then? Use descriptive names - "tbMobileNo" for example - and your code becomes easier to read, more self documenting, easier to maintain - and surprisingly quicker to code because Intellisense can get to to "tbMobile" in three keystrokes, where "TextBox8" takes thinking about and 8 keystrokes...
 
Share this answer
 
Use the String class and split it in the way you want, something like:
C#
string maintext = TextBox.Text;
int length = maintext.length;
for (int i = 0; i < maintext.length; i += 7)
{
    int sslength = 7;
    if (sslength > length)
        sslength = length;    // last section less than 7 characters
    string str = maintext.Substring(i, sslength);
// write the contents of str
    length -= 7;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900