Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7525151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:27:34+00:00 2026-05-30T03:27:34+00:00

I already have this code but it gives me the wrong result. private void

  • 0

I already have this code but it gives me the wrong result.

    private void document_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
    int charPerLine = e.MarginBounds.Width / (int)e.Graphics.MeasureString("m", txtMain.Font).Width;
    }

The txtMain is a textbox.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T03:27:35+00:00Added an answer on May 30, 2026 at 3:27 am

    This should do the trick. Be careful when dividing by a variable cast to an integer. You are leaving yourself open to a divide-by-zero here in the event that the Width property is less than one, which will be truncated to zero. It may be unlikely that you will have such a small font in your application, but it is still good practice.

    private void document_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        if( (int)e.Graphics.MeasureString("m", txtMain.Font).Width > 0 )
        {
    
            int charPerLine = 
                e.MarginBounds.Width / (int)e.Graphics.MeasureString("m", txtMain.Font).Width;
        }
    }
    

    The real issue though is why do you even need to know the number of characters per line. Unless you are trying to do some sort of ASCII art, you can use the different overloads of Graphics.DrawString to have GDI+ layout the text for you inside a bounding rectangle without needing to know how many characters fit on a line.

    This sample from MSDN shows you how to do this:

    // Create string to draw.
    String drawString = "Sample Text";
    
    // Create font and brush.
    Font drawFont = new Font("Arial", 16);
    SolidBrush drawBrush = new SolidBrush(Color.Black);
    
    // Create rectangle for drawing.
    float x = 150.0F;
    float y = 150.0F;
    float width = 200.0F;
    float height = 50.0F;
    RectangleF drawRect = new RectangleF(x, y, width, height);
    
    // Draw rectangle to screen.
    Pen blackPen = new Pen(Color.Black);
    e.Graphics.DrawRectangle(blackPen, x, y, width, height);
    
    // Set format of string.
    StringFormat drawFormat = new StringFormat();
    drawFormat.Alignment = StringAlignment.Center;
    
    // Draw string to screen.
    e.Graphics.DrawString(drawString, drawFont, drawBrush, drawRect, drawFormat);
    

    So if you are trying to print a page of text, you can just set the drawRect to the e.MarginBounds and plug a page worth of text in for drawString.

    Another thing, if you are trying to print tabular data, you can just partition the page into rectangles – one for each column/row (however you need it), and use e.Graphics.DrawLine overloads to print the table borders.

    If you post more details on what you are actually trying to achieve we can help more.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my php code (I already have a connection to the db): $result
I have already have this code to force these URLs to HTTPS: RewriteCond %{HTTPS}
This is similar to this question, except the code is already written. I have
I am sorry I have already asked this question on Superuser, but nobody answers
I have already posted a question about this, but the situation has changed sufficiently
I have already googled for this I have a Table with following structure in
This just saves time. Since I already have a web applciation. I can just
Possible Duplicate: What’s with the love of dynamic Languages I have already read this
Similar to this question: link However I have already mastered that. My problem is
im sorry if this was posted already i have been looking to no avail..

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.