开发者

Limiting a multi-line text box input so that everything will be printed

I'm sure this is a common problem, I don't know if there is a common solution. My problem is when the user is viewing said multi-line text box in the GUI they can just scroll down, not a problem. When I come to print however, certain text occasionally ends up going off the bottom of the given area for that text box.

We'll keep it simple and say it is not a rich text box so the开发者_C百科 user can't choose a larger font which may therefore go off the page. hmmph cross that bride if I need to =)

So I considered;

Using a character limit but then if you just return carriage a number of times, this would end up going off the bottom.

Or a 'row' limit which i'm not entirely sure how to implement but didn't seem right either

Finally I am coming to the conclusion that when you print said multi-line text box you must just expand the area on the printed document to fit whatever text has been entered. If this is the most elegant solution can anybody point me in the right direction for implementing such a feature?

Think of a notes field where the user could type as much as they wanted and the intention is to make sure all that is typed is printed.


Are you calling yourTextBox.DrawToBitMap? All that does is create a bitmap of what appears on the screen, it does not perform printer output, line layout, line breaks, printer fonts, margins, orientation, pagination, or anything else to do with printing.

If you want to send text to a printer, handling all the features mentioned above, you will have to use the System.Drawing.Printing.PrintDocument class and deal with all the things printers require that textBoxes do for you.


How and why are you printing a WinForm? I know this doesn't answer your question but printing a WinForm as a document is going to have these types of issues and I can guarantee that as you release your application to more users, more quirks will arise that will need to be fixed.

If you absolutely must print the Form and you want to limit the content in your TextBox about the best solution that I can offer is to force the font to be a fixed width font of a fixed size and the user can't modify it. Count how many characters can fit in the TextBox and set that value as a MexLength on the control.

Obviously, the user can hit the return/enter key which will use one character but will consume one entire line. To fix this, changed the TextBox's TextChange event to handle the newline character-- you can either strip it from the text or update the TextBox's MaxLength property to a smaller, appropriate value.

With possibly a few minor tweaks, obvious tweaks my recommendations should safely get you what you want. Still, I highly recommend looking for another printing mechanism for your form.

Use at your own risk.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜